-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathGithub.ts
More file actions
485 lines (440 loc) · 12.1 KB
/
Github.ts
File metadata and controls
485 lines (440 loc) · 12.1 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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import fs from "fs";
import path from "path";
import retry from "async-retry";
import mime from "mime-types";
import { Octokit } from "@octokit/rest";
import { RequestError } from "@octokit/request-error";
import { getRepoSlugFromManifest } from "../../files/index.js";
export class Github {
private octokit: Octokit;
private owner: string;
private repo: string;
constructor({ owner, repo }: { owner: string; repo: string }) {
this.owner = owner;
this.repo = repo;
// OAuth2 token from Github
if (!process.env.GITHUB_TOKEN)
throw Error("GITHUB_TOKEN ENV (OAuth2) is required");
this.octokit = new Octokit({
auth: `token ${process.env.GITHUB_TOKEN}`
});
}
static fromLocal(dir: string): Github {
const repoSlug =
process.env.GITHUB_REPOSITORY ||
process.env.TRAVIS_REPO_SLUG ||
getRepoSlugFromManifest({ dir });
if (!repoSlug)
throw Error(
"manifest.repository must be properly defined to create a Github release"
);
const [owner, repo] = repoSlug.split("/");
if (!owner) throw Error(`repoSlug "${repoSlug}" hasn't an owner`);
if (!repo) throw Error(`repoSlug "${repoSlug}" hasn't a repo`);
return new Github({ owner, repo });
}
get repoSlug(): string {
return `${this.owner}/${this.repo}`;
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async getRepo() {
try {
const repoResponse = await this.octokit.rest.repos.get({
owner: this.owner,
repo: this.repo,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
}
});
return repoResponse.data;
} catch (e) {
if (e instanceof RequestError && e.status === 404) {
throw new Error(`Repo does not exist: ${this.repoSlug}`);
}
e.message = `Error verifying repo ${this.repoSlug}: ${e.message}`;
throw e;
}
}
async assertRepoExists(): Promise<void> {
await this.getRepo();
}
/**
* Deletes a tag only if exists, does not throw on 404
* @param tag "v0.2.0", "release/patch"
*/
async deleteTagIfExists(tag: string): Promise<void> {
if (!(await this.tagExists(tag))) return;
await this.octokit.rest.git.deleteRef({
owner: this.owner,
repo: this.repo,
ref: `tags/${tag}`,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
}
});
}
private async tagExists(tag: string): Promise<boolean> {
try {
const matchingTags = await this.octokit.rest.git.listMatchingRefs({
owner: this.owner,
repo: this.repo,
ref: `tags/${tag}`,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
}
});
if (matchingTags.data.length > 0) return true;
return false;
} catch (e) {
e.message = `Error verifying tag ${tag}: ${e.message}`;
throw e;
}
}
/**
* Creates a Github tag at a given commit sha
* @param tag "v0.2.0"
* @param sha "ffac537e6cbbf934b08745a378932722df287a53"
*/
async createTag(tag: string, sha: string): Promise<void> {
try {
await this.octokit.rest.git.createRef({
owner: this.owner,
repo: this.repo,
ref: `refs/tags/${tag}`,
sha,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
}
});
} catch (e) {
e.message = `Error creating tag ${tag} at ${sha}: ${e.message}`;
throw e;
}
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async listReleases() {
return await this.octokit.rest.repos
.listReleases({
owner: this.owner,
repo: this.repo,
per_page: 100,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
}
})
.then(res => res.data);
}
/**
* Removes all Github releases that match a tag, and its assets
* If there are no releases, repos.listReleases will return []
* @param tag "v0.2.0"
*/
async deleteReleaseAndAssets(tag: string): Promise<void> {
const releases = await this.listReleases();
const matchingReleases = releases.filter(
({ tag_name, name }) => tag_name === tag || name === tag
);
for (const matchingRelease of matchingReleases) {
for (const asset of matchingRelease.assets)
try {
await this.octokit.rest.repos.deleteReleaseAsset({
owner: this.owner,
repo: this.repo,
asset_id: asset.id,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
}
});
} catch (e) {
e.message = `Error deleting release asset: ${e.message}`;
throw e;
}
try {
await this.octokit.rest.repos.deleteRelease({
owner: this.owner,
repo: this.repo,
release_id: matchingRelease.id,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
}
});
} catch (e) {
e.message = `Error deleting release: ${e.message}`;
throw e;
}
}
}
/**
* Create a Github release and return the release id
* @param tag "v0.2.0"
* @param options
*/
async createRelease(
tag: string,
options?: {
body?: string;
prerelease?: boolean;
}
): Promise<number> {
const { body, prerelease } = options || {};
const release = await this.octokit.rest.repos
.createRelease({
owner: this.owner,
repo: this.repo,
tag_name: tag,
name: this.buildReleaseNameFromTag(tag),
body,
prerelease,
generate_release_notes: true,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
}
})
.catch(e => {
e.message = `Error creating release: ${e.message}`;
throw e;
});
return release.data.id;
}
async uploadReleaseAssets({
releaseId,
assetsDir,
matchPattern,
fileNamePrefix
}: {
releaseId: number;
assetsDir: string;
matchPattern?: RegExp;
fileNamePrefix?: string;
}) {
for (const file of fs.readdirSync(assetsDir)) {
// Used to ignore duplicated legacy .tar.xz image
if (matchPattern && !matchPattern.test(file)) continue;
const filepath = path.resolve(assetsDir, file);
const contentType = mime.lookup(filepath) || "application/octet-stream";
try {
// The uploadReleaseAssetApi fails sometimes, retry 3 times
await retry(
async () => {
await this.octokit.repos.uploadReleaseAsset({
owner: this.owner,
repo: this.repo,
release_id: releaseId,
data: fs.createReadStream(filepath) as any,
headers: {
"content-type": contentType,
"content-length": fs.statSync(filepath).size
},
name: `${fileNamePrefix || ""}${path.basename(filepath)}`
});
},
{ retries: 3 }
);
} catch (e) {
e.message = `Error uploading release asset: ${e.message}`;
throw e;
}
}
}
/**
* Receives a tag and returns a prettified release name
*
* For single-variant packages:
* Tag: "v0.2.0" => Release name: "v0.2.0"
*
* For multi-variant packages:
* Tag: "gnosis@v0.1.2_holesky@v1.2.3_mainnet@v3.21.1" => Release name: "Gnosis(v0.1.2), Holesky(v1.2.3), Mainnet(v3.21.1)"
*
* @param tag
*/
private buildReleaseNameFromTag(tag: string): string {
const variants = tag.split("_").map(variant => {
const [name, version] = variant.split("@");
// If the variant is a single-variant package
if (!version) return name;
return `${name}(${version})`;
});
return variants.join(", ");
}
/**
* Open a Github pull request
*/
async openPR({
from,
to,
title,
body
}: {
from: string;
to: string;
title: string;
body?: string;
}): Promise<void> {
await this.octokit.rest.pulls.create({
owner: this.owner,
repo: this.repo,
title,
body,
head: from,
base: to,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
}
});
}
/**
* In a Github Actions context, get the pull request number of this run
*/
getPullRequestNumber(): number {
if (!process.env.GITHUB_EVENT_PATH) {
throw Error("ENV GITHUB_EVENT_PATH is not defined");
}
const eventData = JSON.parse(
fs.readFileSync(process.env.GITHUB_EVENT_PATH, "utf8")
);
return eventData.pull_request.number;
}
/**
* Create or update a comment in a Github PR according to `isTargetComment()`
* @param number Pull Request number #45
*/
async commentToPr({
number,
body,
isTargetComment
}: {
number: number;
body: string;
isTargetComment: (commentBody: string) => boolean;
}): Promise<void> {
const comments = await this.octokit.rest.issues.listComments({
owner: this.owner,
repo: this.repo,
issue_number: number,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
}
});
const existingComment = comments.data.find(
comment => comment.body && isTargetComment(comment.body)
);
if (existingComment) {
await this.octokit.rest.issues.updateComment({
owner: this.owner,
repo: this.repo,
comment_id: existingComment.id,
body,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
}
});
} else {
this.createCommentInPr({ number, body });
}
}
/**
* Comment a Pull Request
*/
async createCommentInPr({
number,
body
}: {
number: number;
body: string;
}): Promise<void> {
await this.octokit.rest.issues.createComment({
owner: this.owner,
repo: this.repo,
issue_number: number,
body,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
}
});
}
/**
* Returns open PRs where head branch equals `branch`
* Only branches and PRs originating from the same repo
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async getOpenPrsFromBranch(branch: string) {
const openPrsResponse = await this.octokit.rest.pulls.list({
owner: this.owner,
repo: this.repo,
state: "open",
headers: {
"X-GitHub-Api-Version": "2022-11-28"
},
head: `${this.owner}:${branch}`
});
return openPrsResponse.data;
}
/**
* Returns list of branches, limited to 100. For more implement pagination
* @returns branch = {
* name: "octocat-patch-1"
* commit: {
* sha: "b1b3f9723831141a31a1a7252a213e216ea76e56"
* }
* }
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async listBranches() {
const branchesResponse = await this.octokit.rest.repos.listBranches({
owner: this.owner,
repo: this.repo,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
}
});
return branchesResponse.data;
}
/**
* @param branch "octocat-patch-1"
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async getBranch(branch: string) {
const branchResponse = await this.octokit.rest.repos.getBranch({
owner: this.owner,
repo: this.repo,
branch,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
}
});
return branchResponse.data;
}
/**
* Returns true if the branch exists, false if it does not, error otherwise
* @param branch "octocat-patch-1"
*/
async branchExists(branch: string): Promise<boolean> {
try {
const data = await this.getBranch(branch);
return Boolean(data);
} catch (e) {
if (e instanceof RequestError && e.status === 404) return false;
e.message = `Error verifying branch ${branch}: ${e.message}`;
throw e;
}
}
/**
* Close a PR
*/
async closePR(pull_number: number): Promise<void> {
try {
await this.octokit.rest.pulls.update({
owner: this.owner,
repo: this.repo,
pull_number,
state: "closed",
headers: {
"X-GitHub-Api-Version": "2022-11-28"
}
});
} catch (e) {
e.message = `Error closing PR: ${e.message}`;
throw e;
}
}
}