|
| 1 | +# ============================================================================ |
| 2 | +# Coverage Comment Workflow |
| 3 | +# ============================================================================ |
| 4 | +# Posts coverage reports as PR comments using the workflow_run event. |
| 5 | +# This approach is fork-safe: the GITHUB_TOKEN has write access because |
| 6 | +# this workflow always runs from the default branch's code, not the fork's. |
| 7 | +# |
| 8 | +# Security: |
| 9 | +# - All artifact data (PR number, coverage values) is validated with |
| 10 | +# strict regex before use to prevent injection attacks. |
| 11 | +# - This workflow's code cannot be modified by fork PRs. |
| 12 | +# ============================================================================ |
| 13 | + |
| 14 | +name: Coverage Comment |
| 15 | + |
| 16 | +on: |
| 17 | + workflow_run: |
| 18 | + workflows: ["CI/CD"] |
| 19 | + types: |
| 20 | + - completed |
| 21 | + |
| 22 | +permissions: |
| 23 | + pull-requests: write |
| 24 | + |
| 25 | +jobs: |
| 26 | + comment: |
| 27 | + name: Post Coverage Comment |
| 28 | + runs-on: ubuntu-latest |
| 29 | + # Only run when the triggering workflow was a successful PR build |
| 30 | + if: > |
| 31 | + github.event.workflow_run.event == 'pull_request' && |
| 32 | + github.event.workflow_run.conclusion == 'success' |
| 33 | +
|
| 34 | + steps: |
| 35 | + - name: Download coverage artifact |
| 36 | + uses: actions/download-artifact@v4 |
| 37 | + with: |
| 38 | + name: coverage-report |
| 39 | + path: coverage-report |
| 40 | + run-id: ${{ github.event.workflow_run.id }} |
| 41 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 42 | + |
| 43 | + # Validate and extract PR number (must be numeric only) |
| 44 | + - name: Validate and read PR number |
| 45 | + id: pr |
| 46 | + run: | |
| 47 | + PR_NUMBER=$(cat coverage-report/pr-number.txt | tr -d '[:space:]') |
| 48 | + if ! echo "$PR_NUMBER" | grep -qE '^[0-9]+$'; then |
| 49 | + echo "::error::Invalid PR number: '$PR_NUMBER'" |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | + echo "number=$PR_NUMBER" >> $GITHUB_OUTPUT |
| 53 | +
|
| 54 | + # Validate and extract coverage total (must match percentage pattern) |
| 55 | + - name: Validate and read coverage total |
| 56 | + id: coverage |
| 57 | + run: | |
| 58 | + TOTAL=$(cat coverage-report/coverage-total.txt | tr -d '[:space:]') |
| 59 | + if ! echo "$TOTAL" | grep -qE '^[0-9]+(\.[0-9]+)?%$'; then |
| 60 | + echo "::error::Invalid coverage value: '$TOTAL'" |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + echo "total=$TOTAL" >> $GITHUB_OUTPUT |
| 64 | +
|
| 65 | + # Build and post the PR comment |
| 66 | + - name: Post coverage comment |
| 67 | + env: |
| 68 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 69 | + run: | |
| 70 | + { |
| 71 | + echo "## 📊 Coverage Report" |
| 72 | + echo "" |
| 73 | + echo "**Total Coverage:** \`${{ steps.coverage.outputs.total }}\`" |
| 74 | + echo "" |
| 75 | + echo "<details>" |
| 76 | + echo "<summary>📋 Coverage Details</summary>" |
| 77 | + echo "" |
| 78 | + echo '```' |
| 79 | + cat coverage-report/coverage-summary.txt |
| 80 | + echo '```' |
| 81 | + echo "" |
| 82 | + echo "</details>" |
| 83 | + echo "" |
| 84 | + echo "---" |
| 85 | + echo "*Generated by CI workflow*" |
| 86 | + } > comment.md |
| 87 | +
|
| 88 | + gh pr comment ${{ steps.pr.outputs.number }} \ |
| 89 | + --repo "${{ github.repository }}" \ |
| 90 | + --body-file comment.md |
0 commit comments