-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
update-pr-from-base-branch.tsx
97 lines (82 loc) · 2.61 KB
/
update-pr-from-base-branch.tsx
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
import React from 'dom-chef';
import select from 'select-dom';
import onetime from 'onetime';
import delegate from 'delegate-it';
import {AlertIcon} from '@primer/octicons-react';
import * as pageDetect from 'github-url-detection';
import {observe, Observer} from 'selector-observer';
import features from '.';
import * as api from '../github-helpers/api';
import {getConversationNumber} from '../github-helpers';
const selectorForPushablePRNotice = '.merge-pr > .text-gray:first-child:not(.rgh-update-pr)';
let observer: Observer;
function getBranches(): {base: string; head: string} {
return {
base: select('.base-ref')!.textContent!.trim(),
head: select('.head-ref')!.textContent!.trim()
};
}
async function mergeBranches(): Promise<AnyObject> {
return api.v3(`pulls/${getConversationNumber()!}/update-branch`, {
method: 'PUT',
headers: {
Accept: 'application/vnd.github.lydian-preview+json'
},
ignoreHTTPStatus: true
});
}
async function handler({delegateTarget}: delegate.Event): Promise<void> {
const {base, head} = getBranches();
if (!confirm(`Merge the ${base} branch into ${head}?`)) {
return;
}
const statusMeta = delegateTarget.parentElement!;
statusMeta.textContent = 'Updating branch…';
observer.abort();
const response = await mergeBranches();
if (response.ok) {
statusMeta.remove();
} else {
statusMeta.textContent = response.message ?? 'Error';
statusMeta.prepend(<AlertIcon/>, ' ');
throw new api.RefinedGitHubAPIError('update-pr-from-base-branch: ' + JSON.stringify(response));
}
}
async function addButton(position: Element): Promise<void> {
const {base, head} = getBranches();
const {status} = await api.v3(`compare/${base}...${head}`);
if (status === 'diverged') {
position.append(' ', (
<span className="status-meta d-inline-block rgh-update-pr-from-base-branch">
You can <button type="button" className="btn-link">update the base branch</button>.
</span>
));
}
}
const waitForText = onetime(() => {
delegate(document, '.rgh-update-pr-from-base-branch', 'click', handler);
observer = observe(selectorForPushablePRNotice, {
add(position) {
position.classList.add('rgh-update-pr');
void addButton(position);
}
});
});
async function init(): Promise<void | false> {
await api.expectToken();
// "Resolve conflicts" is the native button to update the PR
if (select.exists('.js-merge-pr a[href$="/conflicts"]')) {
return false;
}
// Quick check before using selector-observer on it
if (!select.exists(selectorForPushablePRNotice)) {
return false;
}
waitForText();
}
void features.add(__filebasename, {
include: [
pageDetect.isPRConversation
],
init
});