-
Notifications
You must be signed in to change notification settings - Fork 3
219 lines (190 loc) · 8.71 KB
/
coverage.yml
File metadata and controls
219 lines (190 loc) · 8.71 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
name: Coverage Report
on:
pull_request:
push:
branches: [main]
jobs:
coverage:
name: Generate Coverage Report
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read # To checkout the code
pull-requests: write # To comment on PRs
issues: write # To comment on issues (PRs are issues)
actions: read # To read workflow artifacts
steps:
- uses: actions/checkout@v4
with:
# Fetch full history for diff coverage analysis
fetch-depth: 0
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
enable-cache: true
- name: Set up Python
run: uv python install 3.13
- name: Install the project
run: uv sync --all-extras --dev
- name: Run coverage
run: |
uv run pytest -n auto --cov=. --cov-report=term-missing
uv run coverage report
uv run coverage html
uv run coverage xml
- name: Upload coverage HTML report as artifact
uses: actions/upload-artifact@v4
id: upload-html-coverage
with:
name: coverage-report-html
path: htmlcov/
retention-days: 30
- name: Upload coverage XML for analysis
uses: actions/upload-artifact@v4
with:
name: coverage-report-xml
path: coverage.xml
retention-days: 30
- name: Generate diff coverage report (PR only)
if: github.event_name == 'pull_request'
run: |
# Generate diff coverage reports
uv run diff-cover coverage.xml --compare-branch=origin/${{ github.event.pull_request.base.ref }} --format markdown:diff-coverage.md
uv run diff-cover coverage.xml --compare-branch=origin/${{ github.event.pull_request.base.ref }} --format html:diff-coverage.html
# Also generate a fail-under report for potential future use
uv run diff-cover coverage.xml --compare-branch=origin/${{ github.event.pull_request.base.ref }} --fail-under=80 || echo "Diff coverage below 80%"
- name: Upload diff coverage report
if: github.event_name == 'pull_request'
uses: actions/upload-artifact@v4
id: upload-diff-coverage
with:
name: diff-coverage-report
path: |
diff-coverage.html
diff-coverage.md
retention-days: 30
- name: Comment PR with coverage info
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
continue-on-error: true # Don't fail the workflow if we can't comment (e.g., from forks)
with:
script: |
const fs = require('fs');
// Read diff coverage markdown if it exists
let diffCoverageContent = '';
try {
const fullDiffCoverage = fs.readFileSync('diff-coverage.md', 'utf8');
// Extract only the high-level sections (header, diff info, and summary)
// Stop before the detailed file-by-file breakdown
const lines = fullDiffCoverage.split('\n');
const summaryLines = [];
let inDetailedSection = false;
let detailedSectionLines = [];
for (const line of lines) {
if (inDetailedSection) {
detailedSectionLines.push(line);
continue;
}
// Skip giant redundant header
if (line === '# Diff Coverage') {
continue;
}
// Stop when we hit a file-specific section (## followed by a file path)
// But keep the sections we want: "## Diff:" and "## Summary"
if (line.startsWith('## ') &&
!line.startsWith('## Diff:') &&
!line.startsWith('## Summary')) {
inDetailedSection = true;
detailedSectionLines.push(line);
continue;
}
// Clean up the diff line to be more concise
let cleanedLine = line;
if (line.startsWith('## Diff: ') && line.includes(', staged and unstaged changes')) {
// Extract just the branch comparison part
const match = line.match(/## (.+?), staged and unstaged changes/);
if (match) {
cleanedLine = `## ${match[1]}`;
}
}
summaryLines.push(cleanedLine);
}
diffCoverageContent = summaryLines.join('\n').trim();
// Github PR comment limit is 65536 characters - we truncate the detailed section to fit
let detailedSectionString = detailedSectionLines.join('\n');
const detailedSectionLinesMaxChars = 65536 - diffCoverageContent.length - 10000; // 10000 is arbitrary slack to make sure it fits
if (detailedSectionString.length > detailedSectionLinesMaxChars) {
detailedSectionString = detailedSectionString.slice(0, detailedSectionLinesMaxChars) + '\n\n... (truncated - see full report for details) ...';
}
if (detailedSectionLines.length > 0) {
diffCoverageContent += `\n\n## Line-by-line\n\n<details>\n<summary>View line-by-line diff coverage</summary>\n\n${detailedSectionString}\n</details>`;
}
} catch (error) {
console.log('No diff coverage report found');
}
// Get overall coverage percentage
const {
execSync
} = require('child_process');
let coveragePercentage = '';
try {
const output = execSync('uv run coverage report --format=text', {
encoding: 'utf8'
});
const match = output.match(/TOTAL\s+\d+\s+\d+\s+(\d+%)/);
if (match) {
coveragePercentage = match[1];
}
} catch (error) {
console.log('Could not extract coverage percentage');
}
let body = '## 📊 Coverage Report\n\n';
body += `**Overall Coverage:** ${coveragePercentage || 'Unknown'}\n\n`;
body += `${diffCoverageContent || 'No diff coverage data available'}\n\n`;
body += '---\n';
body += '- [📊 HTML Coverage Report](${{ steps.upload-html-coverage.outputs.artifact-url }}) - Interactive coverage report\n';
if ('${{ github.event_name }}' === 'pull_request') {
body += '- [📈 Diff Coverage Report](${{ steps.upload-diff-coverage.outputs.artifact-url }}) - Detailed diff analysis\n';
}
body += '- [Github Actions Run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) - View the full coverage report';
// 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('📊 Coverage Report')
);
try {
if (existingComment) {
// Update existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: body
});
console.log('Updated existing coverage comment');
} else {
// Create new comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body
});
console.log('Created new coverage comment');
}
} catch (error) {
console.log('Failed to comment on PR (likely due to permissions from fork):', error.message);
console.log('Coverage reports are still available as artifacts');
}
- name: Coverage Summary
run: |
echo "## Coverage Summary" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
uv run coverage report >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY