Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support working when gitea url contains subpaths #90

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions src/common/adapters/gitea.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Gitea extends PjaxAdapter {
}

// Else, return true only if it isn't in a huge repo list, which we must lazy load
const key = `${repo.username}/${repo.reponame}`;
const key = `${repo.subpath}${repo.username}/${repo.reponame}`;
const hugeRepos = await extStore.get(STORE.HUGE_REPOS);
if (hugeRepos[key] && isValidTimeStamp(hugeRepos[key])) {
// Update the last load time of the repo
Expand Down Expand Up @@ -149,14 +149,26 @@ class Gitea extends PjaxAdapter {
}

async getRepoData(currentRepo, token, cb) {
// (username)/(reponame)[/(type)][/(typeId)]
// [(subpath)*3]/(username)/(reponame)[/(type)][/(typeId)]
// eslint-disable-next-line no-useless-escape
const match = window.location.pathname.match(/([^\/]+)\/([^\/]+)(?:\/([^\/]+))?(?:\/([^\/]+))?/);

const username = match[1];
const reponame = match[2];
const type = match[3];
const typeId = match[4];
const match = window.location.pathname.match(/([^\/]+)\/([^\/]+)(?:\/([^\/]+))?(?:\/([^\/]+))?(?:\/([^\/]+))?(?:\/([^\/]+))?(?:\/([^\/]+))?/);
const htmlTag = window.document.getElementsByClassName('repo-title')[0].getElementsByTagName('a');
const username = htmlTag[0].innerHTML;
const reponame = htmlTag[1].innerHTML;
let flag = -1;
match.forEach(function(e, i, v) {
if (e === username && flag === -1) {
flag = i;
}
});
let subpath = '';
for (let i = 1; i < flag; i++) {
subpath = subpath === '' ? match[i] : subpath + '/' + match[i];
}
// Does not concatenate redundant '/' when there are no subpaths
if (subpath !== '') subpath = subpath + '/';
const type = match[flag + 2];
const typeId = match[flag + 3];

const isPR = type === 'pulls';

Expand Down Expand Up @@ -191,7 +203,7 @@ class Gitea extends PjaxAdapter {
((type === 'releases' || type === 'tags') && 'master') ||
// Get commit ID or branch name from the DOM
branchFromSummary ||
($('.overall-summary .numbers-summary .commits a').attr('href') || '').replace(`/${username}/${reponame}/commits/`, '') ||
($('.overall-summary .numbers-summary .commits a').attr('href') || '').replace(`/${subpath}${username}/${reponame}/commits/`, '') ||
// The above should work for tree|blob, but if DOM changes, fallback to use ID from URL
((type === 'tree' || type === 'blob') && typeId) ||
// Use target branch in a PR page
Expand All @@ -205,13 +217,14 @@ class Gitea extends PjaxAdapter {
// Reuse last selected branch if exist
(currentRepo.username === username && currentRepo.reponame === reponame && currentRepo.branch) ||
// Get default branch from cache
this._defaultBranch[username + '/' + reponame];
this._defaultBranch[subpath + username + '/' + reponame];

const showOnlyChangedInPR = await extStore.get(STORE.PR);
const pullNumber = isPR && showOnlyChangedInPR ? typeId : null;
const pullHead = isPR ? ($('.commit-ref.head-ref').attr('title') || ':').match(/:(.*)/)[1] : null;
const displayBranch = isPR && pullHead ? `${branch} < ${pullHead}` : null;
const repo = {
subpath,
username,
reponame,
branch,
Expand All @@ -231,7 +244,7 @@ class Gitea extends PjaxAdapter {
(err, data) => {
if (err) return cb(err);
// eslint-disable-next-line no-multi-assign
repo.branch = this._defaultBranch[username + '/' + reponame] = data.default_branch || 'master';
repo.branch = this._defaultBranch[subpath + username + '/' + reponame] = data.default_branch || 'master';
cb(null, repo);
}
);
Expand Down Expand Up @@ -284,7 +297,7 @@ class Gitea extends PjaxAdapter {

// @override
getItemHref(repo, type, encodedPath, encodedBranch) {
return `/${repo.username}/${repo.reponame}/src/${encodedBranch}/${encodedPath}`;
return `/${repo.subpath}${repo.username}/${repo.reponame}/src/${encodedBranch}/${encodedPath}`;
}

get isOnPRPage() {
Expand Down Expand Up @@ -438,7 +451,10 @@ class Gitea extends PjaxAdapter {
}

async getContent(path, opts) {
const host = window.location.protocol + '//' + (window.location.host === 'github.com' ? 'api.github.com' : window.location.host + '/api/v1');
const host =
window.location.protocol +
'//' +
(window.location.host === 'github.com' ? 'api.github.com' : window.location.host + '/' + opts.repo.subpath + 'api/v1');
const url = `${host}/repos/${opts.repo.username}/${opts.repo.reponame}`;

const contentPath = path || this.getContentPath() || '';
Expand Down Expand Up @@ -478,7 +494,7 @@ class Gitea extends PjaxAdapter {
if (path && path.startsWith('http')) {
url = path;
} else {
const host = window.location.protocol + '//' + window.location.host + '/api/v1';
const host = window.location.protocol + '//' + window.location.host + '/' + opts.repo.subpath + 'api/v1';
url = `${host}/repos/${opts.repo.username}/${opts.repo.reponame}${path || ''}`;
}

Expand All @@ -498,7 +514,7 @@ class Gitea extends PjaxAdapter {
if (path && path.indexOf('/git/trees') === 0 && data.truncated) {
try {
const hugeRepos = await extStore.get(STORE.HUGE_REPOS);
const repo = `${opts.repo.username}/${opts.repo.reponame}`;
const repo = `${opts.repo.subpath}${opts.repo.username}/${opts.repo.reponame}`;
const repos = Object.keys(hugeRepos).filter(hugeRepoKey => isValidTimeStamp(hugeRepos[hugeRepoKey]));
if (!hugeRepos[repo]) {
// If there are too many repos memoized, delete the oldest one
Expand Down