diff options
author | 2023-05-11 05:28:40 +0800 | |
---|---|---|
committer | 2023-05-10 21:28:40 +0000 | |
commit | 48658675d7bbb3ecd5e3a51df0447b65bb0b1219 (patch) | |
tree | d70832506df4326c612f721b1c6c496513db0eac /source/github-helpers | |
parent | 358edc207f72fb01a32c916c28b90839359c662a (diff) | |
download | refined-github-23.5.10.tar.gz refined-github-23.5.10.tar.zst refined-github-23.5.10.zip |
Improve reliability of `list-prs-for-branch` (#6641)23.5.10
Diffstat (limited to '')
-rw-r--r-- | source/github-helpers/api.ts | 3 | ||||
-rw-r--r-- | source/github-helpers/get-current-git-ref.ts | 4 | ||||
-rw-r--r-- | source/github-helpers/get-default-branch.ts | 5 | ||||
-rw-r--r-- | source/github-helpers/index.ts | 16 | ||||
-rw-r--r-- | source/github-helpers/selectors.ts | 3 |
5 files changed, 27 insertions, 4 deletions
diff --git a/source/github-helpers/api.ts b/source/github-helpers/api.ts index d0fd1043..5aaf5e4a 100644 --- a/source/github-helpers/api.ts +++ b/source/github-helpers/api.ts @@ -248,3 +248,6 @@ export async function getError(apiResponse: JsonObject): Promise<RefinedGitHubAP error.response = apiResponse; return error; } + +// Export single API object as default +export * as default from './api.js'; diff --git a/source/github-helpers/get-current-git-ref.ts b/source/github-helpers/get-current-git-ref.ts index 71b27222..74abb87a 100644 --- a/source/github-helpers/get-current-git-ref.ts +++ b/source/github-helpers/get-current-git-ref.ts @@ -1,6 +1,7 @@ import {isRepoCommitList} from 'github-url-detection'; import select from 'select-dom'; +import {extractCurrentBranchFromBranchPicker} from './index.js'; import {branchSelector} from './selectors.js'; const typesWithGitRef = new Set(['tree', 'blob', 'blame', 'edit', 'commit', 'commits', 'compare']); @@ -10,7 +11,8 @@ const titleWithGitRef = / at (?<branch>[.\w-/]+)( ยท [\w-]+\/[\w-]+)?$/i; export default function getCurrentGitRef(): string | undefined { // Note: This is not in the <head> so it's only available on AJAXed loads. // It appears on every Code page except `commits` on folders/files - const refViaPicker = select(branchSelector)?.textContent!.trim(); + const picker = select(branchSelector); + const refViaPicker = picker && extractCurrentBranchFromBranchPicker(picker); if (refViaPicker) { return refViaPicker; } diff --git a/source/github-helpers/get-default-branch.ts b/source/github-helpers/get-default-branch.ts index ffb87e23..85a61040 100644 --- a/source/github-helpers/get-default-branch.ts +++ b/source/github-helpers/get-default-branch.ts @@ -3,7 +3,7 @@ import elementReady from 'element-ready'; import {type RepositoryInfo} from 'github-url-detection'; import * as api from './api.js'; -import {getRepo} from './index.js'; +import {extractCurrentBranchFromBranchPicker, getRepo} from './index.js'; import {branchSelector} from './selectors.js'; const isCurrentRepo = ({nameWithOwner}: RepositoryInfo): boolean => Boolean(getRepo()?.nameWithOwner === nameWithOwner); @@ -17,8 +17,7 @@ async function fromDOM(): Promise<string | 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(); + return extractCurrentBranchFromBranchPicker((await elementReady(branchSelector))!); } async function fromAPI(repository: RepositoryInfo): Promise<string> { diff --git a/source/github-helpers/index.ts b/source/github-helpers/index.ts index ed491a7b..979ffbcd 100644 --- a/source/github-helpers/index.ts +++ b/source/github-helpers/index.ts @@ -134,3 +134,19 @@ export async function isArchivedRepoAsync(): Promise<boolean> { export const userCanLikelyMergePR = (): boolean => select.exists('.discussion-sidebar-item .octicon-lock'); export const cacheByRepo = (): string => getRepo()!.nameWithOwner; + +// Commit lists for files and folders lack a branch selector +export const isRepoCommitListRoot = (): boolean => pageDetect.isRepoCommitList() && document.title.startsWith('Commits'); + +// Don't make the argument optional, sometimes we really expect it to exist and want to throw an error +export function extractCurrentBranchFromBranchPicker(branchPicker: HTMLElement): string { + return branchPicker.title === 'Switch branches or tags' + ? branchPicker.textContent!.trim() // Branch name is shown in full + : branchPicker.title; // Branch name was clipped, so they placed it in the title attribute +} + +export function addAfterBranchSelector(branchSelectorParent: HTMLDetailsElement, sibling: HTMLElement): void { + const row = branchSelectorParent.closest('.position-relative')!; + row.classList.add('d-flex', 'flex-shrink-0', 'gap-2'); + row.append(sibling); +} diff --git a/source/github-helpers/selectors.ts b/source/github-helpers/selectors.ts index d52bb04e..24663e43 100644 --- a/source/github-helpers/selectors.ts +++ b/source/github-helpers/selectors.ts @@ -15,6 +15,9 @@ export const branchSelector_ = [ 'https://github.com/refined-github/sandbox/commits', ]; +export const branchSelectorParent = 'details#branch-select-menu'; +export const branchSelectorParent_ = branchSelector_; + export const directoryListingFileIcon = [ // .color-fg-muted selects only files; some icon extensions use `img` tags '.react-directory-filename-column > :is(svg, img).color-fg-muted', |