diff options
Diffstat (limited to 'source/github-helpers/get-default-branch.ts')
-rw-r--r-- | source/github-helpers/get-default-branch.ts | 60 |
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()!); } |