diff --git a/scripts/cloud-release-admission.mjs b/scripts/cloud-release-admission.mjs index 007f5e0755e..4c79909af45 100644 --- a/scripts/cloud-release-admission.mjs +++ b/scripts/cloud-release-admission.mjs @@ -1,4 +1,5 @@ import { appendFile } from "node:fs/promises"; +import { setTimeout as delay } from "node:timers/promises"; import { pathToFileURL } from "node:url"; import { createActionsReader } from "./github-actions-reader.mjs"; @@ -20,27 +21,34 @@ function trusted(run, sha, workflow) { Number.isSafeInteger(run.id) && run.id > 0 && Number.isSafeInteger(run.run_attempt) && run.run_attempt > 0; } -async function producer(file, sha, api, status) { +async function producer(file, sha, api, { status, listingAttempts = 1, sleep = delay } = {}) { assertSha(sha); if (!workflows.has(file)) throw new Error("Unknown release workflow."); const workflow = await api(`/repos/${repository}/actions/workflows/${file}`); if (workflow.path !== `.github/workflows/${file}` || !Number.isSafeInteger(workflow.id) || workflow.id < 1) throw new Error("Release workflow identity mismatch."); - const list = await api(`/repos/${repository}/actions/workflows/${workflow.id}/runs?branch=master&event=push&head_sha=${sha}&per_page=100${status ? `&status=${status}` : ""}`); - if (!Array.isArray(list.workflow_runs) || !Number.isSafeInteger(list.total_count) || list.total_count < 0 || - list.total_count > 100 || list.workflow_runs.length !== list.total_count) throw new Error("Incomplete release workflow listing."); - const run = list.workflow_runs.filter(run => trusted(run, sha, workflow) && (!status || run.status === status)).sort((a, b) => b.id - a.id)[0]; - return { workflow, run }; + for (let attempt = 1; ; attempt++) { + const list = await api(`/repos/${repository}/actions/workflows/${workflow.id}/runs?branch=master&event=push&head_sha=${sha}&per_page=100${status ? `&status=${status}` : ""}`); + if (!Array.isArray(list.workflow_runs) || !Number.isSafeInteger(list.total_count) || list.total_count < 0 || + list.total_count > 100) throw new Error("Incomplete release workflow listing."); + if (list.workflow_runs.length !== list.total_count) { + if (attempt >= listingAttempts) throw new Error(`Incomplete release workflow listing for ${file} (${list.workflow_runs.length} returned, ${list.total_count} expected).`); + await sleep(1_000 * attempt); + continue; + } + const run = list.workflow_runs.filter(run => trusted(run, sha, workflow) && (!status || run.status === status)).sort((a, b) => b.id - a.id)[0]; + return { workflow, run }; + } } // A retry is an explicit request for this exact immutable source. Normal push // work starts only for a candidate admitted by an image build or npm canary. -export async function releaseAdmitted(sha, { api, event = "push", attempt = 1, imageOnly = false }) { +export async function releaseAdmitted(sha, { api, event = "push", attempt = 1, imageOnly = false, sleep }) { assertSha(sha); if (!Number.isSafeInteger(attempt) || attempt < 1) throw new Error("Invalid release attempt."); if (!["push", "workflow_dispatch"].includes(event)) return false; if (event === "workflow_dispatch" || (Number.isSafeInteger(attempt) && attempt > 1)) return true; for (const file of imageOnly ? ["docker.yml"] : ["docker.yml", "release.yml"]) { - const { workflow, run } = await producer(file, sha, api, "in_progress"); + const { workflow, run } = await producer(file, sha, api, { status: "in_progress", listingAttempts: 3, sleep }); if (!run) continue; const current = await api(`/repos/${repository}/actions/runs/${run.id}`); if (current.id === run.id && trusted(current, sha, workflow) && current.run_attempt === run.run_attempt && current.status === "in_progress") return true; diff --git a/scripts/cloud-source-verification.test.mjs b/scripts/cloud-source-verification.test.mjs index c20d1f701cf..3ae42c05de9 100644 --- a/scripts/cloud-source-verification.test.mjs +++ b/scripts/cloud-source-verification.test.mjs @@ -485,7 +485,8 @@ describe("release candidate admission", () => { const producer = run(file, { status: "completed", conclusion: "success", ...change }); const admission = file === "cloud-migrator-artifacts.yml" ? migratorAdmissionJob : sourceAdmissionJob; const f = { runs: { [file]: [producer] }, jobs: { [producer.id]: [job(producer, admission, "success"), job(producer, sourceVerificationJob)] }, - current: {}, writes: [], calls: [], requested: new Set(), file, sha, admissionJob: admission }; + current: {}, writes: [], calls: [], waits: [], requested: new Set(), file, sha, admissionJob: admission }; + f.sleep = async ms => f.waits.push(ms); f.api = async path => { f.calls.push(path); const url = new URL(`https://api.github.com${path}`); @@ -557,11 +558,49 @@ describe("release candidate admission", () => { f.total = total; await assert.rejects(releaseAdmitted(sha, f), /Incomplete/); assert.deepEqual(f.writes, []); + assert.deepEqual(f.waits, total === 1 ? [1_000, 2_000] : []); + assert.equal(f.calls.filter(path => path.includes("/runs?")).length, total === 1 ? 3 : 1); } await assert.rejects(releaseAdmitted(sha, { api: async () => { throw new Error("API unavailable"); } }), /unavailable/); await assert.rejects(releaseAdmitted("master", { api: () => assert.fail() }), /full source SHA/); }); + test("admission rereads inconsistent listings before selecting a current consumer", async () => { + for (const file of ["docker.yml", "release.yml"]) { + for (const total of [0, 2]) { + for (const changedAttempt of [false, true]) { + const f = fixture(); + const active = run(file); + f.runs[file] = [active]; + if (changedAttempt) f.current[active.id] = { ...active, run_attempt: 2 }; + const api = f.api; + let reads = 0; + f.api = async path => { + const result = await api(path); + if (path.includes(`/workflows/${ids[file]}/runs?`) && ++reads < 3) return { ...result, total_count: total }; + return result; + }; + assert.equal(await releaseAdmitted(sha, f), !changedAttempt); + assert.equal(reads, 3); + assert.deepEqual(f.waits, [1_000, 2_000]); + assert.equal(f.calls.filter(path => path.endsWith(`/runs/${active.id}`)).length, 1); + assert.deepEqual(f.writes, []); + } + } + } + }); + + test("an inconsistent listing never admits its returned consumer", async () => { + const f = fixture(); + const active = run("docker.yml"); + f.runs["docker.yml"] = [active]; + f.total = 2; + await assert.rejects(releaseAdmitted(sha, f), /Incomplete release workflow listing for docker\.yml \(1 returned, 2 expected\)/); + assert.deepEqual(f.waits, [1_000, 2_000]); + assert.equal(f.calls.filter(path => path.endsWith(`/runs/${active.id}`)).length, 0); + assert.deepEqual(f.writes, []); + }); + test("a selected consumer restarts only a strictly deferred producer, once", async () => { for (const file of ["cloud-readiness.yml", "cloud-migrator-artifacts.yml"]) { const f = fixture(file);