-
Notifications
You must be signed in to change notification settings - Fork 0
285 lines (239 loc) · 9.2 KB
/
pr-preview.yml
File metadata and controls
285 lines (239 loc) · 9.2 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# ==============================================================================
# PR Preview — Build, Test & Report on Pull Requests
# Runs full CI suite on PRs and posts results as a PR comment.
# Does NOT deploy — preview only.
# ==============================================================================
name: PR Preview
on:
pull_request:
branches: [main]
types: [opened, synchronize, reopened]
concurrency:
group: pr-preview-${{ github.event.pull_request.number }}
cancel-in-progress: true
permissions:
contents: read
pull-requests: write
checks: write
jobs:
# --------------------------------------------------------------------------
# Run tests with coverage
# --------------------------------------------------------------------------
test:
name: Tests & Coverage
runs-on: ubuntu-latest
outputs:
coverage: ${{ steps.cov.outputs.coverage }}
tests-passed: ${{ steps.results.outputs.passed }}
tests-failed: ${{ steps.results.outputs.failed }}
tests-total: ${{ steps.results.outputs.total }}
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Cache pip dependencies
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: pip-pr-${{ runner.os }}-py3.11-${{ hashFiles('pyproject.toml', 'setup.cfg', 'requirements*.txt') }}
restore-keys: |
pip-pr-${{ runner.os }}-py3.11-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e ".[dev]"
- name: Run tests with coverage
id: test-run
env:
RATE_LIMIT_ENABLED: "false"
run: |
pytest tests/ -v --tb=short \
--cov=src \
--cov-report=term-missing \
--cov-report=xml:coverage.xml \
--cov-report=html:htmlcov/ \
--junitxml=test-results.xml \
2>&1 | tee test-output.txt
echo "exit_code=${PIPESTATUS[0]}" >> "$GITHUB_OUTPUT"
- name: Extract coverage percentage
id: cov
if: always()
run: |
COVERAGE=$(python -m coverage report --format=total 2>/dev/null || echo "0")
echo "coverage=${COVERAGE}" >> "$GITHUB_OUTPUT"
- name: Parse test results
id: results
if: always()
run: |
if [ -f test-results.xml ]; then
TOTAL=$(python -c "
import xml.etree.ElementTree as ET
tree = ET.parse('test-results.xml')
root = tree.getroot()
print(root.attrib.get('tests', '0'))
" 2>/dev/null || echo "0")
FAILURES=$(python -c "
import xml.etree.ElementTree as ET
tree = ET.parse('test-results.xml')
root = tree.getroot()
f = int(root.attrib.get('failures', '0'))
e = int(root.attrib.get('errors', '0'))
print(f + e)
" 2>/dev/null || echo "0")
PASSED=$((TOTAL - FAILURES))
echo "total=${TOTAL}" >> "$GITHUB_OUTPUT"
echo "passed=${PASSED}" >> "$GITHUB_OUTPUT"
echo "failed=${FAILURES}" >> "$GITHUB_OUTPUT"
else
echo "total=0" >> "$GITHUB_OUTPUT"
echo "passed=0" >> "$GITHUB_OUTPUT"
echo "failed=0" >> "$GITHUB_OUTPUT"
fi
- name: Upload coverage artifact
if: always()
uses: actions/upload-artifact@v4
with:
name: pr-coverage-${{ github.event.pull_request.number }}
path: |
coverage.xml
htmlcov/
test-results.xml
retention-days: 7
# --------------------------------------------------------------------------
# Lint check
# --------------------------------------------------------------------------
lint:
name: Lint Check
runs-on: ubuntu-latest
outputs:
status: ${{ steps.lint-run.outputs.status }}
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Cache pip dependencies
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: pip-pr-lint-${{ runner.os }}-py3.11-${{ hashFiles('pyproject.toml', 'setup.cfg', 'requirements*.txt') }}
restore-keys: |
pip-pr-lint-${{ runner.os }}-py3.11-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e ".[dev]"
- name: Run lint
id: lint-run
run: |
if ruff check src/ tests/ && ruff format --check src/ tests/; then
echo "status=passed" >> "$GITHUB_OUTPUT"
else
echo "status=failed" >> "$GITHUB_OUTPUT"
exit 1
fi
# --------------------------------------------------------------------------
# Security quick scan
# --------------------------------------------------------------------------
security:
name: Security Quick Scan
runs-on: ubuntu-latest
outputs:
status: ${{ steps.sec.outputs.status }}
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install and scan
id: sec
run: |
python -m pip install --upgrade pip
pip install -e "."
pip install pip-audit bandit
ISSUES=0
pip-audit --strict 2>/dev/null || ISSUES=1
bandit -r src/ -s B101,B601 -q 2>/dev/null || ISSUES=1
if [ "$ISSUES" -eq 0 ]; then
echo "status=passed" >> "$GITHUB_OUTPUT"
else
echo "status=warnings" >> "$GITHUB_OUTPUT"
fi
# --------------------------------------------------------------------------
# Post PR comment with results
# --------------------------------------------------------------------------
comment:
name: Post PR Comment
runs-on: ubuntu-latest
needs: [test, lint, security]
if: always()
permissions:
pull-requests: write
steps:
- name: Post results comment
uses: actions/github-script@v7
with:
script: |
const coverage = '${{ needs.test.outputs.coverage }}' || '0';
const testsPassed = '${{ needs.test.outputs.tests-passed }}' || '0';
const testsFailed = '${{ needs.test.outputs.tests-failed }}' || '0';
const testsTotal = '${{ needs.test.outputs.tests-total }}' || '0';
const lintStatus = '${{ needs.lint.outputs.status }}' || 'unknown';
const securityStatus = '${{ needs.security.outputs.status }}' || 'unknown';
const testResult = '${{ needs.test.result }}';
const lintResult = '${{ needs.lint.result }}';
// Coverage badge color
const covNum = parseInt(coverage) || 0;
let covColor = 'red';
if (covNum >= 80) covColor = 'brightgreen';
else if (covNum >= 60) covColor = 'yellow';
else if (covNum >= 40) covColor = 'orange';
// Status emojis
const testEmoji = testResult === 'success' ? '' : '';
const lintEmoji = lintResult === 'success' ? '' : '';
const secEmoji = securityStatus === 'passed' ? '' : '';
const body = `## PR Preview Results

| Check | Status | Details |
|-------|--------|---------|
| ${testEmoji} Tests | **${testResult}** | ${testsPassed}/${testsTotal} passed, ${testsFailed} failed |
| ${lintEmoji} Lint | **${lintResult}** | ruff check + format |
| ${secEmoji} Security | **${securityStatus}** | pip-audit + bandit |
### Coverage: ${coverage}%
<details>
<summary>Details</summary>
- **Passed:** ${testsPassed}
- **Failed:** ${testsFailed}
- **Total:** ${testsTotal}
- **Coverage:** ${coverage}%
</details>
---
*Commit: \`${context.sha.substring(0, 8)}\` | [View workflow run](${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId})*`;
// Find existing comment to update
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(c =>
c.user.type === 'Bot' && c.body.includes('PR Preview Results')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body,
});
}