-
Notifications
You must be signed in to change notification settings - Fork 323
/
github.ts
461 lines (426 loc) · 13.8 KB
/
github.ts
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/**
* Copyright 2019 Google LLC. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as Octokit from '@octokit/rest';
import {IssuesListResponseItem, PullsCreateResponse, PullsListResponseItem, ReposGetLatestReleaseResponse, ReposListTagsResponseItem, Response} from '@octokit/rest';
import chalk from 'chalk';
import * as semver from 'semver';
import {checkpoint, CheckpointType} from './checkpoint';
import {Update} from './updaters/update';
const VERSION_FROM_BRANCH_RE = /^.*:[^-]+-(.*)$/;
interface GitHubOptions {
token?: string;
owner: string;
repo: string;
}
export interface GitHubTag {
name: string;
sha: string;
version: string;
}
export interface GitHubReleasePR {
number: number;
version: string;
sha: string;
}
interface GitHubPR {
branch: string;
version: string;
title: string;
body: string;
sha: string;
updates: Update[];
}
export class GitHub {
octokit: Octokit;
token: string|undefined;
owner: string;
repo: string;
constructor(options: GitHubOptions) {
this.token = options.token;
this.owner = options.owner;
this.repo = options.repo;
this.octokit = new Octokit({auth: this.token});
}
async commitsSinceSha(sha: string|undefined, perPage = 100):
Promise<string[]> {
const commits = [];
for await (const response of this.octokit.paginate.iterator({
method: 'GET',
url: `/repos/${this.owner}/${this.repo}/commits?per_page=${perPage}`
})) {
for (let i = 0, commit; response.data[i] !== undefined; i++) {
commit = response.data[i];
if (commit.sha === sha) {
return commits;
} else {
// conventional commits parser expects:
// [commit message]
// -hash-
// [commit sha]
commits.push(`${commit.commit.message}\n-hash-\n${commit.sha}`);
}
}
}
return commits;
}
async latestTag(perPage = 100): Promise<GitHubTag|undefined> {
const tags: {[version: string]: GitHubTag;} = await this.allTags(perPage);
const versions = Object.keys(tags);
// no tags have been created yet.
if (versions.length === 0) return undefined;
versions.sort(semver.rcompare);
return {
name: tags[versions[0]].name,
sha: tags[versions[0]].sha,
version: tags[versions[0]].version
};
}
// TODO: investigate why this returns a target_commitish of `master`
// even months after the release is created; is there a way to
// get the SHA that the release was created at?
async latestRelease(): Promise<GitHubTag|undefined> {
try {
const latestRelease: Response<ReposGetLatestReleaseResponse> =
await this.octokit.repos.getLatestRelease(
{owner: this.owner, repo: this.repo});
const version = semver.valid(latestRelease.data.name);
if (version) {
console.info(latestRelease.data);
return {
name: latestRelease.data.name,
sha: latestRelease.data.target_commitish,
version
};
}
} catch (err) {
if (err.status === 404) {
// fallback to tags if we don't find a GitHub release.
} else {
throw err;
}
}
return undefined;
}
async findMergedReleasePR(labels: string[], perPage = 25):
Promise<GitHubReleasePR|undefined> {
const pullsResponse: Response<PullsListResponseItem[]> =
await this.octokit.pulls.list({
owner: this.owner,
repo: this.repo,
state: 'closed',
per_page: perPage
});
for (let i = 0, pull; i < pullsResponse.data.length; i++) {
pull = pullsResponse.data[i];
if (this.hasAllLabels(labels, pull.labels.map(l => l.name))) {
// it's expected that a release PR will have a
// HEAD matching the format repo:release-v1.0.0.
if (!pull.head) continue;
const match = pull.head.label.match(VERSION_FROM_BRANCH_RE);
if (!match || !pull.merged_at) continue;
return {
number: pull.number,
sha: pull.merge_commit_sha,
version: match[1]
} as GitHubReleasePR;
}
}
return undefined;
}
private hasAllLabels(labelsA: string[], labelsB: string[]) {
let hasAll = true;
labelsA.forEach((label) => {
if (labelsB.indexOf(label) === -1) hasAll = false;
});
return hasAll;
}
async findOpenReleasePRs(labels: string[], perPage = 25):
Promise<PullsListResponseItem[]> {
const openReleasePRs: PullsListResponseItem[] = [];
const pullsResponse: Response<PullsListResponseItem[]> =
await this.octokit.pulls.list({
owner: this.owner,
repo: this.repo,
state: 'open',
per_page: perPage
});
for (let i = 0, pull; i < pullsResponse.data.length; i++) {
pull = pullsResponse.data[i];
for (let ii = 0, label; ii < pull.labels.length; ii++) {
label = pull.labels[ii];
if (labels.indexOf(label.name) !== -1) {
openReleasePRs.push(pull);
}
}
}
return openReleasePRs;
}
private async allTags(perPage = 100):
Promise<{[version: string]: GitHubTag;}> {
const tags: {[version: string]: GitHubTag;} = {};
for await (const response of this.octokit.paginate.iterator({
method: 'GET',
url: `/repos/${this.owner}/${this.repo}/tags?per_page=${perPage}`
})) {
response.data.forEach((data: ReposListTagsResponseItem) => {
const version = semver.valid(data.name);
if (version) {
tags[version] = {sha: data.commit.sha, name: data.name, version};
}
});
}
return tags;
}
async addLabels(pr: number, labels: string[]) {
checkpoint(
`adding label ${chalk.green(labels.join(','))} to https://github.com/${
this.owner}/${this.repo}/pull/${pr}`,
CheckpointType.Success);
await this.octokit.issues.addLabels(
{owner: this.owner, repo: this.repo, issue_number: pr, labels});
}
async openIssue(
title: string, body: string, labels: string[],
issue?: IssuesListResponseItem) {
if (issue) {
checkpoint(
`updating issue #${issue.number} with labels = ${
JSON.stringify(labels)}`,
CheckpointType.Success);
this.octokit.issues.update({
owner: this.owner,
repo: this.repo,
body,
issue_number: issue.number,
labels
});
} else {
checkpoint(
`creating new release proposal issue with labels = ${
JSON.stringify(labels)}`,
CheckpointType.Success);
this.octokit.issues.create(
{owner: this.owner, repo: this.repo, title, body, labels});
}
}
async findExistingReleaseIssue(
title: string, labels: string[],
perPage = 100): Promise<IssuesListResponseItem|undefined> {
const paged = 0;
try {
for await (const response of this.octokit.paginate.iterator({
method: 'GET',
url: `/repos/${this.owner}/${this.repo}/issues?per_page=${
perPage}&labels=${labels.join(',')}`
})) {
for (let i = 0, issue; response.data[i] !== undefined; i++) {
const issue: IssuesListResponseItem = response.data[i];
if (issue.title.indexOf(title) !== -1 && issue.state === 'open') {
return issue;
}
}
}
} catch (err) {
if (err.status === 404) {
// the most likely cause of a 404 during this step is actually
// that the user does not have access to the repo:
throw new AuthError();
} else {
throw err;
}
}
return undefined;
}
async openPR(options: GitHubPR): Promise<number> {
let refName = await this.refByBranchName(options.branch);
// If the branch exists, we delete it and create a new branch
// with the same name; this results in the existing PR being closed.
if (!refName) {
refName = `refs/heads/${options.branch}`;
} else {
try {
checkpoint(
`branch ${chalk.red(options.branch)} already exists`,
CheckpointType.Failure);
await this.octokit.git.deleteRef({
owner: this.owner,
repo: this.repo,
// DELETE requires that the refs/ prefix used during
// creation be removed; this seems like a bug we should log.
ref: refName.replace('refs/', '')
});
checkpoint(
`branch ${chalk.red(options.branch)} deleted`,
CheckpointType.Success);
} catch (err) {
if (err.status === 404) {
// the most likely cause of a 404 during this step is actually
// that the user does not have access to the repo:
throw new AuthError();
} else {
throw err;
}
}
}
// always create the branch (it was potentially deleted in the prior step).
try {
checkpoint(
`creating branch ${chalk.green(options.branch)}`,
CheckpointType.Success);
await this.octokit.git.createRef(
{owner: this.owner, repo: this.repo, ref: refName, sha: options.sha});
} catch (err) {
if (err.status === 404) {
// the most likely cause of a 404 during this step is actually
// that the user does not have access to the repo:
throw new AuthError();
} else {
throw err;
}
}
await this.updateFiles(options.updates, options.branch, refName);
checkpoint(
`open pull-request: ${chalk.yellow(options.title)}`,
CheckpointType.Success);
const resp: Response<PullsCreateResponse> =
await this.octokit.pulls.create({
owner: this.owner,
repo: this.repo,
title: options.title,
body: options.body,
head: options.branch,
base: 'master'
});
return resp.data.number;
}
async updateFiles(updates: Update[], branch: string, refName: string) {
for (let i = 0; i < updates.length; i++) {
const update = updates[i];
let content;
try {
content = await this.octokit.repos.getContents({
owner: this.owner,
repo: this.repo,
path: update.path,
ref: refName
});
} catch (err) {
if (err.status !== 404) throw err;
// if the file is missing and create = false, just continue
// to the next update, otherwise create the file.
if (!update.create) {
checkpoint(
`file ${chalk.green(update.path)} did not exist`,
CheckpointType.Failure);
continue;
}
}
const contentText = content ?
Buffer.from(content.data.content, 'base64').toString('utf8') :
undefined;
const updatedContent = update.updateContent(contentText);
if (content) {
await this.octokit.repos.updateFile({
owner: this.owner,
repo: this.repo,
path: update.path,
message: `updated ${update.path}`,
content: Buffer.from(updatedContent, 'utf8').toString('base64'),
sha: content.data.sha,
branch
});
} else {
await this.octokit.repos.createFile({
owner: this.owner,
repo: this.repo,
path: update.path,
message: `created ${update.path}`,
content: Buffer.from(updatedContent, 'utf8').toString('base64'),
branch
});
}
}
}
private async refByBranchName(branch: string): Promise<string|undefined> {
let ref;
try {
for await (const response of this.octokit.paginate.iterator(
{method: 'GET', url: `/repos/${this.owner}/${this.repo}/git/refs`})) {
for (let i = 0, r; response.data[i] !== undefined; i++) {
r = response.data[i];
const refRe = new RegExp(`/${branch}$`);
if (r.ref.match(refRe)) {
ref = r.ref;
}
}
}
} catch (err) {
if (err.status === 404) {
// the most likely cause of a 404 during this step is actually
// that the user does not have access to the repo:
throw new AuthError();
} else {
throw err;
}
}
return ref;
}
async closePR(prNumber: number) {
this.octokit.pulls.update({
owner: this.owner,
repo: this.repo,
number: prNumber,
state: 'closed'
});
}
async getFileContents(path: string): Promise<string> {
const content = await this.octokit.repos.getContents(
{owner: this.owner, repo: this.repo, path});
return Buffer.from(content.data.content, 'base64').toString('utf8');
}
async createRelease(version: string, sha: string, releaseNotes: string) {
checkpoint(`creating release ${version}`, CheckpointType.Success);
await this.octokit.repos.createRelease({
owner: this.owner,
repo: this.repo,
tag_name: version,
target_commitish: sha,
body: releaseNotes,
name: version
});
}
async removeLabels(labels: string[], prNumber: number) {
await labels.forEach(async (label) => {
checkpoint(
`removing label ${chalk.green(label)} from ${
chalk.green('' + prNumber)}`,
CheckpointType.Success);
await this.octokit.issues.removeLabel({
owner: this.owner,
repo: this.repo,
issue_number: prNumber,
name: label
});
});
}
}
class AuthError extends Error {
status: number;
constructor() {
super('unauthorized');
this.status = 401;
}
}