Repository navigation
Expand file tree
/
Copy pathsync-pr-commit-title.tsx
More file actions
139 lines (113 loc) · 3.78 KB
/
Copy pathsync-pr-commit-title.tsx
File metadata and controls
139 lines (113 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import delegate from 'delegate-it';
import React from 'dom-chef';
import * as pageDetect from 'github-url-detection';
import {$, $optional} from 'select-dom';
import features from '../feature-manager.js';
import onCommitTitleUpdate from '../github-events/on-commit-title-update.js';
import api from '../github-helpers/api.js';
import {getConversationNumber, userCanLikelyMergePr} from '../github-helpers/index.js';
import parseRenderedText from '../github-helpers/parse-rendered-text.js';
import {confirmMergeButton} from '../github-helpers/selectors.js';
import cleanPrCommitTitle from '../helpers/pr-commit-cleaner.js';
import observe from '../helpers/selector-observer.js';
import {setReactInputValue} from '../helpers/set-react-text-field-value.js';
const commitTitleFieldSelector = '[data-testid="mergebox-partial"] input[type="text"]';
function getCurrentCommitTitle(): string {
return $(commitTitleFieldSelector).value.trim();
}
export function formatPrCommitTitle(title: string, prNumber = getConversationNumber()!): string {
return `${title} (#${prNumber})`;
}
const prTitleSelector = '[data-component="TitleArea"] h1';
function createCommitTitle(): string {
const prTitle = $([
`${prTitleSelector} .markdown-title`,
'div[class^="prc-PageLayout-Header"] input',
]);
const prTitleText = prTitle instanceof HTMLInputElement ? prTitle.value.trim() : parseRenderedText(prTitle);
return formatPrCommitTitle(prTitleText);
}
function needsSubmission(): boolean {
// `needsSubmission` is also called when the PR title is changed, in order to update the open merge box in real time
if (!/squash/i.test($optional(confirmMergeButton)?.textContent ?? '')) {
return false;
}
const currentCommitTitle = getCurrentCommitTitle();
return Boolean(currentCommitTitle) && (createCommitTitle() !== currentCommitTitle);
}
function getUi(): HTMLElement {
const cancelButton = (
<button type="button" className="btn-link Link--muted text-underline rgh-sync-pr-commit-title">
Cancel
</button>
);
return $optional('.rgh-sync-pr-commit-title-note') ?? (
<p className="note rgh-sync-pr-commit-title-note">
The title of this PR will be updated to match this title. {cancelButton}
</p>
);
}
function updateUi(): void {
if (needsSubmission()) {
$(commitTitleFieldSelector).parentElement!.after(getUi());
} else {
getUi().remove();
}
}
async function updatePrTitle(): Promise<void> {
if (!needsSubmission()) {
return;
}
// Remove PR number from commit title
const title = cleanPrCommitTitle(getCurrentCommitTitle(), getConversationNumber()!);
await api.v3(`pulls/${getConversationNumber()!}`, {
method: 'PATCH',
body: {title},
});
}
async function updateCommitTitle(): Promise<void> {
if (!needsSubmission()) {
return;
}
const field = $(commitTitleFieldSelector);
setReactInputValue(field, createCommitTitle());
}
function disableSubmission(): void {
features.unload(import.meta.url);
getUi().remove();
}
function init(signal: AbortSignal): void {
// PR title -> Commit title field
// On panel open
observe(commitTitleFieldSelector, updateCommitTitle, {signal});
// On PR title change
observe(
prTitleSelector,
updateCommitTitle,
{signal},
);
// Commit title field -> toggle checkbox visibility
onCommitTitleUpdate(updateUi, signal);
// On submission, update PR
delegate(confirmMergeButton, 'click', updatePrTitle, {signal});
// On "Cancel", disable the feature
delegate('.rgh-sync-pr-commit-title', 'click', disableSubmission, {signal});
}
void features.add(import.meta.url, {
asLongAs: [
userCanLikelyMergePr,
],
include: [
pageDetect.isPRConversation,
],
exclude: [
pageDetect.isMergedPR,
],
awaitDomReady: true, // DOM-based filters, feature appears at the end of the page
requiresToken: true,
init,
});
/*
Test URLs:
1. Open any PR in https://github.com/pulls
*/