|
| 1 | +name: Coverage Comment |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: [C++ CI] |
| 6 | + types: [completed] |
| 7 | + |
| 8 | +permissions: |
| 9 | + actions: read |
| 10 | + contents: read |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: coverage-comment-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }} |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +jobs: |
| 18 | + comment: |
| 19 | + if: ${{ github.event.workflow_run.event == 'pull_request' }} |
| 20 | + runs-on: ubuntu-latest |
| 21 | + steps: |
| 22 | + - name: Download coverage summary |
| 23 | + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 |
| 24 | + with: |
| 25 | + name: coverage-report |
| 26 | + path: coverage-data |
| 27 | + repository: ${{ github.repository }} |
| 28 | + run-id: ${{ github.event.workflow_run.id }} |
| 29 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 30 | + |
| 31 | + - name: Create or update coverage comment |
| 32 | + uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7 |
| 33 | + env: |
| 34 | + LINE_COVERAGE_THRESHOLD: '80' |
| 35 | + FUNCTION_COVERAGE_THRESHOLD: '80' |
| 36 | + BRANCH_COVERAGE_THRESHOLD: '70' |
| 37 | + with: |
| 38 | + script: | |
| 39 | + const fs = require('fs'); |
| 40 | + const summary = JSON.parse(fs.readFileSync('coverage-data/summary.json', 'utf8')); |
| 41 | +
|
| 42 | + function readMetric(prefix, label, threshold) { |
| 43 | + const percent = Number(summary[`${prefix}_percent`]); |
| 44 | + const covered = Number(summary[`${prefix}_covered`]); |
| 45 | + const total = Number(summary[`${prefix}_total`]); |
| 46 | + const valid = Number.isFinite(percent) |
| 47 | + && Number.isInteger(covered) |
| 48 | + && Number.isInteger(total) |
| 49 | + && percent >= 0 |
| 50 | + && percent <= 100 |
| 51 | + && covered >= 0 |
| 52 | + && total >= covered; |
| 53 | +
|
| 54 | + if (!valid) { |
| 55 | + throw new Error(`Invalid ${label} coverage summary`); |
| 56 | + } |
| 57 | +
|
| 58 | + return { label, percent, covered, total, threshold }; |
| 59 | + } |
| 60 | +
|
| 61 | + const metrics = [ |
| 62 | + readMetric('line', 'Lines', Number(process.env.LINE_COVERAGE_THRESHOLD)), |
| 63 | + readMetric('function', 'Functions', Number(process.env.FUNCTION_COVERAGE_THRESHOLD)), |
| 64 | + readMetric('branch', 'Branches', Number(process.env.BRANCH_COVERAGE_THRESHOLD)), |
| 65 | + ]; |
| 66 | + const rows = metrics.map((metric) => { |
| 67 | + const status = metric.percent >= metric.threshold ? 'PASS' : 'FAIL'; |
| 68 | + return `| ${status} | ${metric.label} | ${metric.percent.toFixed(2)}% (${metric.threshold}% target) | ${metric.covered} / ${metric.total} |`; |
| 69 | + }); |
| 70 | + const run = context.payload.workflow_run; |
| 71 | + const associatedPulls = await github.paginate( |
| 72 | + github.rest.repos.listPullRequestsAssociatedWithCommit, |
| 73 | + { |
| 74 | + ...context.repo, |
| 75 | + commit_sha: run.head_sha, |
| 76 | + per_page: 100, |
| 77 | + }, |
| 78 | + ); |
| 79 | + const pull = associatedPulls.find((candidate) => |
| 80 | + candidate.state === 'open' && candidate.head.sha === run.head_sha); |
| 81 | + if (!pull) { |
| 82 | + core.notice(`No open pull request currently points to ${run.head_sha}`); |
| 83 | + return; |
| 84 | + } |
| 85 | +
|
| 86 | + const marker = '<!-- cpp-coverage-report -->'; |
| 87 | + const body = [ |
| 88 | + marker, |
| 89 | + '## Coverage Report', |
| 90 | + '', |
| 91 | + '| Status | Category | Percentage | Covered / Total |', |
| 92 | + '|:--:|---|---:|---:|', |
| 93 | + ...rows, |
| 94 | + '', |
| 95 | + `[Generated in workflow #${run.run_number}](${run.html_url}) for commit \`${run.head_sha.slice(0, 7)}\` by the C++ coverage workflow.`, |
| 96 | + ].join('\n'); |
| 97 | +
|
| 98 | + const issue_number = pull.number; |
| 99 | + const comments = await github.paginate(github.rest.issues.listComments, { |
| 100 | + ...context.repo, |
| 101 | + issue_number, |
| 102 | + per_page: 100, |
| 103 | + }); |
| 104 | + const previous = comments.find((comment) => |
| 105 | + comment.user.type === 'Bot' && comment.body.includes(marker)); |
| 106 | +
|
| 107 | + if (previous) { |
| 108 | + await github.rest.issues.updateComment({ |
| 109 | + ...context.repo, |
| 110 | + comment_id: previous.id, |
| 111 | + body, |
| 112 | + }); |
| 113 | + } else { |
| 114 | + await github.rest.issues.createComment({ |
| 115 | + ...context.repo, |
| 116 | + issue_number, |
| 117 | + body, |
| 118 | + }); |
| 119 | + } |
0 commit comments