From 8f7e59742f700119875eba1d08d265eae3bd83d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sun, 22 Nov 2026 10:05:26 +0000 Subject: [PATCH] fix(ci): a rerun of the captures check reads the PR's current body (#8479) --- scripts/ci/resolve-workflow-pr.mjs | 37 ++++++++++++------------ scripts/ci/resolve-workflow-pr.test.mjs | 38 ++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 19 deletions(-) diff --git a/scripts/ci/resolve-workflow-pr.mjs b/scripts/ci/resolve-workflow-pr.mjs index 59e237c738..2065871332 100644 --- a/scripts/ci/resolve-workflow-pr.mjs +++ b/scripts/ci/resolve-workflow-pr.mjs @@ -15,28 +15,29 @@ export function dispatchedPullRequest(pulls, repository, branch, sha) { return pr; } -function main() { - const event = JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH, "utf8")); - if (process.env.GITHUB_EVENT_NAME === "workflow_dispatch") { - const repository = process.env.GITHUB_REPOSITORY; - const branch = process.env.GITHUB_REF_NAME; +/** The PR a check runs for. A PR event carries the body it had when it fired, and a rerun replays that event, so the + * body is read now; the head stays the one the run checks out. */ +export function resolvedPullRequest(event, env, api) { + const repository = env.GITHUB_REPOSITORY; + if (env.GITHUB_EVENT_NAME === "workflow_dispatch") { + const branch = env.GITHUB_REF_NAME; const owner = repository.split("/")[0]; - const raw = execFileSync( - "gh", - [ - "api", - `repos/${repository}/pulls?state=open&head=${encodeURIComponent(`${owner}:${branch}`)}&per_page=100`, - ], - { encoding: "utf8", timeout: 60_000 }, - ); - event.pull_request = dispatchedPullRequest( - JSON.parse(raw), - repository, - branch, - process.env.GITHUB_SHA, + const pulls = api( + `repos/${repository}/pulls?state=open&head=${encodeURIComponent(`${owner}:${branch}`)}&per_page=100`, ); + return dispatchedPullRequest(pulls, repository, branch, env.GITHUB_SHA); } if (!event.pull_request) throw new Error("This check requires an open pull request."); + const live = api(`repos/${repository}/pulls/${event.pull_request.number}`); + return { ...event.pull_request, body: live.body }; +} + +const ghApi = (path) => + JSON.parse(execFileSync("gh", ["api", path], { encoding: "utf8", timeout: 60_000 })); + +function main() { + const event = JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH, "utf8")); + event.pull_request = resolvedPullRequest(event, process.env, ghApi); const path = join(process.env.RUNNER_TEMP, "workflow-pr-event.json"); writeFileSync(path, JSON.stringify(event)); appendFileSync( diff --git a/scripts/ci/resolve-workflow-pr.test.mjs b/scripts/ci/resolve-workflow-pr.test.mjs index 953289493a..f22a6c7572 100644 --- a/scripts/ci/resolve-workflow-pr.test.mjs +++ b/scripts/ci/resolve-workflow-pr.test.mjs @@ -1,6 +1,6 @@ import assert from "node:assert/strict"; import { test } from "node:test"; -import { dispatchedPullRequest } from "./resolve-workflow-pr.mjs"; +import { dispatchedPullRequest, resolvedPullRequest } from "./resolve-workflow-pr.mjs"; const pr = { state: "open", @@ -25,3 +25,39 @@ test("refuses ambiguous PRs", () => { test("refuses a PR which advanced after dispatch", () => { assert.throws(() => resolve([pr], "b".repeat(40)), /head changed/); }); + +// fails without the fix: a rerun of a PR's first check run replayed the body the PR opened with, so a body fixed +// since (Before/After captures added) still failed the captures check on every rerun. +test("a PR event takes the PR's current body, and keeps the head the run is for", () => { + const opened = { ...pr, body: "as opened" }; + const asked = []; + const api = (path) => { + asked.push(path); + return { ...pr, body: "as edited", head: { ...pr.head, sha: "b".repeat(40) } }; + }; + const event = { pull_request: opened }; + const env = { GITHUB_EVENT_NAME: "pull_request", GITHUB_REPOSITORY: "owner/repo" }; + const resolved = resolvedPullRequest(event, env, api); + assert.deepEqual(asked, ["repos/owner/repo/pulls/7"]); + assert.equal(resolved.body, "as edited"); + assert.equal(resolved.head.sha, pr.head.sha); +}); +test("a dispatch finds its PR by branch and exact head", () => { + const env = { + GITHUB_EVENT_NAME: "workflow_dispatch", + GITHUB_REPOSITORY: "owner/repo", + GITHUB_REF_NAME: pr.head.ref, + GITHUB_SHA: pr.head.sha, + }; + const api = (path) => { + assert.match(path, /^repos\/owner\/repo\/pulls\?state=open&head=owner%3Abot%2Fcatalog-publish/); + return [pr]; + }; + assert.equal(resolvedPullRequest({}, env, api), pr); +}); +test("refuses an event without a PR", () => { + assert.throws( + () => resolvedPullRequest({}, { GITHUB_EVENT_NAME: "push" }, () => null), + /open pull request/, + ); +});