From 386854a5d35b67ff174d5cded925af78ca7eb305 Mon Sep 17 00:00:00 2001 From: Alexandre Zollinger Chohfi Date: Fri, 7 Nov 2025 11:45:11 -0800 Subject: [PATCH 1/5] Refactor getCheckRunContext to improve SHA resolution from workflow_run events --- src/utils/github-utils.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/utils/github-utils.ts b/src/utils/github-utils.ts index 4f9ee512..8dca8fdb 100644 --- a/src/utils/github-utils.ts +++ b/src/utils/github-utils.ts @@ -6,6 +6,8 @@ import * as github from '@actions/github' import {GitHub} from '@actions/github/lib/utils' import type {PullRequest, WorkflowRunEvent} from '@octokit/webhooks-types' +type WorkflowRunPR = WorkflowRunEvent['workflow_run']['pull_requests'][number] + export function getCheckRunContext(): {sha: string; runId: number} { if (github.context.eventName === 'workflow_run') { core.info('Action was triggered by workflow_run: using SHA and RUN_ID from triggering workflow') @@ -13,8 +15,15 @@ export function getCheckRunContext(): {sha: string; runId: number} { if (!event.workflow_run) { throw new Error("Event of type 'workflow_run' is missing 'workflow_run' field") } + const prs = (event.workflow_run.pull_requests ?? []) as WorkflowRunPR[] + const prShaMatch = prs.find(pr => pr.head?.ref === event.workflow_run.head_branch)?.head?.sha + const prShaFirst = prs[0]?.head?.sha + const sha = prShaMatch ?? prShaFirst ?? event.workflow_run.head_sha + if (!sha) { + throw new Error('Unable to resolve SHA from workflow_run (no PR head.sha or head_sha)') + } return { - sha: event.workflow_run.head_commit.id, + sha, runId: event.workflow_run.id } } From 67e9e545d62b537a0b90929b27d275b19d0d6fc3 Mon Sep 17 00:00:00 2001 From: Jozef Izso Date: Mon, 2 Mar 2026 13:13:41 +0100 Subject: [PATCH 2/5] Document the new behavior for evaluating the associated commit --- src/utils/github-utils.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/utils/github-utils.ts b/src/utils/github-utils.ts index 8dca8fdb..569ba2c2 100644 --- a/src/utils/github-utils.ts +++ b/src/utils/github-utils.ts @@ -16,6 +16,9 @@ export function getCheckRunContext(): {sha: string; runId: number} { throw new Error("Event of type 'workflow_run' is missing 'workflow_run' field") } const prs = (event.workflow_run.pull_requests ?? []) as WorkflowRunPR[] + // For `workflow_run`, we want to report against the PR commit when possible so annotations land + // on the contributor's changes. Prefer the PR whose `head.ref` matches `workflow_run.head_branch`, + // then fall back to the first PR head SHA, and finally to `workflow_run.head_sha` for non-PR runs. const prShaMatch = prs.find(pr => pr.head?.ref === event.workflow_run.head_branch)?.head?.sha const prShaFirst = prs[0]?.head?.sha const sha = prShaMatch ?? prShaFirst ?? event.workflow_run.head_sha From 8446e5e7014aa30653f08f08b5e8f924157f559f Mon Sep 17 00:00:00 2001 From: Jozef Izso Date: Mon, 2 Mar 2026 13:17:13 +0100 Subject: [PATCH 3/5] Simplify the code as the cast to `WorkflowRunPR` type is not necessary Fixes the error: ``` test-reporter/src/utils/github-utils.ts 18:17 error This assertion is unnecessary since it does not change the type of the expression @typescript-eslint/no-unnecessary-type-assertion ``` --- src/utils/github-utils.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/utils/github-utils.ts b/src/utils/github-utils.ts index 569ba2c2..d1539d95 100644 --- a/src/utils/github-utils.ts +++ b/src/utils/github-utils.ts @@ -6,8 +6,6 @@ import * as github from '@actions/github' import {GitHub} from '@actions/github/lib/utils' import type {PullRequest, WorkflowRunEvent} from '@octokit/webhooks-types' -type WorkflowRunPR = WorkflowRunEvent['workflow_run']['pull_requests'][number] - export function getCheckRunContext(): {sha: string; runId: number} { if (github.context.eventName === 'workflow_run') { core.info('Action was triggered by workflow_run: using SHA and RUN_ID from triggering workflow') @@ -15,7 +13,7 @@ export function getCheckRunContext(): {sha: string; runId: number} { if (!event.workflow_run) { throw new Error("Event of type 'workflow_run' is missing 'workflow_run' field") } - const prs = (event.workflow_run.pull_requests ?? []) as WorkflowRunPR[] + const prs = event.workflow_run.pull_requests ?? [] // For `workflow_run`, we want to report against the PR commit when possible so annotations land // on the contributor's changes. Prefer the PR whose `head.ref` matches `workflow_run.head_branch`, // then fall back to the first PR head SHA, and finally to `workflow_run.head_sha` for non-PR runs. From 2c50ce608c211469673b07eea04b947e0a3a54be Mon Sep 17 00:00:00 2001 From: Jozef Izso Date: Mon, 2 Mar 2026 13:17:41 +0100 Subject: [PATCH 4/5] Rebuild the `dist/index.js` file --- dist/index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/dist/index.js b/dist/index.js index b24a5071..773e3e1c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2947,8 +2947,18 @@ function getCheckRunContext() { if (!event.workflow_run) { throw new Error("Event of type 'workflow_run' is missing 'workflow_run' field"); } + const prs = event.workflow_run.pull_requests ?? []; + // For `workflow_run`, we want to report against the PR commit when possible so annotations land + // on the contributor's changes. Prefer the PR whose `head.ref` matches `workflow_run.head_branch`, + // then fall back to the first PR head SHA, and finally to `workflow_run.head_sha` for non-PR runs. + const prShaMatch = prs.find(pr => pr.head?.ref === event.workflow_run.head_branch)?.head?.sha; + const prShaFirst = prs[0]?.head?.sha; + const sha = prShaMatch ?? prShaFirst ?? event.workflow_run.head_sha; + if (!sha) { + throw new Error('Unable to resolve SHA from workflow_run (no PR head.sha or head_sha)'); + } return { - sha: event.workflow_run.head_commit.id, + sha, runId: event.workflow_run.id }; } From d74e0bdec1ec86c32b65356b238106e1c00b22a7 Mon Sep 17 00:00:00 2001 From: Jozef Izso Date: Mon, 2 Mar 2026 13:23:30 +0100 Subject: [PATCH 5/5] Update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9163f49c..93d309cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 2.6.0 +* Fix: For `workflow_run` events, resolve the commit of the check run from related pull request head commits first (matching `workflow_run.head_branch`, then first PR), and fall back to `workflow_run.head_sha` for non-PR runs https://github.com/dorny/test-reporter/pull/673 + ## 2.5.0 * Feature: Add Nette Tester support with `tester-junit` reporter https://github.com/dorny/test-reporter/pull/707 * Maintenance: Bump actions/upload-artifact from 5 to 6 https://github.com/dorny/test-reporter/pull/695