forked from realyze/pr-train
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.js
296 lines (273 loc) · 8.61 KB
/
github.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// @ts-check
const octo = require('octonode');
const promptly = require('promptly');
const {
DEFAULT_REMOTE,
DEFAULT_BASE_BRANCH
} = require('./consts');
const fs = require('fs');
const get = require('lodash/get');
const colors = require('colors');
const emoji = require('node-emoji');
const width = require('string-width');
const { exit } = require('process');
/**
*
* @param {simpleGit.SimpleGit} sg
* @param {string} branch
* @return Promise.<{title: string, body: string}>
*/
async function constructPrMsg(sg, branch) {
const title = await sg.raw(['log', '--format=%s', '-n', '1', branch]);
const body = await sg.raw(['log', '--format=%b', '-n', '1', branch]);
return {
title: title.trim(),
body: body.trim(),
};
}
/**
*
* @param {Object.<string, {title: string, pr: number}>} branchToPrDict
* @param {string} currentBranch
* @param {string} combinedBranch
*/
function constructTrainNavigation(branchToPrDict, currentBranch, combinedBranch) {
let contents = '<pr-train-toc>\n\n';
let prList = [];
Object.keys(branchToPrDict).forEach((branch) => {
const maybeHandRight = branch === currentBranch ? ' 👈' : ' ';
const combinedInfo = branch === combinedBranch ? ' **[combined branch]** ' : ' ';
const prNumber = `#${branchToPrDict[branch].pr}`;
const prInfoHtml = `1. ${prNumber}${combinedInfo}${maybeHandRight}`;
prList.push(prInfoHtml);
});
contents += '### PR Train\n';
contents += prList.join('\n');
contents += '\n</pr-train-toc>'
return contents;
}
/**
*
* @param {string} context
*/
function constructContextSection(context) {
let contents = '<pr-train-context>\n\n';
contents += '### Context\n';
contents += context + "\n";
contents += '</pr-train-context>\n\n';
return contents;
}
function checkGHKeyExists() {
try {
readGHKey()
} catch (e) {
console.log(`"$HOME/.pr-train" not found. Please make sure file exists and contains your GitHub API key.`.red);
process.exit(4);
}
}
function readGHKey() {
return fs
.readFileSync(`${process.env.HOME}/.pr-train`, 'UTF-8')
.toString()
.trim();
}
/**
*
* @param {string} newNavigation
* @param {string} body
*/
function upsertNavigationInBody(newNavigation, body) {
body = body || '';
if (body.match(/<pr-train-toc>/)) {
return body.replace(/<pr-train-toc>[^]*<\/pr-train-toc>/, newNavigation);
} else {
return (body ? body + '\n' : '') + newNavigation;
}
}
/**
* @param {string} contextSection
* @param {string} body
*/
function upsertContextSectionInBody(contextSection, body) {
body = body || '';
if (body.match(/<pr-train-context>/)) {
return body.replace(/<pr-train-context>[^]*<\/pr-train-context>/, contextSection);
} else {
return contextSection + (body ? body + '\n' : '');
}
}
function checkAndReportInvalidBaseError(e, base) {
const { field, code } = get(e, 'body.errors[0]', {});
if (field === 'base' && code === 'invalid') {
console.log([
emoji.get('no_entry'),
`\n${emoji.get('confounded')} This is embarrassing. `,
`The base branch of ${base.bold} doesn't seem to exist on the remote.`,
`\nDid you forget to ${emoji.get('arrow_up')} push it?`,
].join(''));
return true;
}
return false;
}
/**
*
* @param {simpleGit.SimpleGit} sg
* @param {Array.<string>} allBranches
* @param {string} combinedBranch
* @param {boolean} draft
* @param {string} remote
* @param {string} baseBranch
* @param {boolean} printLinks
*/
async function ensurePrsExist({
sg,
allBranches,
combinedBranch,
draft,
remote = DEFAULT_REMOTE,
baseBranch = DEFAULT_BASE_BRANCH,
printLinks = false,
context = ''
}) {
//const allBranches = combinedBranch ? sortedBranches.concat(combinedBranch) : sortedBranches;
const octoClient = octo.client(readGHKey());
// TODO: take remote name from `-r` value.
const nickAndRepo = await getRepoName({sg, remote})
/** @type string */
let combinedBranchTitle;
if (combinedBranch) {
console.log();
console.log(`Now I will need to know what to call your "combined" branch PR in GitHub.`);
combinedBranchTitle = await promptly.prompt(colors.bold(`Combined branch PR title:`));
if (!combinedBranchTitle) {
console.log(`Cannot continue.`.red, `(I need to know what the title of your combined branch PR should be.)`);
process.exit(5);
}
}
const getCombinedBranchPrMsg = () => ({
title: combinedBranchTitle,
body: '',
});
const prText = draft ? 'draft PR' : 'PR';
console.log();
console.log(`This will create (or update) ${prText}s for the following branches:`);
await allBranches.reduce(async (memo, branch) => {
await memo;
const {
title
} = branch === combinedBranch ? getCombinedBranchPrMsg() : await constructPrMsg(sg, branch);
console.log(` -> ${branch.green} (${title.italic})`);
}, Promise.resolve());
console.log();
if (!(await promptly.confirm(colors.bold('Shall we do this? [y/n] ')))) {
console.log('No worries. Bye now.', emoji.get('wave'));
process.exit(0);
}
const nick = nickAndRepo.split('/')[0];
const ghRepo = octoClient.repo(nickAndRepo);
console.log('');
// Construct branch_name <-> PR_data mapping.
// Note: We're running this serially to have nicer logs.
/**
* @type Object.<string, {title: string, pr: number, body: string, updating: boolean}>
*/
const prDict = await allBranches.reduce(async (_memo, branch, index) => {
const memo = await _memo;
const {
title,
body
} = branch === combinedBranch ? getCombinedBranchPrMsg() : await constructPrMsg(sg, branch);
const base = index === 0 || branch === combinedBranch ? baseBranch : allBranches[index - 1];
process.stdout.write(`Checking if PR for branch ${branch} already exists... `);
const prs = await ghRepo.prsAsync({
state: 'all',
sort: 'created',
direction: 'desc',
head: `${nick}:${branch}`,
});
const prsBody = prs[0];
// Use the oldest one for now
let prResponse = prsBody && prsBody[prsBody.length - 1];
let prExists = false;
if (prResponse) {
console.log('yep');
prExists = true;
} else {
console.log('nope');
const payload = {
head: branch,
base,
title,
body,
draft,
};
const baseMessage = base === baseBranch ? colors.dim(` (against ${base})`) : '';
process.stdout.write(`Creating ${prText} for branch "${branch}"${baseMessage}...`);
try {
prResponse = (await ghRepo.prAsync(payload))[0];
} catch (e) {
if (!checkAndReportInvalidBaseError(e, base)) {
console.error(JSON.stringify(e, null, 2));
}
throw e;
}
console.log(emoji.get('white_check_mark'));
}
memo[branch] = {
body: prResponse.body,
title: prResponse.title,
pr: prResponse.number,
updating: prExists,
};
return memo;
}, Promise.resolve({}));
// Now that we have all the PRs, let's update them with the "navigation" section.
// Note: We're running this serially to have nicer logs.
await allBranches.reduce(async (memo, branch) => {
await memo;
const prInfo = prDict[branch];
const ghPr = octoClient.pr(nickAndRepo, prInfo.pr);
const {
title,
body
} = prInfo.updating ?
prInfo // Updating existing PR: keep current body and title.
:
branch === combinedBranch ?
getCombinedBranchPrMsg() :
await constructPrMsg(sg, branch);
const navigation = constructTrainNavigation(prDict, branch, combinedBranch);
let newBody = upsertNavigationInBody(navigation, body);
if (context) {
const contextSection = constructContextSection(context);
newBody = upsertContextSectionInBody(contextSection, newBody);
}
process.stdout.write(`Updating PR for branch ${branch}...`);
const updateResponse = await ghPr.updateAsync({
title,
body: `${newBody}`,
});
const prLink = get(updateResponse, '0._links.html.href', colors.yellow('Could not get URL'));
console.log(emoji.get('white_check_mark') + (printLinks ? ` (${prLink})` : ''));
}, Promise.resolve());
return prDict;
}
async function getRepoName({sg, remote = DEFAULT_REMOTE}) {
const remoteUrl = await sg.raw(['config', '--get', `remote.${remote}.url`]);
if (!remoteUrl) {
console.log(`URL for remote ${remote} not found in your git config`.red);
process.exit(4);
}
const nickAndRepo = remoteUrl.match(/github\.com[/:](.*)/)[1].replace(/\.git$/, '');
if (!nickAndRepo) {
console.log(`I could not parse your remote ${remote} repo URL`.red);
process.exit(4);
}
return nickAndRepo;
}
module.exports = {
ensurePrsExist,
readGHKey,
checkGHKeyExists,
getRepoName,
};