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