-
Notifications
You must be signed in to change notification settings - Fork 27
226 lines (193 loc) · 7.85 KB
/
coverage.yml
File metadata and controls
226 lines (193 loc) · 7.85 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
name: Code Coverage
on:
push:
branches: [main]
paths:
- 'server/src/**'
- 'client/src/**'
- 'coverage.sh'
- '.github/workflows/coverage.yml'
pull_request:
paths:
- 'server/src/**'
- 'client/src/**'
- 'coverage.sh'
- '.github/workflows/coverage.yml'
permissions:
contents: read
pages: write
id-token: write
pull-requests: write
concurrency:
group: coverage-${{ github.ref }}
cancel-in-progress: true
jobs:
coverage:
name: Generate Coverage Report
runs-on: ubuntu-latest
if: github.event_name != 'pull_request' || (github.event.pull_request.user.login != 'gnometranslations' && github.event.pull_request.user.login != 'dependabot[bot]')
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: llvm-tools-preview
- name: Setup Rust cache
uses: Swatinem/rust-cache@v2
- name: Install cargo-tarpaulin
uses: taiki-e/install-action@v2
with:
tool: cargo-tarpaulin
- name: Install grcov for merging coverage
uses: taiki-e/install-action@v2
with:
tool: grcov
- name: Run coverage script
run: |
chmod +x coverage.sh
./coverage.sh
- name: Prepare coverage for deployment
run: |
mkdir -p site/coverage
cp -r coverage/* site/coverage/
# Extract coverage percentage from the generated JSON file
CURRENT_COVERAGE=$(jq -r '.message' site/coverage/html/coverage.json | sed 's/%//')
echo "CURRENT_COVERAGE=$CURRENT_COVERAGE" >> $GITHUB_ENV
echo "Combined coverage: $CURRENT_COVERAGE%"
- name: Generate coverage summary for PR comparison
if: github.event_name == 'pull_request'
run: |
echo "PR_COVERAGE=$CURRENT_COVERAGE" >> $GITHUB_ENV
echo "PR coverage: $CURRENT_COVERAGE%"
- name: Store coverage percentage for main branch
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: |
echo "Main branch coverage: $CURRENT_COVERAGE%"
# Store in a simple text file artifact for PR comparisons
mkdir -p coverage-baseline
echo "$CURRENT_COVERAGE" > coverage-baseline/main-coverage.txt
# Generate coverage badge
COVERAGE_INT=$(echo "$CURRENT_COVERAGE" | cut -d. -f1)
if [ "$COVERAGE_INT" -ge 80 ]; then
COLOR="brightgreen"
elif [ "$COVERAGE_INT" -ge 60 ]; then
COLOR="yellow"
else
COLOR="red"
fi
# Create badge SVG using shields.io format
curl -s "https://img.shields.io/badge/coverage-${CURRENT_COVERAGE}%25-${COLOR}" > site/coverage/badge.svg
- name: Upload baseline coverage
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
uses: actions/upload-artifact@v7
with:
name: coverage-baseline
path: coverage-baseline/main-coverage.txt
retention-days: 90
- name: Get main branch coverage for comparison
if: github.event_name == 'pull_request'
uses: actions/github-script@v9
with:
script: |
// Find the latest successful coverage run on main that has the baseline artifact
const { data: runs } = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: 'coverage.yml',
status: 'completed',
conclusion: 'success',
branch: 'main',
per_page: 10
});
for (const run of runs.workflow_runs) {
try {
const { data: artifacts } = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: run.id
});
const baselineArtifact = artifacts.artifacts.find(a => a.name === 'coverage-baseline');
if (baselineArtifact) {
core.exportVariable('BASELINE_RUN_ID', run.id.toString());
console.log(`Found baseline artifact in run ${run.id}`);
return;
}
} catch (error) {
console.log(`Error checking run ${run.id}: ${error.message}`);
}
}
console.log('No baseline artifact found in recent main branch runs');
core.exportVariable('BASELINE_RUN_ID', '');
- name: Download baseline artifact
if: github.event_name == 'pull_request' && env.BASELINE_RUN_ID != ''
uses: actions/download-artifact@v8
with:
name: coverage-baseline
path: coverage-baseline/
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ env.BASELINE_RUN_ID }}
continue-on-error: true
- name: Set main coverage for comparison
if: github.event_name == 'pull_request'
run: |
if [ -f coverage-baseline/main-coverage.txt ]; then
MAIN_COVERAGE=$(cat coverage-baseline/main-coverage.txt)
echo "MAIN_COVERAGE=$MAIN_COVERAGE" >> $GITHUB_ENV
echo "Using stored main branch coverage: $MAIN_COVERAGE%"
else
echo "MAIN_COVERAGE=0.00" >> $GITHUB_ENV
echo "No baseline coverage found, using 0.00%"
fi
- name: Post coverage comment on PR
if: github.event_name == 'pull_request'
uses: actions/github-script@v9
with:
script: |
const prCoverage = parseFloat(process.env.PR_COVERAGE || '0.00');
const mainCoverage = parseFloat(process.env.MAIN_COVERAGE || '0.00');
const diff = prCoverage - mainCoverage;
const diffText = diff > 0 ? `+${diff.toFixed(2)}%` : `${diff.toFixed(2)}%`;
const emoji = diff > 0 ? '📈' : diff < 0 ? '📉' : '➡️';
const body = `## 📊 Code Coverage Report
| Metric | Value |
|--------|-------|
| **Current PR Coverage** | ${prCoverage.toFixed(2)}% |
| **Main Branch Coverage** | ${mainCoverage.toFixed(2)}% |
| **Coverage Change** | ${emoji} ${diffText} |
${diff < -1 ? '⚠️ **Warning**: Coverage decreased by more than 1%' : ''}
${diff > 1 ? '🎉 **Great**: Coverage increased by more than 1%!' : ''}
*Coverage report generated by [cargo-tarpaulin](https://github.com/xd009642/tarpaulin)*`;
// Find existing coverage comment
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const existingComment = comments.find(comment =>
comment.body.includes('## 📊 Code Coverage Report')
);
if (existingComment) {
// Update existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: body
});
} else {
// Create new comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body
});
}
- name: Upload coverage artifacts
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
uses: actions/upload-artifact@v7
with:
name: coverage-reports
path: site/coverage/html/
retention-days: 30