Skip to content

Commit f24452d

Browse files
committed
Actually perform the update when necessary and requested
1 parent 37262be commit f24452d

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

pr-checks/sync-checks.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ import * as yaml from "yaml";
1313

1414
import { OLDEST_SUPPORTED_MAJOR_VERSION } from "./config";
1515

16+
/** Represents the command-line options. */
17+
interface Options {
18+
/** The token to use to authenticate to the GitHub API. */
19+
token?: string;
20+
/** The git ref to use the checks for. */
21+
ref?: string;
22+
/** Whether to actually apply the changes or not. */
23+
apply: boolean;
24+
}
25+
1626
/** Identifies the CodeQL Action repository. */
1727
const codeqlActionRepo = {
1828
owner: "github",
@@ -130,8 +140,22 @@ async function getReleaseBranches(client: ApiClient): Promise<string[]> {
130140
return refs.data.map((ref) => ref.ref).sort();
131141
}
132142

143+
/** Updates the required status checks for `branch` to `checks`. */
144+
async function patchBranchProtectionRule(
145+
client: ApiClient,
146+
branch: string,
147+
checks: Set<string>,
148+
) {
149+
await client.rest.repos.setStatusCheckContexts({
150+
...codeqlActionRepo,
151+
branch,
152+
contexts: Array.from(checks),
153+
});
154+
}
155+
133156
/** Sets `checkNames` as required checks for `branch`. */
134157
async function updateBranch(
158+
options: Options,
135159
client: ApiClient,
136160
branch: string,
137161
checkNames: Set<string>,
@@ -169,7 +193,14 @@ async function updateBranch(
169193
`For '${branch}': ${removals} removals; ${additions} additions; ${unchanged} unchanged`,
170194
);
171195

172-
// TODO: actually perform the update
196+
// Perform the update if there are changes and `--apply` was specified.
197+
if (unchanged === checkNames.size && removals === 0 && additions === 0) {
198+
console.info("Not applying changes because there is nothing to do.");
199+
} else if (options.apply) {
200+
await patchBranchProtectionRule(client, branch, checkNames);
201+
} else {
202+
console.info("Not applying changes because `--apply` was not specified.");
203+
}
173204
}
174205

175206
async function main(): Promise<void> {
@@ -212,7 +243,7 @@ async function main(): Promise<void> {
212243
const checkNames = new Set(checkInfos.map((info) => info.context));
213244

214245
// Update the main branch.
215-
await updateBranch(client, "main", checkNames);
246+
await updateBranch(options, client, "main", checkNames);
216247

217248
// Retrieve the refs of the release branches.
218249
const releaseBranches = await getReleaseBranches(client);
@@ -241,7 +272,7 @@ async function main(): Promise<void> {
241272
);
242273
continue;
243274
} else {
244-
await updateBranch(client, releaseBranch, checkNames);
275+
await updateBranch(options, client, releaseBranch, checkNames);
245276
}
246277
}
247278

0 commit comments

Comments
 (0)