summaryrefslogtreecommitdiff
path: root/source/github-helpers/get-default-branch.ts
diff options
context:
space:
mode:
authorGravatar Federico Brigante <me@fregante.com> 2023-05-10 22:58:08 +0800
committerGravatar GitHub <noreply@github.com> 2023-05-10 22:58:08 +0800
commit579afb1fae65bc4cd7ef22e6526fce510133d731 (patch)
treebcc780181157d0f0d5fb2bef23f00bc5e6bcda00 /source/github-helpers/get-default-branch.ts
parent1f1045fb2f25580c76ad23dd2312eafb2c9a4073 (diff)
downloadrefined-github-579afb1fae65bc4cd7ef22e6526fce510133d731.tar.gz
refined-github-579afb1fae65bc4cd7ef22e6526fce510133d731.tar.zst
refined-github-579afb1fae65bc4cd7ef22e6526fce510133d731.zip
Default branch and current branch detector improvements (#6636)
Diffstat (limited to 'source/github-helpers/get-default-branch.ts')
-rw-r--r--source/github-helpers/get-default-branch.ts60
1 files changed, 35 insertions, 25 deletions
diff --git a/source/github-helpers/get-default-branch.ts b/source/github-helpers/get-default-branch.ts
index efbbdd4c..ffb87e23 100644
--- a/source/github-helpers/get-default-branch.ts
+++ b/source/github-helpers/get-default-branch.ts
@@ -1,27 +1,27 @@
import cache from 'webext-storage-cache';
import elementReady from 'element-ready';
-import * as pageDetect from 'github-url-detection';
+import {type RepositoryInfo} from 'github-url-detection';
import * as api from './api.js';
import {getRepo} from './index.js';
import {branchSelector} from './selectors.js';
-const isCurrentRepo = ({nameWithOwner}: pageDetect.RepositoryInfo): boolean => Boolean(getRepo()?.nameWithOwner === nameWithOwner);
+const isCurrentRepo = ({nameWithOwner}: RepositoryInfo): boolean => Boolean(getRepo()?.nameWithOwner === nameWithOwner);
-// DO NOT use optional arguments/defaults in "cached functions" because they can't be memoized effectively
-// https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1864
-const _getDefaultBranch = cache.function('default-branch', async (repository: pageDetect.RepositoryInfo): Promise<string> => {
- // TODO: extract from `ref-selector` if available https://github.com/refined-github/refined-github/issues/6557
- if (isCurrentRepo(repository) && ['', 'commits'].includes(repository.path)) {
- // We're on the default branch, so we can extract it from the current page. This usually happens on the pages:
- // @example /user/repo
- // @example /user/repo/commits (without further path)
- const branchPicker = await elementReady(branchSelector);
- if (branchPicker) {
- return branchPicker.textContent!.trim();
- }
+// Do not make this function complicated. We're only optimizing for the repo root.
+async function fromDOM(): Promise<string | undefined> {
+ if (!['', 'commits'].includes(getRepo()!.path)) {
+ return undefined;
}
+ // We're on the default branch, so we can extract it from the current page. This exclusively happens on the exact pages:
+ // /user/repo
+ // /user/repo/commits (without further path)
+ const branchPicker = await elementReady(branchSelector);
+ return branchPicker!.textContent!.trim();
+}
+
+async function fromAPI(repository: RepositoryInfo): Promise<string> {
const response = await api.v4(`
repository(owner: "${repository.owner}", name: "${repository.name}") {
defaultBranchRef {
@@ -31,16 +31,26 @@ const _getDefaultBranch = cache.function('default-branch', async (repository: pa
`);
return response.repository.defaultBranchRef.name;
-}, {
- maxAge: {hours: 1},
- staleWhileRevalidate: {days: 20},
- cacheKey: ([repository]) => repository.nameWithOwner,
-});
-
-export default async function getDefaultBranch(repository: pageDetect.RepositoryInfo | undefined = getRepo()): Promise<string> {
- if (!repository) {
- throw new Error('getDefaultBranch was called on a non-repository page');
- }
+}
+
+// DO NOT use optional arguments/defaults in "cached functions" because they can't be memoized effectively
+// https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1864
+export const getDefaultBranchOfRepo = cache.function('default-branch',
+ async (repository: RepositoryInfo): Promise<string> => {
+ if (!repository) {
+ throw new Error('getDefaultBranch was called on a non-repository page');
+ }
+
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- Wrong, the type is `false | undefined`
+ return (isCurrentRepo(repository) && await fromDOM()) || fromAPI(repository);
+ },
+ {
+ maxAge: {hours: 1},
+ staleWhileRevalidate: {days: 20},
+ cacheKey: ([repository]) => repository.nameWithOwner,
+ },
+);
- return _getDefaultBranch(repository);
+export default async function getDefaultBranch(): Promise<string> {
+ return getDefaultBranchOfRepo(getRepo()!);
}