-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.ts
546 lines (479 loc) · 17.4 KB
/
bot.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/* eslint-disable @typescript-eslint/naming-convention */
import type { EmitterWebhookEventName } from '@octokit/webhooks';
import type { IZipEntry } from 'adm-zip';
import type { Context } from 'probot';
import {
BOT_CONFIGS_FILE_NAME,
DEFAULT_BOT_CONFIGS,
DEFAULT_MAIN_BRANCH,
DEPRECATED_BOT_CONFIGS_FILE_NAME,
GITHUB_CDN_DOMAIN,
GithubFileMode,
IMAGES_STORAGE_FOLDER,
STORAGE_BRANCH,
TEST_REPORT_HIDDEN_LABEL,
} from '../constants';
import {
getWorkflowHeadSha,
getWorkflowPrNumbers,
isWorkflowContext,
} from '../selectors';
import type { IBotConfigs } from '../types';
import {
checkContainsHiddenLabel,
findNewScreenshotImages,
findScreenshotDiffImages,
markCommentWithHiddenLabel,
parseTomlFileBase64Str,
} from '../utils';
export abstract class Bot<T extends EmitterWebhookEventName> {
constructor(protected context: Context<T>) {}
/**
* Send comment to the issue
* (pull request is the same issue but with code).
* ___
* This method uses github api endpoint:
* {@link https://docs.github.com/en/rest/reference/issues#create-an-issue-comment Create an issue comment}.
*
* GitHub App must have the **issues:write**
* (or **pull_requests:write** if you are working only with PRs) permission to use this endpoints.
*
* @param issueNumber number
* @param markdownText string (optionally, can include markdown syntax)
*/
async sendComment(issueNumber: number, markdownText: string) {
const comment = this.context.repo({
body: markdownText,
issue_number: issueNumber,
});
return this.context.octokit.issues.createComment(comment);
}
/**
* Update certain comment in the issue (pull request is the same issue but with code).
* ___
* This method uses github api endpoint
* {@link https://docs.github.com/en/rest/reference/issues#update-an-issue-comment Update an issue comment}.
*
* GitHub App must have the **issues:write**
* (or **pull_requests:write** if you are working only with PRs) permission to use this endpoints.
*
* @param commentId number
* @param newMarkdownContent string (optionally, can include markdown syntax)
*/
async updateComment(commentId: number, newMarkdownContent: string) {
return this.context.octokit.rest.issues.updateComment({
...this.context.repo(),
comment_id: commentId,
body: newMarkdownContent,
});
}
/**
* Get info about all comments in the current issue/PR.
* ___
* This method uses github api endpoint
* {@link https://docs.github.com/en/rest/reference/issues#list-issue-comments List issue comments}.
*
* GitHub App must have the **issues:read**
* (or **pull_requests:read** if you are working only with PRs) permission to use this endpoints.
*/
async getCommentsByIssueId(issueNumber: number) {
return this.context.octokit.rest.issues
.listComments({
...this.context.repo(),
issue_number: issueNumber,
})
.then(({ data }) => data);
}
/**
* Download artifacts (zip files) in the workflow and unpack them.
* ___
* This method uses two github api endpoints:
* - {@link https://docs.github.com/en/rest/reference/actions#list-workflow-run-artifacts List workflow run artifacts}
* - {@link https://docs.github.com/en/rest/reference/actions#download-an-artifact Download an artifact}
*
* GitHub App must have the **actions:read** permission to use these endpoints.
*/
async getWorkflowArtifacts<F>(workflowRunId: number): Promise<F[]> {
const workflowRunInfo = this.context.repo({
run_id: workflowRunId,
});
const artifactsInfo = await this.context.octokit.actions
.listWorkflowRunArtifacts(workflowRunInfo)
.catch(() => null);
const artifacts = (artifactsInfo && artifactsInfo.data.artifacts) || [];
if (artifacts.length) {
const artifactsMetas = artifacts.map(({ id }) =>
this.context.repo({ artifact_id: id, archive_format: 'zip' })
);
const artifactsRequests = artifactsMetas.map((meta) =>
this.context.octokit.actions
.downloadArtifact(meta)
.then(({ data }) => data as F)
);
return Promise.all(artifactsRequests);
}
return [];
}
/**
* Get file (+ meta info about it) by its path in the repository.
* ___
* This method uses github api endpoint
* {@link https://docs.github.com/en/rest/reference/repos#get-repository-content Get repository content}.
*
* GitHub App must have the **contents:read** (or **single_file:read** to required files) permission to use this endpoints.
*
* @param path file location (from root of repo)
* @param branch target branch
* (if branch param is not provided it takes the repository’s default branch (usually master/main))
*/
async getFile(path: string, branch?: string) {
return this.context.octokit.repos
.getContent({
...this.context.repo(),
path,
ref: branch,
})
.catch(() => null);
}
/**
* Get info about git branch by its name.
* ___
* This method uses github api endpoint
* {@link https://docs.github.com/en/rest/reference/repos#get-a-branch Get a branch}.
*
* GitHub App must have the **contents:read** permission to use this endpoints.
*/
async getBranchInfo(branch: string) {
return this.context.octokit.rest.repos
.getBranch({ ...this.context.repo(), branch })
.catch(() => null);
}
/**
* Create git branch in current repository (do nothing if branch already exists).
* ___
* This method uses github api endpoint
* {@link https://docs.github.com/en/rest/reference/git#create-a-reference Create a reference}.
*
* GitHub App must have the **contents:write** permission to use this endpoints.
*
* @param branch new branch name
* @param fromBranch from which to create new branch
* (if branch param is not provided it tries to parse repository’s default branch or use {@link DEFAULT_MAIN_BRANCH})
*/
async createBranch(branch: string, fromBranch?: string) {
if (await this.getBranchInfo(branch)) {
return;
}
const currentRepoDefaultBranch =
'repository' in this.context.payload
? this.context.payload.repository?.default_branch
: '';
const fromBranchInfo = await this.getBranchInfo(
fromBranch || currentRepoDefaultBranch || DEFAULT_MAIN_BRANCH
);
if (!fromBranchInfo) {
return;
}
return this.context.octokit.rest.git.createRef({
...this.context.repo(),
ref: `refs/heads/${branch}`,
sha: fromBranchInfo.data.commit.sha,
});
}
/**
* Upload multiple files to a separate branch under a single commit.
*/
async uploadFiles({
files,
branch,
commitMessage,
}: {
files: ReadonlyArray<{ path: string; content: Buffer }>;
commitMessage: string;
branch: string;
}) {
if (!files.length) {
return [];
}
await this.createCommit({
files,
branch,
commitMessage,
});
const { repo, owner } = this.context.repo();
return files.map(
({ path }) =>
`${GITHUB_CDN_DOMAIN}/${owner}/${repo}/${branch}/${path}`
);
}
/**
* Delete files in the following branch.
*/
async deleteFiles({
paths,
commitMessage,
branch,
}: {
paths: string[];
commitMessage: string;
branch: string;
}) {
if (!paths.length) {
return;
}
await this.createCommit({
files: paths.map((path) => ({ path, content: null })),
branch,
commitMessage,
});
}
/**
* List pull requests.
* ___
* This method uses github api endpoint
* {@link https://docs.github.com/en/rest/reference/pulls#list-pull-requests List pull requests}.
*
* GitHub App must have the **pull_requests:read** permission to use this endpoints.
*/
async getPRsList() {
return this.context.octokit.rest.pulls.list(this.context.repo());
}
/**
* Create commit and push it to the top of the branch.
* ___
* This method uses github api endpoints:
* - {@link https://docs.github.com/en/rest/git/blobs#create-a-blob Create a blob}
* - {@link https://docs.github.com/en/rest/git/refs#get-a-reference Get a reference}
* - {@link https://docs.github.com/en/rest/reference/git#create-a-tree Create a tree}
* - {@link https://docs.github.com/en/rest/reference/git#create-a-commit Create a commit}
* - {@link https://docs.github.com/en/rest/git/refs#update-a-reference Update a reference}
*/
protected async createCommit({
files,
branch,
commitMessage,
}: {
files: ReadonlyArray<{ path: string; content: Buffer | null }>;
commitMessage: string;
branch: string;
}) {
if (!files.length) {
throw new Error('[createCommit] Empty array is forbidden');
}
const repo = this.context.repo();
const storageBranchRef = `heads/${branch}`;
const filesNewSha = await Promise.all(
files.map(({ content, path }) => {
const blobs = content
? this.createBlob(content).then(({ sha }) => sha)
: Promise.resolve(null);
return blobs.then((sha) => ({
path,
sha,
}));
})
);
const baseTreeSha = await this.context.octokit.git
.getRef({
...repo,
ref: storageBranchRef,
})
.then(({ data }) => data.object.sha);
const newTreeSha = await this.context.octokit.git
.createTree({
...repo,
tree: filesNewSha.map(({ path, sha }) => ({
path,
sha,
type: 'blob',
mode: GithubFileMode.Blob,
})),
base_tree: baseTreeSha,
})
.then(({ data }) => data.sha);
const commitSha = await this.context.octokit.git
.createCommit({
...repo,
tree: newTreeSha,
parents: [baseTreeSha],
message: commitMessage,
})
.then(({ data }) => data.sha);
await this.context.octokit.git.updateRef({
...repo,
ref: storageBranchRef,
sha: commitSha,
});
}
private async createBlob(fileContent: Buffer) {
return this.context.octokit.git
.createBlob({
...this.context.repo(),
content: fileContent.toString('base64'),
encoding: 'base64',
})
.then(({ data }) => data);
}
}
export class ScreenshotBot<T extends EmitterWebhookEventName> extends Bot<T> {
private botConfigs: Required<IBotConfigs> | null = null;
async loadBotConfigs(branch?: string): Promise<Required<IBotConfigs>> {
return this.getFile(`.github/${BOT_CONFIGS_FILE_NAME}`, branch)
.then(
(res) =>
res ||
this.getFile(DEPRECATED_BOT_CONFIGS_FILE_NAME, branch)
)
.then((res) =>
res?.data && 'content' in res.data ? res.data.content : ''
)
.then((base64Str) => parseTomlFileBase64Str<IBotConfigs>(base64Str))
.then((configs) => ({ ...DEFAULT_BOT_CONFIGS, ...configs }))
.catch(() => DEFAULT_BOT_CONFIGS);
}
async getBotConfigs(
branch: string = DEFAULT_MAIN_BRANCH
): Promise<Required<IBotConfigs>> {
return this.botConfigs || this.loadBotConfigs(branch);
}
async getPrevBotReportComment(prNumber: number) {
const prComments = await this.getCommentsByIssueId(prNumber);
return (
prComments.find(({ body }) =>
checkContainsHiddenLabel(body || '', TEST_REPORT_HIDDEN_LABEL)
) || null
);
}
async createOrUpdateReport(prNumber: number, markdownText: string) {
const oldBotComment = await this.getPrevBotReportComment(prNumber);
const markedMarkdownText = markCommentWithHiddenLabel(
markdownText,
TEST_REPORT_HIDDEN_LABEL
);
return oldBotComment?.id
? this.updateComment(oldBotComment.id, markedMarkdownText)
: this.sendComment(prNumber, markedMarkdownText);
}
async getImagesByFn(
zipFiles: Array<ArrayBuffer | Buffer>,
fn: (zipFile: ArrayBuffer | Buffer) => IZipEntry[],
branch?: string
): Promise<IZipEntry[]> {
if (!zipFiles.length) {
return Promise.resolve([]);
}
if (!this.botConfigs) {
this.botConfigs = await this.loadBotConfigs(branch);
}
const screenshots = [];
for (const file of zipFiles) {
const images = fn(file);
screenshots.push(...images);
}
return screenshots;
}
async getNewScreenshotImages(
zipFiles: Array<ArrayBuffer | Buffer>,
branch?: string
): Promise<IZipEntry[]> {
const filterFn = (zipFile: ArrayBuffer | Buffer) =>
findNewScreenshotImages(
zipFile,
this.botConfigs?.newScreenshotMark
);
return this.getImagesByFn(zipFiles, filterFn, branch);
}
async getScreenshotDiffImages(
zipFiles: Array<ArrayBuffer | Buffer>,
branch?: string
): Promise<IZipEntry[]> {
const filterFn = (zipFile: ArrayBuffer | Buffer) =>
findScreenshotDiffImages(
zipFile,
this.botConfigs?.screenshotsDiffsPaths
);
return this.getImagesByFn(zipFiles, filterFn, branch);
}
async uploadImages(
images: Buffer[],
prNumber: number,
workflowRunId: number
) {
await this.createBranch(STORAGE_BRANCH);
const files = images.map((content, i) => ({
content,
path: `${this.getSavedImagePathPrefix(
prNumber
)}/${workflowRunId}-${i}.png`,
}));
return this.uploadFiles({
files,
branch: STORAGE_BRANCH,
commitMessage: `ci(screenshot-bot): Upload | {pr: ${prNumber}, workflow: ${workflowRunId}}`,
});
}
async checkShouldSkipWorkflow(
workflowName: string,
workflowBranch?: string
): Promise<boolean> {
if (!this.botConfigs) {
this.botConfigs = await this.loadBotConfigs(workflowBranch);
}
const { workflowWithTests, branchesIgnore } = this.botConfigs;
const workflowWithNoTests =
!workflowName ||
!workflowWithTests.some((regExp) =>
new RegExp(regExp, 'gi').test(workflowName)
);
const branchIgnored =
!!workflowBranch &&
branchesIgnore.some((regExp) =>
new RegExp(regExp, 'gi').test(workflowBranch)
);
return workflowWithNoTests || branchIgnored;
}
async deleteUploadedImagesFolder(prNumber: number) {
const folder = await this.getFile(
this.getSavedImagePathPrefix(prNumber),
STORAGE_BRANCH
);
const paths =
folder && Array.isArray(folder.data)
? folder.data.map(({ path }) => path)
: [];
return this.deleteFiles({
paths,
commitMessage: `ci(screenshot-bot): Delete | {pr: ${prNumber}}`,
branch: STORAGE_BRANCH,
});
}
/**
* If it is contribution from forked repo,
* `workflow-run` event will always have `pull_requests` field as empty array.
* See this {@link https://github.community/t/pull-request-attribute-empty-in-workflow-run-event-object-for-pr-from-forked-repo/154682 issue}.
*
* In this case bot get PR number in another way (with help of addition API request).
*/
async getWorkflowPrNumber(): Promise<number | null> {
if (!isWorkflowContext(this.context)) {
return null;
}
const [prNumber] = getWorkflowPrNumbers(this.context);
if (typeof prNumber === 'number') {
// almost all cases (except for contribution via forked repo)
return prNumber;
}
const currentRepoPulls = await this.getPRsList().then(
({ data }) => data
);
const headSha = getWorkflowHeadSha(this.context);
const contributionPR =
currentRepoPulls.find((pr) => pr.head.sha === headSha) || null;
return contributionPR && contributionPR.number;
}
private getSavedImagePathPrefix(prNumber: number): string {
const { repo, owner } = this.context.repo();
return `${IMAGES_STORAGE_FOLDER}/${owner}-${repo}-${prNumber}`;
}
}
/* eslint-enable @typescript-eslint/naming-convention */