summaryrefslogtreecommitdiff
path: root/source/github-helpers/get-default-branch.ts
diff options
context:
space:
mode:
authorGravatar Fregante <opensource@bfred.it> 2020-06-16 19:56:43 +0200
committerGravatar GitHub <noreply@github.com> 2020-06-16 19:56:43 +0200
commita918f9a3eb6827dd67efdd1bcf362df2e7152406 (patch)
tree4aeb0966e89ed9a641f519f8934a959a868a8823 /source/github-helpers/get-default-branch.ts
parent1d003c988da53f575b0285f8366ada9b80b76510 (diff)
downloadrefined-github-a918f9a3eb6827dd67efdd1bcf362df2e7152406.tar.gz
refined-github-a918f9a3eb6827dd67efdd1bcf362df2e7152406.tar.zst
refined-github-a918f9a3eb6827dd67efdd1bcf362df2e7152406.zip
Meta: Minor improvements and lint (#3231)
Diffstat (limited to 'source/github-helpers/get-default-branch.ts')
-rw-r--r--source/github-helpers/get-default-branch.ts36
1 files changed, 18 insertions, 18 deletions
diff --git a/source/github-helpers/get-default-branch.ts b/source/github-helpers/get-default-branch.ts
index f9ab632c..33ad4d22 100644
--- a/source/github-helpers/get-default-branch.ts
+++ b/source/github-helpers/get-default-branch.ts
@@ -1,8 +1,9 @@
-import select from 'select-dom';
import cache from 'webext-storage-cache';
+import select from 'select-dom';
+import {isForkedRepo} from 'github-url-detection/esm';
import * as api from './api';
-import {getRepoURL, getRepoGQL} from '.';
+import {RepositoryInfo, getCurrentRepository, getRepoURL} from '.';
// This regex should match all of these combinations:
// "This branch is even with master."
@@ -11,30 +12,29 @@ import {getRepoURL, getRepoGQL} from '.';
// "This branch is 1 commit ahead, 27 commits behind master."
const branchInfoRegex = /([^ ]+)\.$/;
-function parseBranchFromDom(): string | undefined {
- if (select.exists('.repohead h1 .octicon-repo-forked')) {
- return; // It's a fork, no "default branch" info available #1132
+export default cache.function(async (repository: Partial<RepositoryInfo> = getCurrentRepository()): Promise<string> => {
+ if (JSON.stringify(repository) === JSON.stringify(getCurrentRepository())) {
+ if (!isForkedRepo()) {
+ // We can find the name in the infobar, available in folder views
+ const branchInfo = select('.branch-infobar')?.textContent!.trim();
+ const defaultBranch = branchInfoRegex.exec(branchInfo!)?.[1];
+ if (defaultBranch) {
+ return defaultBranch;
+ }
+ }
}
- // We can find the name in the infobar, available in folder views
- const branchInfo = select('.branch-infobar')?.textContent!.trim();
- return branchInfoRegex.exec(branchInfo!)?.[1];
-}
-
-async function fetchFromApi(): Promise<string> {
- const {repository} = await api.v4(`
- repository(${getRepoGQL()}) {
+ const response = await api.v4(`
+ repository(owner: "${repository.owner!}", name: "${repository.name!}") {
defaultBranchRef {
name
}
}
`);
- return repository.defaultBranchRef.name;
-}
-
-export default cache.function(async () => parseBranchFromDom() ?? fetchFromApi(), {
+ return response.repository.defaultBranchRef.name;
+}, {
maxAge: 10,
staleWhileRevalidate: 20,
- cacheKey: () => 'default-branch:' + getRepoURL()
+ cacheKey: ([repository]) => repository ? `default-branch:${repository.owner!}/${repository.name!}` : `default-branch:${getRepoURL()}`
});