-
Notifications
You must be signed in to change notification settings - Fork 76
102 lines (84 loc) · 3.49 KB
/
pr-label-inheritance.yml
File metadata and controls
102 lines (84 loc) · 3.49 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
name: PR Label Inheritance
on:
pull_request_target:
types: [opened, edited, reopened]
permissions:
issues: read
pull-requests: write
jobs:
inherit-labels:
runs-on: ubuntu-latest
steps:
- name: Inherit Labels from Linked Issues
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prNumber = context.issue.number;
const prBody = context.payload.pull_request.body || '';
const prCreatedAt =
new Date(context.payload.pull_request.created_at);
console.log(`Processing PR #${prNumber}`);
console.log(`PR created at: ${prCreatedAt}`);
// Extract issue numbers from PR body
// Matches patterns like:
// Fixes #123, Closes #456, Resolves #789, etc.
const issuePattern =
/(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)\s+#(\d+)/gi;
const matches = [...prBody.matchAll(issuePattern)];
const issueNumbers = matches.map(match => parseInt(match[1]));
const foundIssues = issueNumbers.join(', ') || 'none';
console.log(`Found linked issues: ${foundIssues}`);
// Collect all labels to add
const labelsToAdd = new Set();
// Fetch labels from linked issues
for (const issueNumber of issueNumbers) {
try {
const issue = await github.rest.issues.get({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber
});
const issueLabels = issue.data.labels.map(label =>
typeof label === 'string' ? label : label.name
);
const labelsList = issueLabels.join(', ') || 'none';
console.log(`Issue #${issueNumber} has labels: ${labelsList}`);
issueLabels.forEach(label => labelsToAdd.add(label));
} catch (error) {
console.log(`Could not fetch issue #${issueNumber}: ${error.message}`);
}
}
// Add hacktoberfest-accepted label for PRs in October
const month = prCreatedAt.getMonth(); // 0-indexed (9 = Oct)
if (month === 9) { // October
labelsToAdd.add('hacktoberfest-accepted');
console.log('PR in October - adding hacktoberfest-accepted');
}
// Get current PR labels
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
const currentLabels = new Set(
pr.data.labels.map(label =>
typeof label === 'string' ? label : label.name
)
);
// Filter out labels that are already on the PR
const newLabels = [...labelsToAdd].filter(
label => !currentLabels.has(label)
);
if (newLabels.length > 0) {
console.log(`Adding labels to PR: ${newLabels.join(', ')}`);
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: newLabels
});
console.log('Labels added successfully!');
} else {
console.log('No new labels to add');
}