Repository navigation
90 lines (79 loc) · 3.38 KB
/
Copy pathtest-coverage.yml
File metadata and controls
90 lines (79 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
name: Test Coverage
on:
pull_request:
types: [opened, synchronize, reopened]
push:
branches: [main, dev, stg, 'backport/public-wave-**']
# backport/public-wave-** — same reason as ci.yml (see its comment): a
# deploy-key push from auto-backport.yml lands here as a real push event,
# which is what lets this job's check satisfy the dev branch ruleset (a
# workflow_dispatch re-drive cannot, per GitHub's docs, which is why this
# workflow no longer has that trigger). The only event-conditional step
# (the PR comment) already gates on github.event_name == 'pull_request'
# and simply skips for a push, wave branch or otherwise.
permissions:
contents: read
pull-requests: write
jobs:
coverage:
name: Coverage (>= 80%)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Setup Node.js
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: 22
cache: 'npm'
- run: npm ci
- name: Run unit tests with coverage
run: npm run test:coverage
env:
CI: true
- name: Enforce 80% line coverage minimum
run: |
if [ -f coverage/coverage-summary.json ]; then
LINES_PCT=$(jq -r '.total.lines.pct' coverage/coverage-summary.json)
echo "Line coverage: ${LINES_PCT}%"
if (( $(echo "$LINES_PCT < 80" | bc -l) )); then
echo "::error::Line coverage ${LINES_PCT}% is below the 80% minimum threshold"
exit 1
fi
else
echo "::error::Coverage report not generated"
exit 1
fi
- name: Generate Coverage Summary
id: coverage-summary
if: always()
run: |
if [ -f coverage/coverage-summary.json ]; then
TOTAL_LINES=$(jq -r '.total.lines.pct' coverage/coverage-summary.json)
TOTAL_STATEMENTS=$(jq -r '.total.statements.pct' coverage/coverage-summary.json)
TOTAL_FUNCTIONS=$(jq -r '.total.functions.pct' coverage/coverage-summary.json)
TOTAL_BRANCHES=$(jq -r '.total.branches.pct' coverage/coverage-summary.json)
{
echo "## Test Coverage Report"
echo ""
echo "| Metric | Coverage |"
echo "|--------|----------|"
echo "| Lines | ${TOTAL_LINES}% |"
echo "| Statements | ${TOTAL_STATEMENTS}% |"
echo "| Functions | ${TOTAL_FUNCTIONS}% |"
echo "| Branches | ${TOTAL_BRANCHES}% |"
} | tee -a "$GITHUB_STEP_SUMMARY" > coverage-report.md
echo "coverage_generated=true" >> "$GITHUB_OUTPUT"
else
echo "## Coverage Report Not Available" >> "$GITHUB_STEP_SUMMARY"
echo "Coverage data was not generated. Check the test run for errors." >> "$GITHUB_STEP_SUMMARY"
echo "coverage_generated=false" >> "$GITHUB_OUTPUT"
fi
- name: Add Coverage PR Comment
uses: marocchino/sticky-pull-request-comment@5770ad5eb8f42dd2c4f34da00c94c5381e49af88 # v3.0.5
if: github.event_name == 'pull_request' && steps.coverage-summary.outputs.coverage_generated == 'true'
with:
recreate: true
path: coverage-report.md
continue-on-error: true