-
Notifications
You must be signed in to change notification settings - Fork 0
353 lines (303 loc) · 13.2 KB
/
Copy pathpr-validation.yml
File metadata and controls
353 lines (303 loc) · 13.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
name: PR Validation
on:
pull_request:
branches: [master]
types: [opened, synchronize, reopened]
concurrency:
group: pr-${{ github.event.pull_request.number }}
cancel-in-progress: true
permissions:
contents: write
pull-requests: write
jobs:
validate:
name: Validate Pull Request
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Comment in progress
uses: actions/github-script@v9
with:
script: |
const marker = '<!-- pr-validation-bot -->';
const body = `${marker}
⏳ **PR Validation IN PROGRESS**
**Commit:** \`${{ github.event.pull_request.head.sha }}\`
**Branch:** \`${{ github.head_ref }}\`
**Checks:**
- ⏳ Dependencies
- ⏳ Linting
- ⏳ Format
- ⏳ Build
- ⏳ Docker build
- ⏳ E2E tests
---
🔗 [View workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
⏰ Started at: \`${new Date().toISOString()}\``;
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(marker)
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body,
});
}
- name: Checkout PR code
uses: actions/checkout@v7
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v7
with:
node-version: '22'
cache: 'npm'
- name: Install dependencies
id: install
run: npm ci
- name: Lint code
id: lint
if: always() && steps.install.outcome == 'success'
run: npm run lint
- name: Format check
id: format
if: always() && steps.install.outcome == 'success'
run: npm run format:check
- name: Build project
id: build
if: always() && steps.install.outcome == 'success'
run: npm run build
- name: Validate Dockerfile
id: docker
if: always() && steps.install.outcome == 'success'
run: docker build -t diffractwd-com:pr-${{ github.event.pull_request.number }} .
- name: Install Playwright browsers
if: always() && steps.build.outcome == 'success'
run: npx playwright install --with-deps chromium
- name: Extract base branch screenshots
if: always() && steps.build.outcome == 'success'
run: |
mkdir -p /tmp/screenshots-base
BASE_SHA="${{ github.event.pull_request.base.sha }}"
git show "${BASE_SHA}":e2e/screenshots/ 2>/dev/null | \
grep '\.png$' | while read f; do
git show "${BASE_SHA}:e2e/screenshots/${f}" > "/tmp/screenshots-base/${f}" 2>/dev/null || true
done
echo "Base screenshots:"
ls /tmp/screenshots-base/ 2>/dev/null || echo "None found"
- name: Copy base screenshots as baselines
if: always() && steps.build.outcome == 'success'
run: |
if ls /tmp/screenshots-base/*.png 1>/dev/null 2>&1; then
cp /tmp/screenshots-base/*.png e2e/screenshots/
echo "Copied base branch screenshots as baselines"
else
echo "No base screenshots found — first run will generate baselines"
fi
- name: Run E2E tests (visual warnings only)
id: e2e
if: always() && steps.build.outcome == 'success'
continue-on-error: true
env:
CI: true
VISUAL_MODE: warning
run: npx playwright test 2>&1 | tee e2e-output.txt
- name: Collect current screenshots
if: always() && steps.e2e.outcome != 'skipped'
run: |
mkdir -p /tmp/screenshots-current
if [ -d "test-results" ]; then
find test-results -name "*-actual.png" | while read f; do
name=$(basename "$f" | sed 's/-actual\.png/.png/')
cp "$f" "/tmp/screenshots-current/$name"
done
fi
for f in e2e/screenshots/*.png; do
name=$(basename "$f")
if [ ! -f "/tmp/screenshots-current/$name" ]; then
cp "$f" "/tmp/screenshots-current/$name"
fi
done
echo "Current screenshots:"
ls /tmp/screenshots-current/
- name: Run smart diff comparison
if: always() && steps.e2e.outcome != 'skipped'
run: |
node scripts/compare-screenshots.js \
/tmp/screenshots-base \
/tmp/screenshots-current \
--output /tmp/comparison.json \
--diff-dir /tmp/screenshot-diffs \
--highlight-dir /tmp/screenshot-highlights
- name: Ensure ci-assets release exists
if: always() && steps.e2e.outcome != 'skipped'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release view ci-assets 2>/dev/null || \
gh release create ci-assets --title "CI Assets" --notes "Automated visual regression assets" --prerelease
- name: Upload images to release assets
if: always() && steps.e2e.outcome != 'skipped'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_NUM="${{ github.event.pull_request.number }}"
RUN_ID="${{ github.run_id }}"
REPO="${{ github.repository }}"
PREFIX="pr${PR_NUM}-run${RUN_ID}"
gh release view ci-assets --json assets --jq ".assets[].name" 2>/dev/null | \
grep "^pr${PR_NUM}-" | while read -r old; do
gh release delete-asset ci-assets "$old" --yes 2>/dev/null || true
done
CHANGED=$(node -e "
const r = JSON.parse(require('fs').readFileSync('/tmp/comparison.json','utf-8'));
r.images.filter(i => i.status === 'changed').forEach(i => console.log(i.name));
")
mkdir -p /tmp/upload-staging
URLS=""
if [ -n "$CHANGED" ]; then
while IFS= read -r name; do
[ -z "$name" ] && continue
BEFORE_URL=""
AFTER_URL=""
HIGHLIGHT_URL=""
if [ -f "/tmp/screenshots-base/$name" ]; then
ASSET="${PREFIX}-before-${name}"
cp "/tmp/screenshots-base/$name" "/tmp/upload-staging/${ASSET}"
gh release upload ci-assets "/tmp/upload-staging/${ASSET}" --clobber 2>/dev/null || true
BEFORE_URL="https://github.com/${REPO}/releases/download/ci-assets/${ASSET}"
fi
if [ -f "/tmp/screenshots-current/$name" ]; then
ASSET="${PREFIX}-after-${name}"
cp "/tmp/screenshots-current/$name" "/tmp/upload-staging/${ASSET}"
gh release upload ci-assets "/tmp/upload-staging/${ASSET}" --clobber 2>/dev/null || true
AFTER_URL="https://github.com/${REPO}/releases/download/ci-assets/${ASSET}"
fi
if [ -f "/tmp/screenshot-highlights/highlight-$name" ]; then
ASSET="${PREFIX}-highlight-${name}"
cp "/tmp/screenshot-highlights/highlight-$name" "/tmp/upload-staging/${ASSET}"
gh release upload ci-assets "/tmp/upload-staging/${ASSET}" --clobber 2>/dev/null || true
HIGHLIGHT_URL="https://github.com/${REPO}/releases/download/ci-assets/${ASSET}"
fi
URLS="${URLS}${name}|${BEFORE_URL}|${AFTER_URL}|${HIGHLIGHT_URL}\n"
done <<< "$CHANGED"
fi
rm -rf /tmp/upload-staging
echo -e "$URLS" > /tmp/image-urls.txt
- name: Upload all screenshots (combined artifact)
if: always() && steps.e2e.outcome != 'skipped'
uses: actions/upload-artifact@v7
with:
name: all-screenshots
path: |
/tmp/screenshots-base/
/tmp/screenshots-current/
/tmp/screenshot-diffs/
/tmp/screenshot-highlights/
/tmp/comparison.json
retention-days: 14
if-no-files-found: ignore
- name: Comment on PR
if: always()
uses: actions/github-script@v9
with:
script: |
const fs = require('fs');
const marker = '<!-- pr-validation-bot -->';
// Standard checks
const checks = [
{ name: 'Dependencies installed', outcome: '${{ steps.install.outcome }}' },
{ name: 'Linting passed', outcome: '${{ steps.lint.outcome }}' },
{ name: 'Format check passed', outcome: '${{ steps.format.outcome }}' },
{ name: 'Build successful', outcome: '${{ steps.build.outcome }}' },
{ name: 'Docker build', outcome: '${{ steps.docker.outcome }}' },
{ name: 'E2E tests', outcome: '${{ steps.e2e.outcome }}' },
];
const allPassed = checks.every(c => c.outcome === 'success');
const status = allPassed ? '✅ PASSED' : '❌ FAILED';
const emoji = allPassed ? '🎉' : '💥';
const checkLines = checks
.map(c => `- ${c.outcome === 'success' ? '✅' : c.outcome === 'skipped' ? '⏭️' : '❌'} ${c.name}`)
.join('\n');
// Visual regression section
let visualSection = '';
try {
const report = JSON.parse(fs.readFileSync('/tmp/comparison.json', 'utf-8'));
const { summary } = report;
const changedImages = report.images.filter(i => i.status === 'changed');
const urlMap = new Map();
try {
const lines = fs.readFileSync('/tmp/image-urls.txt', 'utf-8').trim().split('\n');
for (const line of lines) {
if (!line.trim()) continue;
const [name, before, after, highlight] = line.split('|');
if (name) urlMap.set(name, { before, after, highlight });
}
} catch {}
if (changedImages.length > 0) {
visualSection += `\n**Visual Changes:** ${summary.changed} of ${summary.total} screenshots changed\n\n`;
for (const img of changedImages) {
const pageName = img.name.replace('.png', '').replace(/-/g, ' ');
const pct = img.changedPercent !== undefined ? ` — ${img.changedPercent}% changed` : '';
const urls = urlMap.get(img.name);
visualSection += `<details>\n<summary>📸 <strong>${pageName}</strong>${pct}</summary>\n\n`;
if (urls?.before && urls?.after && urls?.highlight) {
visualSection += `| Before | After | Diff |\n|--------|-------|------|\n`;
visualSection += `| <img src="${urls.before}" width="280" /> | <img src="${urls.after}" width="280" /> | <img src="${urls.highlight}" width="280" /> |\n\n`;
}
visualSection += `</details>\n\n`;
}
} else {
visualSection += `\n**Visual Regression:** ✅ All ${summary.total} screenshots match — no changes.\n`;
}
} catch {
visualSection = '';
}
const body = `${marker}
${emoji} **PR Validation ${status}**
**Commit:** \`${{ github.event.pull_request.head.sha }}\`
**Branch:** \`${{ github.head_ref }}\`
**Checks:**
${checkLines}
${visualSection}
${allPassed ? '**Ready to merge!** ✨' : '**Please fix the failing checks.**'}
---
🔗 [View workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
⏰ Generated at: \`${new Date().toISOString()}\``;
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(marker)
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body,
});
}