-
-
Notifications
You must be signed in to change notification settings - Fork 3k
401 lines (361 loc) Β· 15.9 KB
/
pr-validator.yml
File metadata and controls
401 lines (361 loc) Β· 15.9 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
name: PR Validator
on:
pull_request:
types: [opened, edited, synchronize]
paths:
- 'contributions/**'
permissions:
pull-requests: write
issues: write
contents: read
concurrency:
group: pr-validator-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
validate:
name: Validate Contribution
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false
steps:
- name: Remove stale label on new activity
uses: actions/github-script@v7
with:
script: |
try {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
name: 'stale',
});
core.info('Removed stale label due to new activity.');
} catch {
// Label was not present, that is fine
}
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: cd .github/scripts && npm ci --omit=dev
# Stage 1: PR Format Check (Idea 1 & 5)
- name: Validate PR format
id: pr_format
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: node .github/scripts/validate-pr-format.js
# Stage 2 & 3: Diff Analysis - Auto-label + Critical Detection (Ideas 6, 12, 13)
- name: Analyse diff
id: diff
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: node .github/scripts/analyse-diff.js
# Stage 4 & 5: JSON Lint + Schema Validation (Ideas 2 & 15)
- name: Validate schema
id: schema
continue-on-error: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: node .github/scripts/validate-schema.js
# Stage 6: Cross-Reference Validation (Idea 9)
- name: Validate cross-references
id: crossref
continue-on-error: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: node .github/scripts/validate-cross-reference.js
# Stage 7: Coordinate Bounds Check (Idea 8)
- name: Check coordinate bounds
id: coords
continue-on-error: true
run: node .github/scripts/validate-coordinates.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Stage 8: Duplicate Detection (Idea 7)
- name: Detect duplicates
id: duplicates
continue-on-error: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: node .github/scripts/detect-duplicates.js
# Stage 9: Source URL Check (Idea 16)
- name: Check source URLs
id: urls
continue-on-error: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: node .github/scripts/check-source-urls.js
# Post consolidated validation report
- name: Post validation report
if: always()
uses: actions/github-script@v7
env:
PR_FORMAT_RESULTS: ${{ steps.pr_format.outputs.results }}
PR_FORMAT_NEEDS_CLARIFICATION: ${{ steps.pr_format.outputs.needs_clarification }}
DIFF_LABELS: ${{ steps.diff.outputs.labels }}
DIFF_IS_CRITICAL: ${{ steps.diff.outputs.is_critical }}
DIFF_CRITICAL_REASONS: ${{ steps.diff.outputs.critical_reasons }}
DIFF_TOTAL_RECORDS: ${{ steps.diff.outputs.total_records }}
DIFF_IS_LARGE: ${{ steps.diff.outputs.is_large }}
SCHEMA_ERRORS: ${{ steps.schema.outputs.errors }}
SCHEMA_WARNINGS: ${{ steps.schema.outputs.warnings }}
SCHEMA_RECORD_COUNT: ${{ steps.schema.outputs.record_count }}
SCHEMA_HAS_ERRORS: ${{ steps.schema.outputs.has_errors }}
CROSSREF_ERRORS: ${{ steps.crossref.outputs.errors }}
CROSSREF_VALID: ${{ steps.crossref.outputs.valid }}
CROSSREF_HAS_ERRORS: ${{ steps.crossref.outputs.has_errors }}
COORD_WARNINGS: ${{ steps.coords.outputs.warnings }}
COORD_CHECKED: ${{ steps.coords.outputs.checked }}
DUP_WARNINGS: ${{ steps.duplicates.outputs.warnings }}
DUP_CHECKED: ${{ steps.duplicates.outputs.checked }}
URL_ERRORS: ${{ steps.urls.outputs.errors }}
URL_VALID: ${{ steps.urls.outputs.valid }}
with:
script: |
const prFormatResults = JSON.parse(process.env.PR_FORMAT_RESULTS || '{}');
const needsClarification = process.env.PR_FORMAT_NEEDS_CLARIFICATION === 'true';
const labels = JSON.parse(process.env.DIFF_LABELS || '[]');
const isCritical = process.env.DIFF_IS_CRITICAL === 'true';
const criticalReasons = JSON.parse(process.env.DIFF_CRITICAL_REASONS || '[]');
const totalRecords = parseInt(process.env.DIFF_TOTAL_RECORDS || '0');
const isLarge = process.env.DIFF_IS_LARGE === 'true';
const schemaErrors = JSON.parse(process.env.SCHEMA_ERRORS || '[]');
const schemaWarnings = JSON.parse(process.env.SCHEMA_WARNINGS || '[]');
const schemaRecordCount = parseInt(process.env.SCHEMA_RECORD_COUNT || '0');
const schemaHasErrors = process.env.SCHEMA_HAS_ERRORS === 'true';
const crossrefErrors = JSON.parse(process.env.CROSSREF_ERRORS || '[]');
const crossrefValid = parseInt(process.env.CROSSREF_VALID || '0');
const crossrefHasErrors = process.env.CROSSREF_HAS_ERRORS === 'true';
const coordWarnings = JSON.parse(process.env.COORD_WARNINGS || '[]');
const coordChecked = parseInt(process.env.COORD_CHECKED || '0');
const dupWarnings = JSON.parse(process.env.DUP_WARNINGS || '[]');
const dupChecked = parseInt(process.env.DUP_CHECKED || '0');
const urlErrors = JSON.parse(process.env.URL_ERRORS || '[]');
const urlValid = parseInt(process.env.URL_VALID || '0');
// Build report
let report = '## CSC Validation Report\n\n';
// PR Format section
report += '### PR Format\n';
for (const [key, val] of Object.entries(prFormatResults)) {
const icon = val.pass ? ':white_check_mark:' : ':x:';
report += `- ${icon} ${val.label}\n`;
}
report += '\n';
// Labels
if (labels.length > 0) {
report += `**Labels applied:** ${labels.map(l => '`' + l + '`').join(', ')}\n\n`;
}
// Critical warning
if (isCritical) {
report += '### :rotating_light: Critical Change Detected\n';
report += '> This PR contains changes that require explicit maintainer approval.\n\n';
for (const reason of criticalReasons) {
report += `- ${reason}\n`;
}
report += '\n';
}
// Large contribution warning
if (isLarge) {
report += '### :warning: Large Contribution\n';
report += `> This PR contains ${totalRecords} records. Large contributions require manual review.\n\n`;
}
// Schema Validation
report += '### Schema Validation';
if (schemaRecordCount > 0) {
report += ` (${schemaRecordCount} records)\n`;
} else {
report += '\n';
}
if (schemaErrors.length > 0) {
report += '\n**Errors (blocking):**\n';
for (const err of schemaErrors.slice(0, 20)) {
report += `- :x: ${err}\n`;
}
if (schemaErrors.length > 20) {
report += `- _...and ${schemaErrors.length - 20} more errors_\n`;
}
}
if (schemaWarnings.length > 0) {
report += '\n**Warnings:**\n';
for (const warn of schemaWarnings.slice(0, 10)) {
report += `- :warning: ${warn}\n`;
}
if (schemaWarnings.length > 10) {
report += `- _...and ${schemaWarnings.length - 10} more warnings_\n`;
}
}
if (schemaErrors.length === 0 && schemaWarnings.length === 0 && schemaRecordCount > 0) {
report += `:white_check_mark: All records passed validation\n`;
}
report += '\n';
// Cross-Reference
if (crossrefValid > 0 || crossrefErrors.length > 0) {
report += '### Cross-Reference Validation\n';
if (crossrefValid > 0) {
report += `:white_check_mark: ${crossrefValid} reference(s) verified\n`;
}
for (const err of crossrefErrors.slice(0, 10)) {
report += `- :x: ${err}\n`;
}
report += '\n';
}
// Coordinate Bounds
if (coordChecked > 0 || coordWarnings.length > 0) {
report += '### Geo-Bounds Check\n';
if (coordWarnings.length > 0) {
for (const warn of coordWarnings.slice(0, 10)) {
report += `- :warning: ${warn}\n`;
}
} else {
report += `:white_check_mark: All ${coordChecked} coordinate(s) within expected country bounds\n`;
}
report += '\n';
}
// Duplicate Detection
if (dupChecked > 0 || dupWarnings.length > 0) {
report += '### Duplicate Detection\n';
if (dupWarnings.length > 0) {
for (const warn of dupWarnings.slice(0, 10)) {
report += `- :warning: ${warn}\n`;
}
} else {
report += `:white_check_mark: No duplicates found among ${dupChecked} record(s)\n`;
}
report += '\n';
}
// Source URLs
if (urlValid > 0 || urlErrors.length > 0) {
report += '### Source URL Verification\n';
if (urlValid > 0) {
report += `:white_check_mark: ${urlValid} source URL(s) accessible\n`;
}
for (const err of urlErrors) {
report += `- :warning: ${err}\n`;
}
report += '\n';
}
// Summary
const totalErrors = schemaErrors.length + crossrefErrors.length;
const totalWarnings = schemaWarnings.length + coordWarnings.length + dupWarnings.length + urlErrors.length;
report += '---\n';
if (needsClarification) {
report += ':speech_balloon: **This PR modifies data but has no linked issue or clear description.** ';
report += 'Please provide context about what this change does and why, or link a related issue.\n\n';
}
if (totalErrors === 0 && totalWarnings === 0) {
report += ':white_check_mark: **All checks passed** | Status: Ready for review\n';
} else if (totalErrors === 0) {
report += `:white_check_mark: **0 errors, ${totalWarnings} warning(s)** | Status: Ready for review (with warnings)\n`;
} else {
report += `:x: **${totalErrors} error(s), ${totalWarnings} warning(s)** | Status: Changes required\n`;
report += '\nPlease fix the errors above and push a new commit. ';
report += 'Refer to our [Contribution Guidelines](https://github.com/dr5hn/countries-states-cities-database/blob/master/.github/CONTRIBUTING.md) for details.\n';
}
// Find existing bot comment to update (avoid spam)
const COMMENT_MARKER = '## CSC Validation Report';
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
per_page: 100,
});
const botComment = comments.find(
(c) =>
(c.user.login === 'github-actions[bot]' || c.user.type === 'Bot') &&
c.body.includes(COMMENT_MARKER)
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: report,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: report,
});
}
// Apply status labels
const statusLabels = [];
if (totalErrors > 0) {
statusLabels.push('needs-changes');
} else {
statusLabels.push('ready-for-review');
}
try {
// Remove conflicting status labels first
const labelsToRemove = ['needs-changes', 'ready-for-review'].filter(
(l) => !statusLabels.includes(l)
);
for (const label of labelsToRemove) {
try {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
name: label,
});
} catch {
// Label might not exist yet
}
}
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
labels: statusLabels,
});
} catch (err) {
core.warning(`Could not update status labels: ${err.message}`);
}
# Slack notification for critical PRs
- name: Notify Slack (critical)
if: steps.diff.outputs.is_critical == 'true'
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
DIFF_CRITICAL_REASONS: ${{ steps.diff.outputs.critical_reasons }}
uses: actions/github-script@v7
with:
script: |
if (!process.env.SLACK_WEBHOOK_URL) {
core.info('SLACK_WEBHOOK_URL not set. Skipping notification.');
return;
}
const { sendSlackMessage, buildCriticalAlert } = require('./.github/scripts/slack-notify.js');
const pr = context.payload.pull_request;
const reasons = JSON.parse(process.env.DIFF_CRITICAL_REASONS || '[]');
await sendSlackMessage(process.env.SLACK_WEBHOOK_URL, buildCriticalAlert({
prNumber: pr.number,
prTitle: pr.title,
prUrl: pr.html_url,
author: pr.user.login,
reasons,
}));
# Slack notification for large contributions
- name: Notify Slack (large contribution)
if: steps.diff.outputs.is_large == 'true'
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
DIFF_TOTAL_RECORDS: ${{ steps.diff.outputs.total_records }}
uses: actions/github-script@v7
with:
script: |
if (!process.env.SLACK_WEBHOOK_URL) {
core.info('SLACK_WEBHOOK_URL not set. Skipping notification.');
return;
}
const { sendSlackMessage, buildLargeContributionWarning } = require('./.github/scripts/slack-notify.js');
const pr = context.payload.pull_request;
await sendSlackMessage(process.env.SLACK_WEBHOOK_URL, buildLargeContributionWarning({
prNumber: pr.number,
prTitle: pr.title,
prUrl: pr.html_url,
author: pr.user.login,
recordCount: process.env.DIFF_TOTAL_RECORDS,
}));