-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
470 lines (435 loc) · 20.4 KB
/
Copy pathpr-category-check.yml
File metadata and controls
470 lines (435 loc) · 20.4 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
name: PR Category Check
# Ensures every PR has at least one category checkbox checked.
# Automatically applies matching GitHub labels to the PR.
# Creates labels with custom colors if they don't exist yet.
# Uses pull_request_target so it works for fork PRs too (runs in base repo context).
# This is safe because we only read the PR body from the event payload — no fork code is checked out or executed.
on:
pull_request_target:
types: [opened, edited, synchronize, reopened]
permissions:
pull-requests: write
contents: read
issues: write
jobs:
check-pr-category:
name: Validate PR Category & Auto-Label
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Check categories and apply labels
uses: actions/github-script@v7
with:
script: |
const prBody = context.payload.pull_request.body || '';
const prNumber = context.payload.pull_request.number;
// Category checkboxes mapped to their GitHub label names, colors, and descriptions
const categories = [
{
label: 'bug fix',
color: 'D73A4A',
description: 'Fixes a bug or incorrect behavior',
displayName: 'Bug Fix',
pattern: /-\s*\[x\]\s*Bug Fix/i,
},
{
label: 'feature',
color: '9333EA',
description: 'Adds new functionality',
displayName: 'Feature',
pattern: /-\s*\[x\]\s*Feature/i,
},
{
label: 'performance',
color: 'F97316',
description: 'Improves performance (load time, memory, rendering)',
displayName: 'Performance',
pattern: /-\s*\[x\]\s*Performance/i,
},
{
label: 'tests',
color: '3B82F6',
description: 'Adds or updates test coverage',
displayName: 'Tests',
pattern: /-\s*\[x\]\s*Tests/i,
},
{
label: 'documentation',
color: '10B981',
description: 'Updates to docs, comments, or README',
displayName: 'Documentation',
pattern: /-\s*\[x\]\s*Documentation/i,
},
{
label: 'ci',
color: '6B7280',
description: 'Changes to CI/CD workflows and automation',
displayName: 'CI/CD',
pattern: /-\s*\[x\]\s*CI\s*\/\s*CD/i,
},
{
label: 'chore',
color: 'A0AEC0',
description: 'Maintenance, refactoring, or tooling with no behavior change',
displayName: 'Chore / Refactor',
pattern: /-\s*\[x\]\s*Chore/i,
},
];
const checkedCategories = categories.filter(cat => cat.pattern.test(prBody));
const uncheckedCategories = categories.filter(cat => !cat.pattern.test(prBody));
// --- Step 1: Fail CI if no category is selected ---
if (!prBody.trim()) {
core.setFailed('PR description is empty. Please fill in the PR template and check at least one category checkbox.');
return;
}
if (checkedCategories.length === 0) {
const message = [
'## PR Category Required',
'',
'This pull request does not have any **PR Category** selected.',
'Please edit your PR description and check **at least one** category checkbox:',
'',
'| Category | Description |',
'|----------|-------------|',
'| Bug Fix | Fixes a bug or incorrect behavior |',
'| Feature | Adds new functionality |',
'| Performance | Improves performance |',
'| Tests | Adds or updates test coverage |',
'| Documentation | Updates to docs, comments, or README |',
'| CI/CD | Changes to CI/CD workflows and automation |',
'',
'Example: Change `- [ ] Bug Fix` to `- [x] Bug Fix`',
'',
'> **Tip:** You can select multiple categories if your PR spans several areas.',
].join('\n');
core.setFailed(message);
return;
}
// --- Step 2: Ensure labels exist with proper colors ---
// Paginated to correctly handle repos with more than 100 labels.
const existingLabels = await github.paginate(github.rest.issues.listLabelsForRepo, {
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100,
});
const existingLabelNames = existingLabels.map(l => l.name);
for (const cat of categories) {
if (!existingLabelNames.includes(cat.label)) {
try {
await github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: cat.label,
color: cat.color,
description: cat.description,
});
core.info(`Created label: "${cat.label}" with color #${cat.color}`);
} catch (error) {
core.warning(`Could not create label "${cat.label}". Error: ${error.message}`);
}
}
}
// --- Step 3: Auto-apply labels for checked categories ---
const labelsToAdd = checkedCategories.map(cat => cat.label);
const labelsToRemove = uncheckedCategories.map(cat => cat.label);
// Get current labels on the PR
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const currentLabelNames = currentLabels.map(l => l.name);
// Add labels that are checked but not yet on the PR
for (const label of labelsToAdd) {
if (!currentLabelNames.includes(label)) {
try {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: [label],
});
core.info(`Added label: "${label}"`);
} catch (error) {
core.warning(`Could not add label "${label}". Error: ${error.message}`);
}
}
}
// Remove labels that are unchecked but still on the PR
for (const label of labelsToRemove) {
if (currentLabelNames.includes(label)) {
try {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
name: label,
});
core.info(`Removed label: "${label}"`);
} catch (error) {
core.warning(`Could not remove label "${label}". Error: ${error.message}`);
}
}
}
const selected = checkedCategories.map(c => c.displayName).join(', ');
core.info(`PR categories selected: ${selected}`);
core.info(`Labels synced: ${labelsToAdd.join(', ')}`);
- name: Apply PR size label
if: success()
uses: actions/github-script@v7
with:
script: |
const prNumber = context.payload.pull_request.number;
const additions = context.payload.pull_request.additions;
const deletions = context.payload.pull_request.deletions;
const totalChanges = additions + deletions;
const sizeTiers = [
{ label: 'size/XS', color: '3CBF00', description: 'Extra small: < 10 lines changed', max: 9 },
{ label: 'size/S', color: '5D9801', description: 'Small: 10-49 lines changed', max: 49 },
{ label: 'size/M', color: 'EEB800', description: 'Medium: 50-249 lines changed', max: 249 },
{ label: 'size/L', color: 'EA8700', description: 'Large: 250-499 lines changed', max: 499 },
{ label: 'size/XL', color: 'E05000', description: 'Extra large: 500-999 lines changed', max: 999 },
{ label: 'size/XXL', color: 'D73A4A', description: 'XXL: 1000+ lines changed', max: Infinity },
];
// size/XXL has max: Infinity so find() always matches; no fallback is needed.
const applicable = sizeTiers.find(t => totalChanges <= t.max);
core.info(`PR has ${totalChanges} total line changes -> ${applicable.label}`);
// Ensure all size labels exist in the repo (paginated to handle > 100 labels).
const repoLabels = await github.paginate(github.rest.issues.listLabelsForRepo, {
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100,
});
const repoLabelNames = repoLabels.map(l => l.name);
for (const tier of sizeTiers) {
if (!repoLabelNames.includes(tier.label)) {
try {
await github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: tier.label,
color: tier.color,
description: tier.description,
});
} catch (e) {
core.warning(`Could not create label "${tier.label}": ${e.message}`);
}
}
}
// Sync: remove stale size labels, add applicable one
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const currentLabelNames = currentLabels.map(l => l.name);
for (const tier of sizeTiers) {
if (tier.label !== applicable.label && currentLabelNames.includes(tier.label)) {
try {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
name: tier.label,
});
} catch (e) {
core.warning(`Could not remove label "${tier.label}": ${e.message}`);
}
}
}
if (!currentLabelNames.includes(applicable.label)) {
try {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: [applicable.label],
});
core.info(`Added size label: "${applicable.label}"`);
} catch (e) {
core.warning(`Could not add label "${applicable.label}": ${e.message}`);
}
}
- name: Apply PR area labels
if: success()
uses: actions/github-script@v7
with:
script: |
const prNumber = context.payload.pull_request.number;
// Fetch all changed files (paginated)
const files = await github.paginate(github.rest.pulls.listFiles, {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
per_page: 100,
});
const filePaths = files.map(f => f.filename);
const areaMappings = [
{ label: 'area/javascript', color: 'F7DF1E', description: 'Changes to JS source files', pattern: /^js\// },
{ label: 'area/css', color: '264DE4', description: 'Changes to CSS/SASS style files', pattern: /^css\// },
{ label: 'area/plugins', color: '6E40C9', description: 'Changes to plugin files', pattern: /^plugins\// },
{ label: 'area/docs', color: '0075CA', description: 'Changes to documentation', pattern: /^(Docs\/|README|CONTRIBUTING|.*\.md$)/i },
{ label: 'area/tests', color: '3B82F6', description: 'Changes to test files', pattern: /^(cypress\/|jest\.config|.*\.test\.|.*\.spec\.)/i },
{ label: 'area/i18n', color: 'E4E669', description: 'Changes to localization files', pattern: /^(locales|po)\// },
{ label: 'area/assets', color: 'D93F0B', description: 'Changes to images, sounds, or fonts', pattern: /^(images|sounds|fonts|header-icons|screenshots)\// },
{ label: 'area/ci-cd', color: '0052CC', description: 'Changes to CI/CD workflows', pattern: /^\.github\// },
{ label: 'area/lib', color: 'BFD4F2', description: 'Changes to library files', pattern: /^lib\// },
// Explicit extension list prevents .md files from matching and colliding with area/docs.
{ label: 'area/core', color: 'E11D48', description: 'Changes to core app entry files', pattern: /^(index|sw|script|env)\.(js|ts|mjs|cjs|html|json)$/i },
];
const touchedAreas = areaMappings.filter(area =>
filePaths.some(fp => area.pattern.test(fp))
);
core.info(`Areas touched: ${touchedAreas.map(a => a.label).join(', ') || 'none'}`);
// Ensure all area labels exist in the repo (paginated to handle > 100 labels).
const repoLabels = await github.paginate(github.rest.issues.listLabelsForRepo, {
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100,
});
const repoLabelNames = repoLabels.map(l => l.name);
for (const area of areaMappings) {
if (!repoLabelNames.includes(area.label)) {
try {
await github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: area.label,
color: area.color,
description: area.description,
});
} catch (e) {
core.warning(`Could not create label "${area.label}": ${e.message}`);
}
}
}
// Sync area labels on the PR
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const currentLabelNames = currentLabels.map(l => l.name);
const touchedLabelNames = touchedAreas.map(a => a.label);
for (const area of touchedAreas) {
if (!currentLabelNames.includes(area.label)) {
try {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: [area.label],
});
core.info(`Added area label: "${area.label}"`);
} catch (e) {
core.warning(`Could not add label "${area.label}": ${e.message}`);
}
}
}
for (const area of areaMappings) {
if (!touchedLabelNames.includes(area.label) && currentLabelNames.includes(area.label)) {
try {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
name: area.label,
});
core.info(`Removed area label: "${area.label}"`);
} catch (e) {
core.warning(`Could not remove label "${area.label}": ${e.message}`);
}
}
}
# - name: Check release notes
# if: success()
# uses: actions/github-script@v7
# with:
# script: |
# const prNumber = context.payload.pull_request.number;
# const prBody = context.payload.pull_request.body || '';
#
# // Detect a "## Release Notes" heading with non-empty, meaningful content.
# // Uses [\r\n]+ to handle Windows (CRLF) and Unix (LF) line endings.
# // Content that is only "N/A", "none", or an HTML comment counts as absent.
# const rnPattern = /#+\s*release\s+notes\s*[\r\n]+([\s\S]+?)(?=\n#|\s*$)/i;
# const match = prBody.match(rnPattern);
# const rnContent = match ? match[1].trim() : '';
# // Strip all HTML comments first, then check if anything meaningful remains.
# // This correctly handles trailing text after a comment and multiple comments.
# const stripped = rnContent.replace(/<!--[\s\S]*?-->/g, '').trim();
# const isPlaceholder = !stripped || /^(n\/a|none)$/i.test(stripped);
# const hasReleaseNotes = rnContent.length > 0 && !isPlaceholder;
#
# const rnLabel = 'release-notes';
# const noRnLabel = 'needs-release-notes';
# const labelDefs = [
# { label: rnLabel, color: '0E8A16', description: 'PR includes release notes' },
# { label: noRnLabel, color: 'FBCA04', description: 'PR is missing a release notes section' },
# ];
#
# // Ensure both labels exist in the repo (paginated to handle > 100 labels).
# const repoLabels = await github.paginate(github.rest.issues.listLabelsForRepo, {
# owner: context.repo.owner,
# repo: context.repo.repo,
# per_page: 100,
# });
# const repoLabelNames = repoLabels.map(l => l.name);
#
# for (const def of labelDefs) {
# if (!repoLabelNames.includes(def.label)) {
# try {
# await github.rest.issues.createLabel({
# owner: context.repo.owner,
# repo: context.repo.repo,
# name: def.label,
# color: def.color,
# description: def.description,
# });
# } catch (e) {
# core.warning(`Could not create label "${def.label}": ${e.message}`);
# }
# }
# }
#
# const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
# owner: context.repo.owner,
# repo: context.repo.repo,
# issue_number: prNumber,
# });
# const currentLabelNames = currentLabels.map(l => l.name);
#
# const labelToAdd = hasReleaseNotes ? rnLabel : noRnLabel;
# const labelToRemove = hasReleaseNotes ? noRnLabel : rnLabel;
#
# if (!currentLabelNames.includes(labelToAdd)) {
# try {
# await github.rest.issues.addLabels({
# owner: context.repo.owner,
# repo: context.repo.repo,
# issue_number: prNumber,
# labels: [labelToAdd],
# });
# } catch (e) {
# core.warning(`Could not add label "${labelToAdd}": ${e.message}`);
# }
# }
#
# if (currentLabelNames.includes(labelToRemove)) {
# try {
# await github.rest.issues.removeLabel({
# owner: context.repo.owner,
# repo: context.repo.repo,
# issue_number: prNumber,
# name: labelToRemove,
# });
# } catch (e) {
# core.warning(`Could not remove label "${labelToRemove}": ${e.message}`);
# }
# }
#
# if (hasReleaseNotes) {
# core.info('Release notes section found.');
# } else {
# core.warning('No release notes section found. Add a "## Release Notes" heading with content.');
# }