From 1353ca0f60e9a3f5b86435dd79c65c5b966422bf Mon Sep 17 00:00:00 2001 From: Chris Hambridge Date: Wed, 14 Jan 2026 19:50:55 -0500 Subject: [PATCH] fix(ci): use gh pr view for fork PR number lookup in coverage comment The workflow_run event's pull_requests array is empty for fork PRs due to a known GitHub limitation. This prevented coverage comments from being posted on fork PRs. Replace the JavaScript-based pull_requests array lookup with gh pr view command, which reliably finds PRs from both forks and the same repo. Add conditional logic to use the correct branch query format: - Fork PRs: "owner:branch" format - Same-repo PRs: "branch" format See: https://github.com/orgs/community/discussions/25220 Co-Authored-By: Claude Signed-off-by: Chris Hambridge --- .github/workflows/coverage-comment.yml | 52 +++++++++++++++++--------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/.github/workflows/coverage-comment.yml b/.github/workflows/coverage-comment.yml index ed214c4..80cc0fd 100644 --- a/.github/workflows/coverage-comment.yml +++ b/.github/workflows/coverage-comment.yml @@ -24,26 +24,42 @@ jobs: steps: - name: Get PR number from workflow run id: pr_info - uses: actions/github-script@v7 - with: - script: | - // Get the PR associated with the workflow run (trusted source) - const workflowRun = await github.rest.actions.getWorkflowRun({ - owner: context.repo.owner, - repo: context.repo.repo, - run_id: context.payload.workflow_run.id - }); + env: + GH_TOKEN: ${{ github.token }} + run: | + # For fork PRs, pull_requests array is empty in workflow_run event + # Use gh pr view to find the PR number + # See: https://github.com/orgs/community/discussions/25220 - // Extract PR number from the workflow run's pull_requests array - const pullRequests = workflowRun.data.pull_requests; - if (!pullRequests || pullRequests.length === 0) { - core.setFailed('No pull request associated with this workflow run'); - return; - } + HEAD_REPO="${{ github.event.workflow_run.head_repository.full_name }}" + HEAD_BRANCH="${{ github.event.workflow_run.head_branch }}" + TARGET_REPO="${{ github.repository }}" + + echo "Looking for PR from ${HEAD_REPO} branch ${HEAD_BRANCH}" + + # Determine branch query format: + # - Fork PRs: use "owner:branch" format + # - Same-repo PRs: use just "branch" format + if [ "$HEAD_REPO" = "$TARGET_REPO" ]; then + BRANCH_QUERY="${HEAD_BRANCH}" + else + BRANCH_QUERY="${HEAD_REPO%%/*}:${HEAD_BRANCH}" + fi + + echo "Using branch query: ${BRANCH_QUERY}" + + PR_NUMBER=$(gh pr view \ + --repo "$TARGET_REPO" \ + "$BRANCH_QUERY" \ + --json number --jq .number 2>/dev/null || echo "") + + if [ -z "$PR_NUMBER" ]; then + echo "::error::Could not find PR for ${BRANCH_QUERY} in ${TARGET_REPO}" + exit 1 + fi - const prNumber = pullRequests[0].number; - core.setOutput('pr_number', prNumber); - console.log(`PR number from workflow run: ${prNumber}`); + echo "Found PR #${PR_NUMBER}" + echo "pr_number=${PR_NUMBER}" >> "$GITHUB_OUTPUT" - name: Download coverage data uses: actions/download-artifact@v4