diff --git a/scripts/required_pr_merge_gate.mjs b/scripts/required_pr_merge_gate.mjs index 3869ea3a6..84f776ce0 100644 --- a/scripts/required_pr_merge_gate.mjs +++ b/scripts/required_pr_merge_gate.mjs @@ -180,6 +180,32 @@ export function extractPrNumbers(event) { .filter(n=>Number.isSafeInteger(n)&&n>0))]; } +// The workflow_run payload for an owner-dispatched full Chromium run may have +// no pull_requests, even when GitHub's REST run readback associates a PR. +// Only the approved UI workflow may trigger commit->PR lookup in that case. +// Recompute ALL required lanes from verified jobs; never trust the event's +// success or a display title as a substitute for real qualification. +export function fallbackSourceForWorkflowRun(event) { + const run=event?.workflow_run; + const sha=run?.head_sha; + if (!/^[0-9a-f]{40}$/.test(sha||"")) return null; + const knownAutomatic=run.event==="pull_request" && + Object.values(workflowNames).some(workflow=>workflowPath(run,workflow)); + const approvedManualUi=run.event==="workflow_dispatch" && + workflowPath(run,workflowNames.ui); + return knownAutomatic || approvedManualUi ? sha : null; +} + +export function associatedCurrentPrNumbers(event,related,repo) { + const sha=fallbackSourceForWorkflowRun(event); + if (!sha || !Array.isArray(related)) return []; + return [...new Set(related.filter(pr=> + Number.isSafeInteger(pr?.number) && pr.number>0 && + pr.state==="open" && pr.head?.sha===sha && + pr.base?.ref==="main" && pr.base?.repo?.full_name===repo + ).map(pr=>pr.number))]; +} + export async function refreshOne(repo,number,api) { const pr=await api("/pulls/"+number); if(pr?.state!=="open"||pr?.base?.ref!=="main"|| @@ -219,13 +245,14 @@ async function main(){ const api=(path,method="GET",payload)=>request(repo,path,token,method,payload); const prs=extractPrNumbers(event); for(const n of prs)console.log(JSON.stringify(await refreshOne(repo,n,api))); - // Some workflow_run events omit pull_requests. Reconcile their source SHA - // from GitHub's verified commit->PR association, never guess a PR number. - if(!prs.length && event.workflow_run?.event==="pull_request" && - /^[a-f0-9]{40}$/.test(event.workflow_run.head_sha||"")) { - const related=await api("/commits/"+event.workflow_run.head_sha+"/pulls?per_page=100"); - for(const pr of related.filter(p=>p.state==="open")) - console.log(JSON.stringify(await refreshOne(repo,pr.number,api))); + // Completion events can omit pull_requests. Approved manual UI is the + // only workflow_dispatch fallback; manual Windows already has a verified + // PR+SHA title, and unrelated dispatches must not mutate PR statuses. + const fallbackSha=prs.length ? null : fallbackSourceForWorkflowRun(event); + if(fallbackSha) { + const related=await api("/commits/"+fallbackSha+"/pulls?per_page=100"); + for(const n of associatedCurrentPrNumbers(event,related,repo)) + console.log(JSON.stringify(await refreshOne(repo,n,api))); } } if(process.argv[1]?.endsWith("required_pr_merge_gate.mjs")) diff --git a/tests/required_pr_merge_gate.test.mjs b/tests/required_pr_merge_gate.test.mjs index fef94a420..def284c03 100644 --- a/tests/required_pr_merge_gate.test.mjs +++ b/tests/required_pr_merge_gate.test.mjs @@ -2,7 +2,8 @@ import test from "node:test"; import assert from "node:assert/strict"; import {context,requiredLanes,expectedWindowsTitle,workflowPath,findRuns,evaluate, - extractPrNumbers,refreshOne} from "../scripts/required_pr_merge_gate.mjs"; + extractPrNumbers,refreshOne,fallbackSourceForWorkflowRun, + associatedCurrentPrNumbers} from "../scripts/required_pr_merge_gate.mjs"; const sha="478cf65d18f05d3bdb62285ab32e56e427304930"; const repo="masarray/arsonmix",number=753; @@ -212,3 +213,39 @@ test("status publication is exact-head and uses deterministic scoped checks",asy assert.equal(writes[0].state,"success"); assert.equal(writes[0].context,context); }); + +test("completed manual UI exact-head can refresh when workflow_run omits pull_requests",()=>{ + const event={workflow_run:{event:"workflow_dispatch",status:"completed", + path:".github/workflows/ui-smoke.yml",head_sha:sha,pull_requests:[]}}; + assert.deepEqual(extractPrNumbers(event),[]); + assert.equal(fallbackSourceForWorkflowRun(event),sha); + const pr={number,state:"open",head:{sha},base:{ref:"main",repo:{full_name:repo}}}; + assert.deepEqual(associatedCurrentPrNumbers(event,[pr,pr],repo),[number]); +}); +test("manual UI fallback is fail-closed on stale SHA, unrelated workflow and PR identity",()=>{ + const ui={workflow_run:{event:"workflow_dispatch", + path:".github/workflows/ui-smoke.yml",head_sha:sha,pull_requests:[]}}; + const good={number,state:"open",head:{sha},base:{ref:"main",repo:{full_name:repo}}}; + const wrongSha={...good,number:754,head:{sha:"a".repeat(40)}}; + const closed={...good,number:755,state:"closed"}; + const wrongBase={...good,number:756,base:{ref:"dev",repo:{full_name:repo}}}; + const forkTarget={...good,number:757,base:{ref:"main",repo:{full_name:"other/repo"}}}; + assert.deepEqual(associatedCurrentPrNumbers(ui, + [wrongSha,closed,wrongBase,forkTarget,good],repo),[number]); + assert.deepEqual(associatedCurrentPrNumbers(ui,[good],"other/repo"),[]); + assert.equal(fallbackSourceForWorkflowRun({workflow_run:{ + ...ui.workflow_run,head_sha:"not-a-sha"}}),null); + for(const path of [".github/workflows/p1-windows.yml", + ".github/workflows/release-contracts.yml", + ".github/workflows/untrusted.yml"]){ + assert.equal(fallbackSourceForWorkflowRun({workflow_run:{ + ...ui.workflow_run,path}}),null); + } + assert.equal(fallbackSourceForWorkflowRun({workflow_run:{ + ...ui.workflow_run,event:"schedule"}}),null); + assert.equal(fallbackSourceForWorkflowRun({workflow_run:{ + event:"pull_request",path:".github/workflows/untrusted.yml",head_sha:sha}}),null); + assert.equal(fallbackSourceForWorkflowRun({workflow_run:{ + event:"pull_request",path:".github/workflows/p1-windows.yml",head_sha:sha}}),sha); + assert.deepEqual(associatedCurrentPrNumbers(ui,null,repo),[]); +});