-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
87 lines (78 loc) · 2.13 KB
/
script.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
/**
* Removes required CI check
*
* @param {import('@octoherd/cli').Octokit} octokit
* @param {import('@octoherd/cli').Repository} repository
* @param { {check: string} } options Custom user options passed to the CLI
*/
export async function script(octokit, repository, options) {
if (!options.check) {
throw new Error(`--check is required`);
}
const owner = repository.owner.login;
const repo = repository.name;
if (repository.archived) {
octokit.log.info(
{ owner, repo, updated: false },
`${repository.html_url} is archived`
);
return;
}
const branches = await octokit.paginate(
"GET /repos/{owner}/{repo}/branches",
{
owner,
repo,
protected: true,
}
);
if (branches.length === 0) {
octokit.log.info(
{ owner, repo, updated: false },
`No protected branches in ${owner}/${repo}`
);
}
try {
for (const { name } of branches) {
const {
data: { contexts },
} = await octokit.request(
"GET /repos/{owner}/{repo}/branches/{branch}/protection/required_status_checks",
{
owner,
repo,
branch: name,
}
);
if (!contexts.includes(options.check)) {
octokit.log.info(
{ owner, repo, updated: false, contexts },
`"${options.check}" not found in "${name}"'s branch protection in ${owner}/${repo}`
);
continue;
}
await octokit.request(
"PATCH /repos/{owner}/{repo}/branches/{branch}/protection/required_status_checks",
{
owner,
repo,
branch: name,
contexts: contexts.filter((context) => context !== options.check),
}
);
octokit.log.info(
{ owner, repo, updated: true },
`"${options.check}" removed from "${name}"'s branch protection in ${owner}/${repo}`
);
}
} catch (error) {
if (/Required status checks not enabled/i.test(error.message)) {
octokit.log.info(
{ owner, repo, updated: true },
`"Required status checks not enabled in ${owner}/${repo}`
);
return;
}
throw error;
}
}