From bc19cac8e435e6186989724cd476894f2396b0f5 Mon Sep 17 00:00:00 2001 From: Danyang Chen Date: Tue, 17 Nov 2026 08:37:42 +0800 Subject: [PATCH] Retry the pull-request read in audit-merges (#3127) One empty `gh api` reply (`JSON.parse('')` -> unexpected end of JSON input) ended the patrol with exit 1 and no finding, so "the API hiccuped" and "a merge skipped the queue" were the same red. The read is now retried 3 times with a short backoff; when it still fails the run fails with `could not read the pulls of after 3 attempts: ` and never reports that commit as bypassed or direct. A `[]` from /pulls stays an answer. Docs-not-needed: .github/scripts/audit-merges.mjs is a read-only patrol script; no subsystem page owns it and this change adds no interface. --- .github/scripts/audit-merges.mjs | 48 +++++++++++-- .github/scripts/audit-merges.test.mjs | 100 ++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 5 deletions(-) diff --git a/.github/scripts/audit-merges.mjs b/.github/scripts/audit-merges.mjs index 53b9686b5..b920da2ca 100644 --- a/.github/scripts/audit-merges.mjs +++ b/.github/scripts/audit-merges.mjs @@ -136,6 +136,34 @@ export const exitCodeFor = (findings) => (findings.some((finding) => !finding.ea const realGh = (...args) => execFileSync('gh', args, { encoding: 'utf8', maxBuffer: 256 << 20 }) +// #3127: `gh api` has three ways to hand back no answer, and they are one event: it exits nonzero +// (execFileSync throws), it prints nothing (`JSON.parse('')` throws `unexpected end of JSON input`), +// or it prints a body that is not JSON. None of them says anything about the commit — in particular +// none of them means "this commit has no pull request" — so a read is retried before it may fail the +// run. Without this, one API hiccup turned the patrol red with no finding and hid the window it had +// not audited yet. +export const READ_ATTEMPTS = 3 +export const READ_BACKOFF_MS = 100 +const realSleep = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms) + +export function retryRead(read, { attempts = READ_ATTEMPTS, sleep = realSleep } = {}) { + let last + for (let attempt = 1; attempt <= attempts; attempt += 1) { + try { + return read() + } catch (e) { + last = e + if (attempt < attempts) sleep(READ_BACKOFF_MS * attempt) + } + } + throw last +} + +// A retried read still failed: the reason is one line, so the error names the commit and the cause +// without echoing a whole `execFileSync` stack into the job log. +const reasonOf = (e) => + String(e?.message ?? e).split('\n').map((line) => line.trim()).find(Boolean) ?? 'no answer' + function arg(name) { const i = process.argv.indexOf(name) return i > 0 ? process.argv[i + 1] : undefined @@ -143,8 +171,9 @@ function arg(name) { // The entry point a workflow or a hand run calls. `api`, `env` and the options are injectable so a // test drives the whole walk from fixtures without the network. -export function main({ api, gh = realGh, env = process.env, repo: repoOption, limit: limitOption, since: sinceOption, baseline: baselineOption } = {}) { +export function main({ api, gh = realGh, sleep = realSleep, env = process.env, repo: repoOption, limit: limitOption, since: sinceOption, baseline: baselineOption } = {}) { const fetchJson = api ?? ((path) => JSON.parse(gh('api', path))) + const readJson = (path) => retryRead(() => fetchJson(path), { sleep }) const repo = repoOption || env.GH_REPO || arg('--repo') if (!repo) throw new Error('set GH_REPO or pass --repo owner/name') const limit = Number(limitOption ?? arg('--limit') ?? env.LIMIT ?? 50) @@ -153,14 +182,23 @@ export function main({ api, gh = realGh, env = process.env, repo: repoOption, li const baseline = Boolean(baselineOption ?? (process.argv.includes('--baseline') || env.BASELINE)) const base = `repos/${repo}` - const since = sinceDate({ value: sinceValue, repo, fetchJson }) - const commits = fetchJson(`${base}/commits?sha=main&per_page=${limit}`) + const since = sinceDate({ value: sinceValue, repo, fetchJson: readJson }) + const commits = readJson(`${base}/commits?sha=main&per_page=${limit}`) if (!Array.isArray(commits)) throw new Error('the commits API did not return a list') + // `?? []` keeps a `[]` — and a `null` body — an answer ("no pull request"). A read that never + // answered is a different thing: it is thrown, naming the commit, and never becomes a finding. + const pullsFor = (sha) => { + try { + return readJson(`${base}/commits/${sha}/pulls?per_page=100`) ?? [] + } catch (e) { + throw new Error(`could not read the pulls of ${sha} after ${READ_ATTEMPTS} attempts: ${reasonOf(e)}`) + } + } const findings = classify(audit({ commits, - pullsFor: (sha) => fetchJson(`${base}/commits/${sha}/pulls?per_page=100`) ?? [], + pullsFor, // The runs endpoint answers `{ total_count, workflow_runs }`; only the runs matter here. - runsFor: (sha) => fetchJson(`${base}/actions/runs?event=merge_group&head_sha=${sha}&per_page=100`)?.workflow_runs ?? [], + runsFor: (sha) => readJson(`${base}/actions/runs?event=merge_group&head_sha=${sha}&per_page=100`)?.workflow_runs ?? [], }), { since, baseline }) for (const finding of findings) console.log(finding.line) const failing = findings.filter((finding) => !finding.earlier) diff --git a/.github/scripts/audit-merges.test.mjs b/.github/scripts/audit-merges.test.mjs index b516e7e47..4960817d3 100644 --- a/.github/scripts/audit-merges.test.mjs +++ b/.github/scripts/audit-merges.test.mjs @@ -261,3 +261,103 @@ test('#697: the patrol is read-only, runs daily, and takes its own previous run assert.match(text, /uses: actions\/setup-node@[0-9a-f]{40}/) assert.doesNotMatch(text, /permissions: write-all|contents: write/, 'no write path into the repository') }) + +// #3127: the patrol went red with no finding because one `gh api` reply came back empty and +// `JSON.parse('')` threw. A read that did not answer is not an answer: retry it, and only then fail — +// naming the commit it could not read, never as a bypass or a direct commit. +const drive = (api, overrides = {}) => { + const lines = [] + const errors = [] + const [log, error] = [console.log, console.error] + console.log = (line) => lines.push(line) + console.error = (line) => errors.push(line) + let result + let failure + try { + result = main({ api, repo: 'o/r', sleep: () => {}, ...overrides }) + } catch (e) { + failure = e + } finally { + console.log = log + console.error = error + } + return { result, failure, lines, errors } +} + +test('#3127: a pulls read that fails twice and answers on the third attempt is retried, not a red run', () => { + const queued = fixture('queued') + const pullsPath = `repos/o/r/commits/${queued.sha}/pulls?per_page=100` + let attempts = 0 + const api = (path) => { + if (path === 'repos/o/r/commits?sha=main&per_page=50') return [{ sha: queued.sha }] + if (path === pullsPath) { + attempts += 1 + if (attempts < 3) throw new Error('unexpected end of JSON input') + return queued.pulls + } + if (path === `repos/o/r/actions/runs?event=merge_group&head_sha=${queued.sha}&per_page=100`) return { workflow_runs: queued.runs } + throw new Error(`unexpected path ${path}`) + } + const { result, failure, lines, errors } = drive(api) + assert.equal(failure, undefined, 'a transient read failure does not end the run') + assert.equal(attempts, 3) + assert.deepEqual(lines, [], 'the queued merge prints nothing') + assert.equal(result.ok, true) + assert.equal(exitCodeFor(result.findings), 0) + assert.match(errors.join('\n'), /1 commits on main, 0 bypassed the queue, 0 direct/) +}) + +test('#3127: a pulls read that never answers names the commit and is never reported as a finding', () => { + const queued = fixture('queued') + const pullsPath = `repos/o/r/commits/${queued.sha}/pulls?per_page=100` + let attempts = 0 + const api = (path) => { + if (path === 'repos/o/r/commits?sha=main&per_page=50') return [{ sha: queued.sha }] + if (path === pullsPath) { + attempts += 1 + throw new Error('unexpected end of JSON input') + } + if (path.startsWith('repos/o/r/actions/runs')) return { workflow_runs: [] } + throw new Error(`unexpected path ${path}`) + } + const { failure, lines } = drive(api) + assert.equal(attempts, 3, 'three attempts, then give up') + assert.ok(failure, 'the run fails instead of inventing a finding') + assert.match(failure.message, new RegExp(`^could not read the pulls of ${queued.sha} after 3 attempts: unexpected end of JSON input`)) + assert.deepEqual(lines, [], 'a read failure is not printed as a finding line') +}) + +test('#3127: a bypass fixture still fails the run after the retry change', () => { + const bypassed = fixture('bypassed') + const api = (path) => { + if (path === 'repos/o/r/commits?sha=main&per_page=50') return [{ sha: bypassed.sha }] + if (path === `repos/o/r/commits/${bypassed.sha}/pulls?per_page=100`) return bypassed.pulls + if (path === `repos/o/r/actions/runs?event=merge_group&head_sha=${bypassed.sha}&per_page=100`) return { workflow_runs: bypassed.runs } + throw new Error(`unexpected path ${path}`) + } + const { result, failure, lines } = drive(api) + assert.equal(failure, undefined) + assert.deepEqual(lines, [`bypassed queue: #680 ${bypassed.sha.slice(0, 12)} (merge_group: failure)`]) + assert.equal(result.bypassed, 1) + assert.equal(exitCodeFor(result.findings), 1) +}) + +test('#3127: an empty /pulls list is still an answer, not a read failure to retry', () => { + const direct = fixture('direct') + let pullsCalls = 0 + const api = (path) => { + if (path === 'repos/o/r/commits?sha=main&per_page=50') return [{ sha: direct.sha }] + if (path === `repos/o/r/commits/${direct.sha}/pulls?per_page=100`) { + pullsCalls += 1 + return [] + } + if (path === `repos/o/r/actions/runs?event=merge_group&head_sha=${direct.sha}&per_page=100`) return { workflow_runs: direct.runs } + throw new Error(`unexpected path ${path}`) + } + const { result, failure, lines } = drive(api) + assert.equal(failure, undefined) + assert.equal(pullsCalls, 1, 'an empty answer is accepted on the first attempt') + assert.deepEqual(lines, [`direct commit: ${direct.sha}`]) + assert.equal(result.direct, 1) + assert.equal(exitCodeFor(result.findings), 1) +})