Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion .github/scripts/native-queue-policy.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
};
29 changes: 29 additions & 0 deletions .github/scripts/native-queue-policy.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down
Loading