Skip to content

Commit 1afef69

Browse files
committed
Add wait-for-checks script for docs gate
1 parent e58eb0a commit 1afef69

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

.github/wait-for-checks.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
2+
const DEFAULT_MAX_ATTEMPTS = 60;
3+
const DEFAULT_INTERVAL_MS = 10_000;
4+
const MILLISECONDS_IN_SECOND = 1000;
5+
6+
module.exports = async function waitForChecks({
7+
github,
8+
context,
9+
core,
10+
checks,
11+
}) {
12+
const owner = context.repo.owner;
13+
const repo = context.repo.repo;
14+
const ref = context.sha;
15+
const desiredChecks =
16+
Array.isArray(checks) && checks.length > 0 ? checks : ["test"];
17+
const checkSet = new Set(desiredChecks);
18+
19+
const maxAttempts = Number.parseInt(
20+
process.env.CIGEN_WAIT_CHECKS_ATTEMPTS || String(DEFAULT_MAX_ATTEMPTS),
21+
10
22+
);
23+
const intervalMs = Number.parseInt(
24+
process.env.CIGEN_WAIT_CHECKS_INTERVAL_MS || String(DEFAULT_INTERVAL_MS),
25+
10
26+
);
27+
28+
core.info(
29+
`Waiting for checks [${desiredChecks.join(", ")}] on ${owner}/${repo}@${ref}`
30+
);
31+
32+
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
33+
const { data } = await github.rest.checks.listForRef({
34+
owner,
35+
repo,
36+
ref,
37+
});
38+
39+
const runs = data.check_runs.filter((run) => checkSet.has(run.name));
40+
41+
if (runs.length === checkSet.size) {
42+
const incomplete = runs.filter((run) => run.status !== "completed");
43+
if (incomplete.length === 0) {
44+
const failures = runs.filter((run) => run.conclusion !== "success");
45+
if (failures.length > 0) {
46+
const summary = failures
47+
.map((run) => `${run.name} -> ${run.conclusion}`)
48+
.join(", ");
49+
throw new Error(`Checks failed: ${summary}`);
50+
}
51+
52+
core.info("All required checks completed successfully.");
53+
return;
54+
}
55+
}
56+
57+
core.info(
58+
`Attempt ${attempt}/${maxAttempts}: checks not complete yet. Sleeping ${(
59+
intervalMs / MILLISECONDS_IN_SECOND
60+
).toFixed(1)}s...`
61+
);
62+
await sleep(intervalMs);
63+
}
64+
65+
throw new Error(
66+
`Timed out waiting for checks: ${desiredChecks.join(", ")}. Increase CIGEN_WAIT_CHECKS_ATTEMPTS?`
67+
);
68+
};

0 commit comments

Comments
 (0)