-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
587 lines (497 loc) · 22.2 KB
/
action.yml
File metadata and controls
587 lines (497 loc) · 22.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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
name: 'Issue & PR Automation Suite'
description: 'Automate GitHub Issues & PRs with project boards, label/milestone sync, and ZAP scan labeling'
author: 'SillyLittleTech'
branding:
icon: 'git-pull-request'
color: 'blue'
inputs:
github-token:
description: 'GitHub token for API access'
required: true
default: ${{ github.token }}
project-name:
description: 'Name of the GitHub Projects V2 project to manage'
required: false
default: 'Portfolio Devmt'
enable-project-automation:
description: 'Enable project board automation'
required: false
default: 'true'
enable-label-sync:
description: 'Enable label and milestone synchronization between issues and PRs'
required: false
default: 'true'
enable-zap-labeling:
description: 'Enable automatic labeling of ZAP security scan issues'
required: false
default: 'true'
zap-labels:
description: 'Comma-separated list of labels to apply to ZAP scan issues'
required: false
default: 'Meta,Stylistic,javascript,meta:seq,ZAP!'
runs:
using: 'composite'
steps:
- name: Project Board Automation
if: ${{ inputs.enable-project-automation == 'true' && ((github.event_name == 'issues' && github.event.action == 'opened') || github.event_name == 'pull_request') }}
uses: actions/github-script@v7
with:
github-token: ${{ inputs.github-token }}
script: |
const projectName = '${{ inputs.project-name }}';
if (context.eventName === 'issues' && context.payload.action === 'opened') {
const issue = context.payload.issue;
const issueId = issue.node_id;
const issueNumber = issue.number;
// Query to find the project and add the issue
const findProjectQuery = `
query($owner: String!) {
user(login: $owner) {
projectsV2(first: 20) {
nodes {
id
title
fields(first: 20) {
nodes {
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
}
}
}
`;
try {
// Find the project
const projectResult = await github.graphql(findProjectQuery, {
owner: context.repo.owner
});
const project = projectResult.user.projectsV2.nodes.find(p =>
p.title === projectName
);
if (!project) {
console.log(`❌ Project "${projectName}" not found`);
console.log('Available projects:', projectResult.user.projectsV2.nodes.map(p => p.title));
return;
}
console.log(`✅ Found project: ${project.title} (${project.id})`);
// Add issue to project
const addItemMutation = `
mutation($projectId: ID!, $contentId: ID!) {
addProjectV2ItemById(input: {
projectId: $projectId
contentId: $contentId
}) {
item {
id
}
}
}
`;
const addResult = await github.graphql(addItemMutation, {
projectId: project.id,
contentId: issueId
});
const itemId = addResult.addProjectV2ItemById.item.id;
console.log(`✅ Added issue #${issueNumber} to project (item ID: ${itemId})`);
// Find the Status field and Backlog option
const statusField = project.fields.nodes.find(f =>
f.name === 'Status' && f.options
);
if (!statusField) {
console.log('⚠️ Status field not found, item added without specific status');
return;
}
const backlogOption = statusField.options.find(o =>
o.name === 'Backlog'
);
if (!backlogOption) {
console.log('⚠️ Backlog status not found');
console.log('Available statuses:', statusField.options.map(o => o.name));
return;
}
// Set item status to Backlog
const updateStatusMutation = `
mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $value: ProjectV2FieldValue!) {
updateProjectV2ItemFieldValue(input: {
projectId: $projectId
itemId: $itemId
fieldId: $fieldId
value: $value
}) {
projectV2Item {
id
}
}
}
`;
await github.graphql(updateStatusMutation, {
projectId: project.id,
itemId: itemId,
fieldId: statusField.id,
value: { singleSelectOptionId: backlogOption.id }
});
console.log(`✅ Set issue #${issueNumber} status to "Backlog"`);
} catch (error) {
console.error('❌ Error managing project:', error.message);
if (error.errors) {
console.error('GraphQL errors:', JSON.stringify(error.errors, null, 2));
}
}
} else if (context.eventName === 'pull_request') {
const pr = context.payload.pull_request;
const prNumber = pr.number;
const action = context.payload.action;
// Extract issue numbers from PR body and title
const prText = `${pr.title} ${pr.body || ''}`;
const issuePattern = /#(\d+)|(?:close[ds]?|fix(?:e[ds]?)?|resolve[ds]?)\s+#(\d+)|(?:https?:\/\/)?(?:www\.)?github\.com\/[^\/]+\/[^\/]+\/issues\/(\d+)/gi;
const issueNumbers = new Set();
let match;
while ((match = issuePattern.exec(prText)) !== null) {
const issueNum = match[1] || match[2] || match[3];
if (issueNum) {
issueNumbers.add(parseInt(issueNum));
}
}
if (issueNumbers.size === 0) {
console.log('ℹ️ No linked issues found in PR');
return;
}
console.log(`🔗 Found ${issueNumbers.size} linked issue(s):`, Array.from(issueNumbers));
// Determine target status based on PR state and action
let targetStatus = null;
if (action === 'opened' || action === 'converted_to_draft') {
targetStatus = 'In Progress';
} else if (action === 'ready_for_review') {
targetStatus = 'In Review';
} else if (action === 'closed' && pr.merged) {
targetStatus = 'In Review'; // Set to In Review when merged (not Done, per requirements)
} else if (action === 'closed' && !pr.merged) {
console.log('ℹ️ PR closed without merging - not updating issue status');
return;
}
if (!targetStatus) {
console.log(`ℹ️ No status update needed for action: ${action}`);
return;
}
console.log(`📊 Target status: "${targetStatus}"`);
// Query to find the project and get issue details
const findProjectQuery = `
query($owner: String!) {
user(login: $owner) {
projectsV2(first: 20) {
nodes {
id
title
fields(first: 20) {
nodes {
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
items(first: 100) {
nodes {
id
content {
... on Issue {
number
}
}
}
}
}
}
}
}
`;
try {
// Find the project
const projectResult = await github.graphql(findProjectQuery, {
owner: context.repo.owner
});
const project = projectResult.user.projectsV2.nodes.find(p =>
p.title === projectName
);
if (!project) {
console.log(`❌ Project "${projectName}" not found`);
return;
}
console.log(`✅ Found project: ${project.title}`);
// Find the Status field and target option
const statusField = project.fields.nodes.find(f =>
f.name === 'Status' && f.options
);
if (!statusField) {
console.log('❌ Status field not found in project');
return;
}
const targetOption = statusField.options.find(o =>
o.name === targetStatus
);
if (!targetOption) {
console.log(`❌ Status "${targetStatus}" not found in project`);
console.log('Available statuses:', statusField.options.map(o => o.name));
return;
}
// Update status for each linked issue
const updateStatusMutation = `
mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $value: ProjectV2FieldValue!) {
updateProjectV2ItemFieldValue(input: {
projectId: $projectId
itemId: $itemId
fieldId: $fieldId
value: $value
}) {
projectV2Item {
id
}
}
}
`;
for (const issueNumber of issueNumbers) {
// Find the project item for this issue
const projectItem = project.items.nodes.find(item =>
item.content && item.content.number === issueNumber
);
if (!projectItem) {
console.log(`⚠️ Issue #${issueNumber} not found in project, skipping`);
continue;
}
await github.graphql(updateStatusMutation, {
projectId: project.id,
itemId: projectItem.id,
fieldId: statusField.id,
value: { singleSelectOptionId: targetOption.id }
});
console.log(`✅ Updated issue #${issueNumber} status to "${targetStatus}"`);
}
} catch (error) {
console.error('❌ Error updating issue status:', error.message);
if (error.errors) {
console.error('GraphQL errors:', JSON.stringify(error.errors, null, 2));
}
}
}
- name: Label & Milestone Synchronization
if: ${{ inputs.enable-label-sync == 'true' }}
uses: actions/github-script@v7
with:
github-token: ${{ inputs.github-token }}
script: |
const { owner, repo } = context.repo;
const eventName = context.eventName;
const action = context.payload.action;
console.log(`Event: ${eventName}, Action: ${action}`);
// Helper function to extract linked issue numbers from text
function extractIssueNumbers(text) {
const issuePattern = /#(\d+)|(?:close[ds]?|fix(?:e[ds]?)?|resolve[ds]?)\s+#(\d+)|(?:https?:\/\/)?(?:www\.)?github\.com\/[^\/]+\/[^\/]+\/issues\/(\d+)/gi;
const issueNumbers = new Set();
let match;
while ((match = issuePattern.exec(text)) !== null) {
const issueNum = match[1] || match[2] || match[3];
if (issueNum) {
issueNumbers.add(parseInt(issueNum));
}
}
return Array.from(issueNumbers);
}
// Helper function to find PRs that link to a specific issue
async function findLinkedPRs(issueNumber) {
const { data: prs } = await github.rest.pulls.list({
owner,
repo,
state: 'open',
});
const linkedPRs = [];
for (const pr of prs) {
const prText = `${pr.title} ${pr.body || ''}`;
const linkedIssues = extractIssueNumbers(prText);
if (linkedIssues.includes(issueNumber)) {
linkedPRs.push(pr.number);
}
}
return linkedPRs;
}
// ISSUE EVENTS
if (eventName === 'issues') {
const issue = context.payload.issue;
const issueNumber = issue.number;
if (action === 'opened') {
console.log(`✨ New issue #${issueNumber} opened`);
}
if (action === 'labeled' || action === 'unlabeled') {
console.log(`🏷️ Issue #${issueNumber} labels changed`);
// Find linked PRs and sync labels
const linkedPRs = await findLinkedPRs(issueNumber);
console.log(`Found ${linkedPRs.length} linked PRs:`, linkedPRs);
if (linkedPRs.length > 0) {
const issueLabels = issue.labels.map(label =>
typeof label === 'object' ? label.name : label
).filter(Boolean);
console.log('📋 Issue labels:', issueLabels);
for (const prNumber of linkedPRs) {
try {
await github.rest.issues.setLabels({
owner,
repo,
issue_number: prNumber,
labels: issueLabels,
});
console.log(`✅ Synced labels to PR #${prNumber}`);
} catch (error) {
console.log(`❌ Failed to sync labels to PR #${prNumber}:`, error.message);
}
}
}
}
if (action === 'milestoned' || action === 'demilestoned') {
console.log(`📅 Issue #${issueNumber} milestone changed`);
// Find linked PRs and sync milestone
const linkedPRs = await findLinkedPRs(issueNumber);
for (const prNumber of linkedPRs) {
try {
await github.rest.issues.update({
owner,
repo,
issue_number: prNumber,
milestone: issue.milestone ? issue.milestone.number : null,
});
console.log(`✅ Synced milestone to PR #${prNumber}`);
} catch (error) {
console.log(`❌ Failed to sync milestone to PR #${prNumber}:`, error.message);
}
}
}
}
// PULL REQUEST EVENTS
if (eventName === 'pull_request' || eventName === 'pull_request_target') {
const pr = context.payload.pull_request;
const prNumber = pr.number;
const prText = `${pr.title} ${pr.body || ''}`;
const linkedIssues = extractIssueNumbers(prText);
console.log(`🔀 PR #${prNumber} event: ${action}`);
console.log(`Found ${linkedIssues.length} linked issues:`, linkedIssues);
if (action === 'opened') {
// Sync labels from linked issues to PR
if (linkedIssues.length > 0) {
const allLabels = new Set();
for (const issueNumber of linkedIssues) {
try {
const { data: issue } = await github.rest.issues.get({
owner,
repo,
issue_number: issueNumber,
});
issue.labels.forEach(label => {
if (typeof label === 'object' && label.name) {
allLabels.add(label.name);
}
});
} catch (error) {
console.log(`❌ Could not fetch issue #${issueNumber}:`, error.message);
}
}
if (allLabels.size > 0) {
try {
await github.rest.issues.setLabels({
owner,
repo,
issue_number: prNumber,
labels: Array.from(allLabels),
});
console.log(`✅ Applied ${allLabels.size} labels to PR #${prNumber}`);
} catch (error) {
console.log(`❌ Failed to apply labels to PR #${prNumber}:`, error.message);
}
}
}
}
// Sync milestones
if (linkedIssues.length > 0) {
for (const issueNumber of linkedIssues) {
try {
const { data: issue } = await github.rest.issues.get({
owner,
repo,
issue_number: issueNumber,
});
// Sync milestone from issue to PR if PR doesn't have one
if (issue.milestone && !pr.milestone) {
await github.rest.issues.update({
owner,
repo,
issue_number: prNumber,
milestone: issue.milestone.number,
});
console.log(`✅ Applied milestone "${issue.milestone.title}" from issue #${issueNumber} to PR #${prNumber}`);
}
// Sync milestone from PR to issue if issue doesn't have one
else if (!issue.milestone && pr.milestone) {
await github.rest.issues.update({
owner,
repo,
issue_number: issueNumber,
milestone: pr.milestone.number,
});
console.log(`✅ Applied milestone "${pr.milestone.title}" from PR #${prNumber} to issue #${issueNumber}`);
}
} catch (error) {
console.log(`❌ Could not sync milestone for issue #${issueNumber}:`, error.message);
}
}
}
}
console.log('🎉 Label & milestone sync completed')
- name: Auto-label ZAP Security Scan Issues
if: ${{ inputs.enable-zap-labeling == 'true' && github.event_name == 'issues' && (github.event.action == 'opened' || github.event.action == 'edited') }}
uses: actions/github-script@v7
with:
github-token: ${{ inputs.github-token }}
script: |
const { owner, repo } = context.repo;
const issue = context.payload.issue;
const issueNumber = issue.number;
const issueTitle = issue.title || '';
console.log(`Checking issue #${issueNumber}: "${issueTitle}"`);
// Configurable pattern for ZAP Scan Baseline Report (case-insensitive)
const zapPattern = 'ZAP Scan Baseline Report';
if (issueTitle.toLowerCase().includes(zapPattern.toLowerCase())) {
console.log('✅ Issue title matches ZAP Scan Baseline Report pattern');
// Parse labels from input
const labelsToApply = '${{ inputs.zap-labels }}'.split(',').map(l => l.trim()).filter(Boolean);
// Get current labels on the issue
const currentLabels = issue.labels.map(label =>
typeof label === 'object' ? label.name : label
).filter(Boolean);
console.log('Current labels:', currentLabels);
// Combine current labels with new labels (avoiding duplicates)
const allLabels = [...new Set([...currentLabels, ...labelsToApply])];
console.log('Applying labels:', allLabels);
try {
await github.rest.issues.setLabels({
owner,
repo,
issue_number: issueNumber,
labels: allLabels,
});
console.log(`✅ Successfully applied labels to issue #${issueNumber}`);
} catch (error) {
console.error(`❌ Failed to apply labels to issue #${issueNumber}:`, error.message);
}
} else {
console.log('ℹ️ Issue title does not match ZAP Scan Baseline Report pattern, skipping');
}
console.log('✅ ZAP issue labeling completed')