forked from electron/trop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecks-util.ts
111 lines (101 loc) · 2.86 KB
/
checks-util.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
import { CheckRunStatus } from '../enums';
import { BACKPORT_INFORMATION_CHECK, CHECK_PREFIX } from '../constants';
import {
SimpleWebHookRepoContext,
WebHookPR,
WebHookPRContext,
} from '../types';
export async function updateBackportValidityCheck(
context: WebHookPRContext,
checkRun: BackportCheck,
statusItems: {
conclusion: CheckRunStatus;
title: string;
summary: string;
},
) {
await context.octokit.checks.update(
context.repo({
check_run_id: checkRun.id,
name: checkRun.name,
conclusion: statusItems.conclusion as CheckRunStatus,
completed_at: new Date().toISOString(),
details_url:
'https://github.com/electron/trop/blob/main/docs/manual-backports.md',
output: {
title: statusItems.title,
summary: statusItems.summary,
},
}),
);
}
export async function getBackportInformationCheck(context: WebHookPRContext) {
const pr = context.payload.pull_request;
const allChecks = await context.octokit.checks.listForRef(
context.repo({
ref: pr.head.sha,
per_page: 100,
}),
);
const backportCheck = allChecks.data.check_runs.filter((run) =>
run.name.startsWith(BACKPORT_INFORMATION_CHECK),
);
return backportCheck.length > 0 ? backportCheck[0] : null;
}
type BackportCheck = NonNullable<
Awaited<ReturnType<typeof getBackportInformationCheck>>
>;
export async function updateBackportInformationCheck(
context: WebHookPRContext,
backportCheck: BackportCheck,
statusItems: {
conclusion: CheckRunStatus;
title: string;
summary: string;
},
) {
await context.octokit.checks.update(
context.repo({
check_run_id: backportCheck.id,
name: backportCheck.name,
conclusion: statusItems.conclusion as CheckRunStatus,
completed_at: new Date().toISOString(),
details_url: 'https://github.com/electron/trop',
output: {
title: statusItems.title,
summary: statusItems.summary,
},
}),
);
}
export async function queueBackportInformationCheck(context: WebHookPRContext) {
const pr = context.payload.pull_request;
await context.octokit.checks.create(
context.repo({
name: BACKPORT_INFORMATION_CHECK,
head_sha: pr.head.sha,
status: 'queued',
details_url: 'https://github.com/electron/trop',
output: {
title: 'Needs Backport Information',
summary:
'This PR requires backport information. It should have a "no-backport" or a "target/<branch>" label.',
},
}),
);
}
export async function getCheckRun(
context: SimpleWebHookRepoContext,
pr: WebHookPR,
targetBranch: string,
) {
const allChecks = await context.octokit.checks.listForRef(
context.repo({
ref: pr.head.sha,
per_page: 100,
}),
);
return allChecks.data.check_runs.find((run) => {
return run.name === `${CHECK_PREFIX}${targetBranch}`;
});
}