Repository navigation
217 lines (193 loc) · 6.45 KB
/
Copy pathpull-request-tests.yml
File metadata and controls
217 lines (193 loc) · 6.45 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
name: Pull Request Tests
on:
pull_request:
branches:
- master
workflow_dispatch:
permissions:
contents: read
checks: write
pull-requests: write
issues: write
env:
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
jobs:
backend-tests:
name: Backend Tests
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
env:
POSTGRES_DB: nlf_db
POSTGRES_USER: nlf_user
POSTGRES_PASSWORD: nlf_user
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
DB_HOST: localhost
DB_PORT: 5432
DB_NAME: nlf_db
DB_USER: nlf_user
DB_PASSWORD: nlf_user
outputs:
total: ${{ steps.backend-coverage.outputs.total }}
defaults:
run:
working-directory: backend
steps:
- name: Checkout repository
uses: actions/checkout@v5
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.11'
cache: pip
cache-dependency-path: backend/requirements.txt
- name: Install backend dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run migrations
run: |
alembic upgrade head
- name: Run backend unit tests
run: |
mkdir -p test-results coverage
pytest tests -v \
--cov=app \
--cov-report=term-missing \
--cov-report=html:coverage/html \
--cov-report=json:coverage/coverage.json \
--junitxml=test-results/backend-pytest.xml
- name: Publish backend test results
if: always()
uses: EnricoMi/publish-unit-test-result-action@v2
with:
files: backend/test-results/backend-pytest.xml
check_name: Backend Test Results
- name: Upload backend coverage report
if: always()
uses: actions/upload-artifact@v6
with:
name: backend-coverage-report
path: backend/coverage/
retention-days: 14
- name: Save backend coverage summary
if: always()
id: backend-coverage
run: |
if [ -f coverage/coverage.json ]; then
TOTAL=$(python -c "import json; d=json.load(open('coverage/coverage.json')); print(round(d['totals']['percent_covered'],1))")
echo "total=${TOTAL}" >> $GITHUB_OUTPUT
else
echo "total=n/a" >> $GITHUB_OUTPUT
fi
frontend-tests:
name: Frontend Tests
runs-on: ubuntu-latest
outputs:
total: ${{ steps.frontend-coverage.outputs.total }}
defaults:
run:
working-directory: frontend
steps:
- name: Checkout repository
uses: actions/checkout@v5
- name: Set up Node.js
uses: actions/setup-node@v6
with:
node-version: 'lts/*'
cache: npm
cache-dependency-path: frontend/package-lock.json
- name: Install frontend dependencies
run: npm ci --legacy-peer-deps
- name: Run frontend unit tests
run: |
mkdir -p test-results
npx vitest run \
--reporter=default \
--reporter=junit \
--outputFile=test-results/frontend-vitest.xml \
--coverage
- name: Publish frontend test results
if: always()
uses: EnricoMi/publish-unit-test-result-action@v2
with:
files: frontend/test-results/frontend-vitest.xml
check_name: Frontend Test Results
- name: Upload frontend coverage report
if: always()
uses: actions/upload-artifact@v6
with:
name: frontend-coverage-report
path: frontend/coverage/
retention-days: 14
- name: Save frontend coverage summary
if: always()
id: frontend-coverage
run: |
if [ -f coverage/coverage-summary.json ]; then
TOTAL=$(node -e "const d=require('./coverage/coverage-summary.json'); console.log(d.total.lines.pct)")
echo "total=${TOTAL}" >> $GITHUB_OUTPUT
else
echo "total=n/a" >> $GITHUB_OUTPUT
fi
pr-summary:
name: PR Summary
runs-on: ubuntu-latest
needs: [backend-tests, frontend-tests]
if: >
always() &&
github.event_name == 'pull_request'
steps:
- name: Write PR summary comment
if: github.event.pull_request.head.repo.full_name == github.repository
uses: actions/github-script@v7
with:
script: |
const backendResult = '${{ needs.backend-tests.result }}';
const frontendResult = '${{ needs.frontend-tests.result }}';
const backendCov = '${{ needs.backend-tests.outputs.total }}';
const frontendCov = '${{ needs.frontend-tests.outputs.total }}';
const icon = r => r === 'success' ? '✅' : r === 'skipped' ? '⏭️' : '❌';
const fmt = v => (!v || v === 'n/a') ? 'n/a' : `${v}%`;
const body = [
'## Test Results',
'',
'| Suite | Status | Coverage |',
'|-------|--------|----------|',
`| Backend (pytest) | ${icon(backendResult)} ${backendResult} | ${fmt(backendCov)} |`,
`| Frontend (vitest) | ${icon(frontendResult)} ${frontendResult} | ${fmt(frontendCov)} |`,
'',
'_Coverage HTML reports are available as workflow artifacts (14 days)._',
].join('\n');
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const existing = comments.find(c =>
c.user.login === 'github-actions[bot]' &&
c.body.startsWith('## Test Results')
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}