From ed0dbc0b99f47b285a705a4d015fc86e43660056 Mon Sep 17 00:00:00 2001 From: Tim Hosey Date: Mon, 26 Oct 2026 12:13:19 -0700 Subject: [PATCH] Report the PR head commit instead of the temporary merge commit Co-Authored-By: Claude Opus 5.5 --- src/report.js | 14 ++++++++++++-- test/report.test.js | 8 ++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/report.js b/src/report.js index bdaf600..bf41e65 100644 --- a/src/report.js +++ b/src/report.js @@ -35,7 +35,8 @@ export function renderReport(runs, verdict, { failOn = [], parameters, notes = [ else facts.push(verdict.success ? '**Build passes**' : '**Build fails**'); facts.push(`fail-on: ${failOn.length ? failOn.join(', ') : 'none'}`); for (const [k, v] of Object.entries(parameters ?? {})) facts.push(`${k}: ${code(v)}`); - if (env.GITHUB_SHA) facts.push(`commit ${code(env.GITHUB_SHA.slice(0, 7))}`); + const sha = commitSha(env); + if (sha) facts.push(`commit ${code(sha.slice(0, 7))}`); const runUrl = workflowRunUrl(env); if (runUrl) facts.push(`[workflow run](${runUrl})`); lines.push(facts.join(' ยท '), ''); @@ -114,9 +115,18 @@ export async function upsertPrComment(markdown, env, { token, key, fetchImpl = f } function pullRequestNumber(env) { + return pullRequest(env)?.number; +} + +// On pull requests GITHUB_SHA is a temporary merge commit; show the PR's head instead. +function commitSha(env) { + return pullRequest(env)?.head?.sha ?? env.GITHUB_SHA; +} + +function pullRequest(env) { if (!['pull_request', 'pull_request_target'].includes(env.GITHUB_EVENT_NAME) || !env.GITHUB_EVENT_PATH) return undefined; try { - return JSON.parse(readFileSync(env.GITHUB_EVENT_PATH, 'utf8')).pull_request?.number; + return JSON.parse(readFileSync(env.GITHUB_EVENT_PATH, 'utf8')).pull_request; } catch { return undefined; } diff --git a/test/report.test.js b/test/report.test.js index ff08c52..9e2951c 100644 --- a/test/report.test.js +++ b/test/report.test.js @@ -107,6 +107,14 @@ test('updates the existing comment for the same key, across pages', async () => assert.deepEqual(gh.calls.at(-1), { method: 'PATCH', path: '/repos/acme/web/issues/comments/777', body: { body: '\nnew' } }); }); +test('shows the PR head commit, not the merge commit', () => { + const e = prEnv(); + writeFileSync(e.GITHUB_EVENT_PATH, JSON.stringify({ pull_request: { number: 9, head: { sha: '2b523ef0000' } } })); + const md = renderReport([], { status: 'skipped' }, { env: { ...e, GITHUB_SHA: 'b366fb20000' } }); + assert.ok(md.includes('commit `2b523ef`'), md); + assert.ok(renderReport([], { status: 'skipped' }, { env: { GITHUB_SHA: 'b366fb20000' } }).includes('commit `b366fb2`')); +}); + test('does nothing outside pull requests', async () => { const gh = fakeGitHub(); assert.equal(await upsertPrComment('x', { ...prEnv(), GITHUB_EVENT_NAME: 'push' }, { token: 't', key: 'k', fetchImpl: gh.fetchImpl }), undefined);