Skip to content

Commit d31f18e

Browse files
committed
ci: Use native Actions checks instead of manual statuses for coverage report
Removes the need for using the GITHUB_TOKEN when reporting the coverage percentage (as a manual commit status). Instead, use native checks through a dedicated CI job. Caveat is that the actual percentage is not shown in the check/status description anymore. We will have to rely on the job summary. Also, add a threshold below which coverage job fails (so that we may not need to constantly check that value anymore)
1 parent 8a1e7e7 commit d31f18e

3 files changed

Lines changed: 75 additions & 111 deletions

File tree

.github/workflows/ci.yml

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ on:
88

99
permissions:
1010
contents: read
11-
statuses: write
1211

1312
jobs:
1413
check-pr-labels:
@@ -69,25 +68,62 @@ jobs:
6968
- name: Run all checks and rebuild artifacts
7069
run: npm run all
7170

71+
- name: Upload coverage summary
72+
uses: actions/upload-artifact@v4
73+
with:
74+
name: coverage-summary
75+
path: coverage/coverage-summary.json
76+
if-no-files-found: error
77+
7278
- name: Check for clean working directory
7379
run: npm run check:clean
7480

81+
coverage:
82+
name: Coverage
83+
runs-on: ubuntu-latest
84+
needs: tests
85+
env:
86+
COVERAGE_THRESHOLD: '90'
87+
88+
steps:
89+
- uses: actions/checkout@v6
90+
91+
- uses: ./.github/actions/common-setup
92+
93+
# Downloads the artifact named below from this workflow run; `needs: tests` ensures that upload finished first.
94+
- name: Download coverage summary
95+
uses: actions/download-artifact@v4
96+
with:
97+
name: coverage-summary
98+
path: coverage
99+
75100
- name: Generate coverage summary
76101
id: coverage
77102
run: |
78-
COVERAGE=$(npx tsx src/scripts/coverage-summary.ts)
103+
COVERAGE=$(npm run --silent coverage)
79104
echo "pct=$COVERAGE" >> "$GITHUB_OUTPUT"
80105
81-
- name: Set coverage status
82-
uses: actions/github-script@v7
83-
with:
84-
script: |
85-
const pct = '${{ steps.coverage.outputs.pct }}';
86-
await github.rest.repos.createCommitStatus({
87-
owner: context.repo.owner,
88-
repo: context.repo.repo,
89-
sha: context.sha,
90-
state: 'success',
91-
description: `${pct}%`,
92-
context: 'Coverage',
93-
});
106+
- name: Enforce coverage threshold
107+
env:
108+
COVERAGE_PCT: ${{ steps.coverage.outputs.pct }}
109+
COVERAGE_MIN: ${{ env.COVERAGE_THRESHOLD }}
110+
run: |
111+
node --input-type=module <<'EOF'
112+
const pct = Number(process.env.COVERAGE_PCT)
113+
const min = Number(process.env.COVERAGE_MIN)
114+
115+
if (Number.isNaN(pct)) {
116+
throw new Error(`Invalid coverage percentage: ${process.env.COVERAGE_PCT}`)
117+
}
118+
119+
if (pct < min) {
120+
console.error(
121+
`Coverage ${pct.toFixed(2)}% is below required ${min.toFixed(0)}%`,
122+
)
123+
process.exit(1)
124+
}
125+
126+
console.log(
127+
`Coverage ${pct.toFixed(2)}% meets required ${min.toFixed(0)}%`,
128+
)
129+
EOF

dist/chunks/common.js

Lines changed: 18 additions & 95 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/scripts/coverage-summary.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,23 @@ if (!total?.statements?.pct && total?.statements?.pct !== 0) {
2828
}
2929

3030
const pct = total.statements.pct
31+
const threshold = Number(process.env.COVERAGE_THRESHOLD ?? '90')
32+
const meetsThreshold = pct >= threshold
3133

3234
// Print coverage percentage for CI to capture
3335
console.log(pct.toFixed(2))
3436

3537
// Write GitHub Actions job summary if running in CI
3638
const summaryFile = process.env.GITHUB_STEP_SUMMARY
3739
if (summaryFile) {
38-
const emoji = pct >= 90 ? '🟢' : pct >= 80 ? '🟡' : pct >= 70 ? '🟠' : '🔴'
40+
const emoji = meetsThreshold ? '🟢' : '🔴'
41+
const status = meetsThreshold ? 'meets' : 'is below'
3942

4043
const summary = [
4144
`## ${emoji} Code Coverage: ${pct.toFixed(2)}%`,
4245
'',
46+
`Coverage ${status} the ${threshold.toFixed(0)}% threshold.`,
47+
'',
4348
'| Metric | Coverage | Covered | Total |',
4449
'| --- | --- | --- | --- |',
4550
`| Statements | ${total.statements.pct?.toFixed(2)}% | ${total.statements.covered} | ${total.statements.total} |`,

0 commit comments

Comments
 (0)