diff options
Diffstat (limited to 'source/github-helpers')
-rw-r--r-- | source/github-helpers/dom-formatters.ts | 8 | ||||
-rw-r--r-- | source/github-helpers/get-default-branch.ts | 36 | ||||
-rw-r--r-- | source/github-helpers/github-url.ts | 7 | ||||
-rw-r--r-- | source/github-helpers/index.ts | 21 |
4 files changed, 38 insertions, 34 deletions
diff --git a/source/github-helpers/dom-formatters.ts b/source/github-helpers/dom-formatters.ts index 99783f81..9340ec07 100644 --- a/source/github-helpers/dom-formatters.ts +++ b/source/github-helpers/dom-formatters.ts @@ -3,7 +3,7 @@ import linkifyURLsCore from 'linkify-urls'; import linkifyIssuesCore from 'linkify-issues'; import getTextNodes from '../helpers/get-text-nodes'; -import {getOwnerAndRepo} from '.'; +import {getCurrentRepository} from '.'; import parseBackticksCore from './parse-backticks'; // Shared class necessary to avoid also shortening the links @@ -12,12 +12,12 @@ export const linkifiedURLClass = 'rgh-linkified-code'; // If we are not in a repo, relative issue references won't make sense // but `user`/`repo` need to be set to avoid breaking errors in `linkify-issues` // https://github.com/sindresorhus/refined-github/issues/1305 -const currentRepo = getOwnerAndRepo(); +const currentRepo = getCurrentRepository(); export function linkifyIssues(element: Element, options: Partial<linkifyIssuesCore.TypeDomOptions> = {}): void { const linkified = linkifyIssuesCore(element.textContent!, { - user: currentRepo.ownerName ?? '/', - repository: currentRepo.repoName ?? '/', + user: currentRepo.owner ?? '/', + repository: currentRepo.name ?? '/', type: 'dom', baseUrl: '', ...options, 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()}` }); diff --git a/source/github-helpers/github-url.ts b/source/github-helpers/github-url.ts index 1966ee2a..97c347a0 100644 --- a/source/github-helpers/github-url.ts +++ b/source/github-helpers/github-url.ts @@ -12,8 +12,6 @@ export default class GitHubURL { // @ts-expect-error filePath: string; - assign = Object.assign.bind(null, this); - private internalUrl: URL; constructor(url: string) { @@ -30,6 +28,11 @@ export default class GitHubURL { return this.href; } + assign(...replacements: Array<Partial<GitHubURL>>): this { + Object.assign(this, ...replacements); + return this; + } + private disambiguateReference(ambiguousReference: string[]): {branch: string; filePath: string} { const branch = ambiguousReference[0]; const filePathFromSearch = this.searchParams.getAll('path[]').join('/'); diff --git a/source/github-helpers/index.ts b/source/github-helpers/index.ts index 8f9432f3..fc02b7a8 100644 --- a/source/github-helpers/index.ts +++ b/source/github-helpers/index.ts @@ -34,20 +34,21 @@ export const isFirefox = navigator.userAgent.includes('Firefox/'); export const getRepoURL = (): string => location.pathname.slice(1).split('/', 2).join('/').toLowerCase(); export const getRepoGQL = (): string => { - const {ownerName, repoName} = getOwnerAndRepo(); - return `owner: "${ownerName!}", name: "${repoName!}"`; + const {owner, name} = getCurrentRepository(); + return `owner: "${owner!}", name: "${name!}"`; }; -export const getOwnerAndRepo = (): { - ownerName?: string; - repoName?: string; -} => { - const [, ownerName, repoName] = location.pathname.split('/', 3); - return {ownerName, repoName}; -}; +export interface RepositoryInfo { + owner: string; + name: string; +} +export const getCurrentRepository = oneTime((): Partial<RepositoryInfo> => { + const [, owner, name] = location.pathname.split('/', 3); + return {owner, name}; +}); export function getForkedRepo(): string | undefined { - return select<HTMLAnchorElement>('.fork-flag a')?.pathname.slice(1); + return select<HTMLMetaElement>('[name="octolytics-dimension-repository_parent_nwo"]')?.content; } export const parseTag = (tag: string): {version: string; namespace: string} => { |