diff --git a/.github/scripts/native-queue-policy.cjs b/.github/scripts/native-queue-policy.cjs index b2c4f8f698..91ea76049c 100644 --- a/.github/scripts/native-queue-policy.cjs +++ b/.github/scripts/native-queue-policy.cjs @@ -169,7 +169,26 @@ function snapshot(members) { return JSON.stringify(members); } -async function evaluate({ github, context }) { +// A 5xx from GitHub is the API failing, not the evidence failing: three v5 +// PRs saw `GET .../check-runs - 500` in the same second (#16258) and the +// bounded wait above threw on the first attempt. Retry it like pending +// evidence; the loop still fails closed when it never clears. 4xx stays a +// hard failure (missing refs and permissions do not fix themselves). +function isTransientAPIError(error) { + const status = Number(error?.status); + return Number.isInteger(status) && status >= 500 && status <= 599; +} + +async function evaluate(args) { + try { + return await evaluateOnce(args); + } catch (error) { + if (!isTransientAPIError(error)) throw error; + throw new PendingEvidence(`GitHub API ${error.status} (${error.message}); retrying`, { cause: error }); + } +} + +async function evaluateOnce({ github, context }) { const repo = context.repo; if (context.eventName === 'pull_request_target') { const event = context.payload.pull_request; @@ -222,4 +241,5 @@ async function evaluate({ github, context }) { module.exports = { ACTIONS_APP_ID, DCO_APP_ID, POLICY_CHECKS, BUILD_CHECKS, PendingEvidence, membersForGroup, assertEligible, evidenceForHead, closedNoop, evaluate, + isTransientAPIError, }; diff --git a/.github/scripts/native-queue-policy.test.cjs b/.github/scripts/native-queue-policy.test.cjs index e64e50471a..06fad6eb64 100644 --- a/.github/scripts/native-queue-policy.test.cjs +++ b/.github/scripts/native-queue-policy.test.cjs @@ -4,6 +4,7 @@ const assert = require('node:assert/strict'); const { ACTIONS_APP_ID, DCO_APP_ID, POLICY_CHECKS, BUILD_CHECKS, PendingEvidence, membersForGroup, assertEligible, evidenceForHead, closedNoop, evaluate, + isTransientAPIError, } = require('./native-queue-policy.cjs'); const sha = n => n.toString(16).padStart(40, '0'); @@ -207,6 +208,34 @@ test('PR closed during evidence wait passes as a no-op', async () => { assert.match(await evaluate(f), new RegExp(closedNoop(f.state.prs[0]))); }); +test('a 5xx from the API is retried as pending evidence, never read as a verdict', async () => { + const apiError = status => Object.assign(new Error(`GET /check-runs - ${status}`), { status }); + for (const eventName of ['pull_request_target', 'merge_group']) { + const f = fixture(eventName); + const paginate = f.github.paginate; + let calls = 0; + f.github.paginate = async (method, args) => { + if (method === 'checks' && ++calls === 1) throw apiError(500); + return paginate(method, args); + }; + await assert.rejects(evaluate(f), error => + error instanceof PendingEvidence && /GitHub API 500/.test(error.message) && error.cause?.status === 500); + // The retry is the workflow loop's job: the next evaluate succeeds on its own. + assert.match(await evaluate(f), /policy providers succeeded|validated all/); + } + // Only the server side is transient: a 4xx or a non-HTTP failure stays a hard failure. + for (const failure of [apiError(404), apiError(403), new Error('socket hang up')]) { + const f = fixture('pull_request_target'); + f.github.paginate = async () => { throw failure; }; + await assert.rejects(evaluate(f), error => !(error instanceof PendingEvidence) && error === failure); + } + assert.equal(isTransientAPIError({ status: 502 }), true); + assert.equal(isTransientAPIError({ status: '503' }), true); + assert.equal(isTransientAPIError({ status: 429 }), false); + assert.equal(isTransientAPIError({ status: 600 }), false); + assert.equal(isTransientAPIError(undefined), false); +}); + test('open PR with moved head still fails closed on pull_request_target', async () => { const f = fixture('pull_request_target'); f.state.prs[0].head.sha = sha(99);