summaryrefslogtreecommitdiff
path: root/source/features/pr-base-commit.tsx
diff options
context:
space:
mode:
authorGravatar Federico Brigante <me@fregante.com> 2023-04-26 15:37:42 +0800
committerGravatar GitHub <noreply@github.com> 2023-04-26 15:37:42 +0800
commit55dfdfd903bd7d36e0c2f3dc46847bddc73544f5 (patch)
tree34d969496dfcc1836af820975bc6e7ede37017d1 /source/features/pr-base-commit.tsx
parent7e94056bb1edae5c64f4ed9901547419284a170f (diff)
downloadrefined-github-55dfdfd903bd7d36e0c2f3dc46847bddc73544f5.tar.gz
refined-github-55dfdfd903bd7d36e0c2f3dc46847bddc73544f5.tar.zst
refined-github-55dfdfd903bd7d36e0c2f3dc46847bddc73544f5.zip
Add `pr-base-commit` feature (#6538)
Co-authored-by: Yakov <16872793+yakov116@users.noreply.github.com>
Diffstat (limited to 'source/features/pr-base-commit.tsx')
-rw-r--r--source/features/pr-base-commit.tsx85
1 files changed, 85 insertions, 0 deletions
diff --git a/source/features/pr-base-commit.tsx b/source/features/pr-base-commit.tsx
new file mode 100644
index 00000000..42d3e3a6
--- /dev/null
+++ b/source/features/pr-base-commit.tsx
@@ -0,0 +1,85 @@
+import React from 'dom-chef';
+import select from 'select-dom';
+
+import * as pageDetect from 'github-url-detection';
+
+import features from '../feature-manager';
+import observe from '../helpers/selector-observer';
+import * as api from '../github-helpers/api';
+import {getBranches} from '../github-helpers/pr-branches';
+import getPrInfo, {PullRequestInfo} from '../github-helpers/get-pr-info';
+import pluralize from '../helpers/pluralize';
+import {buildRepoURL} from '../github-helpers';
+import {linkifyCommit} from '../github-helpers/dom-formatters';
+import {removeTextNodeContaining} from '../helpers/dom-utils';
+
+function getBaseCommitNotice(prInfo: PullRequestInfo): JSX.Element {
+ const {base} = getBranches();
+ const commit = linkifyCommit(prInfo.baseRefOid);
+ const count = pluralize(prInfo.behindBy, '$$ commit');
+ const countLink = (
+ <a href={buildRepoURL('compare', `${prInfo.baseRefOid.slice(0, 8)}...${base.branch}`)}>
+ {count}
+ </a>
+ );
+ return (
+ <>It’s {countLink} behind (base commit: {commit})</>
+ );
+}
+
+async function addInfo(statusMeta: Element): Promise<void> {
+ // Selector copied from GitHub. Don't @ me
+ // This excludes hidden ".status-meta" items without adding this longass selector to the observer
+ // Added: .rgh-update-pr-from-base-branch-row
+ if (!statusMeta.closest('.merge-pr.is-merging .merging-body, .merge-pr.is-merging .merge-commit-author-email-info, .merge-pr.is-merging-solo .merging-body, .merge-pr.is-merging-jump .merging-body, .merge-pr.is-merging-group .merging-body, .merge-pr.is-rebasing .rebasing-body, .merge-pr.is-squashing .squashing-body, .merge-pr.is-squashing .squash-commit-author-email-info, .merge-pr.is-merging .branch-action-state-error-if-merging .merging-body-merge-warning, .rgh-update-pr-from-base-branch-row')) {
+ return;
+ }
+
+ const {base} = getBranches();
+ const prInfo = await getPrInfo(base.relative);
+ if (!prInfo.needsUpdate) {
+ return;
+ }
+
+ const previousMessage = statusMeta.firstChild!; // Extract now because it won't be the first child anymore
+ statusMeta.prepend(getBaseCommitNotice(prInfo));
+ removeTextNodeContaining(previousMessage, 'Merging can be performed automatically.');
+}
+
+async function init(signal: AbortSignal): Promise<false | void> {
+ await api.expectToken();
+
+ observe('.branch-action-item .status-meta', addInfo, {signal});
+}
+
+void features.add(import.meta.url, {
+ include: [
+ pageDetect.isPRConversation,
+ ],
+ exclude: [
+ pageDetect.isClosedPR,
+ () => select('.head-ref')!.title === 'This repository has been deleted',
+ ],
+ awaitDomReady: true, // DOM-based exclusions
+ init,
+});
+
+/*
+Test URLs
+
+PR without conflicts
+https://github.com/refined-github/sandbox/pull/60
+
+Draft PR without conflicts
+https://github.com/refined-github/sandbox/pull/61
+
+Native "Update branch" button
+(pick a conflict-free PR from https://github.com/refined-github/refined-github/pulls?q=is%3Apr+is%3Aopen+sort%3Acreated-asc)
+
+Native "Resolve conflicts" button
+https://github.com/refined-github/sandbox/pull/9
+
+Cross-repo PR with long branch names
+https://github.com/refined-github/sandbox/pull/13
+
+*/