-
Notifications
You must be signed in to change notification settings - Fork 0
87 lines (76 loc) · 3.04 KB
/
mirror-issue-labels.yml
File metadata and controls
87 lines (76 loc) · 3.04 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
name: Mirror Issue Labels to PR
on:
pull_request:
types: [opened, reopened, ready_for_review]
permissions: {}
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
mirror:
name: Mirror linked issue labels to PR
if: >-
contains(github.event.pull_request.labels.*.name, 'coding-agent') ||
github.event.pull_request.user.login == 'Copilot' ||
github.actor == 'copilot-swe-agent[bot]'
runs-on: ubuntu-latest
permissions:
pull-requests: write
issues: read
steps:
- name: Copy labels from linked issue(s)
uses: actions/github-script@v9
with:
script: |
const body = context.payload.pull_request.body || '';
// Strip HTML comments and fenced code blocks before matching, so
// example "Closes #N" lines in template guidance don't trigger.
const stripped = body
.replace(/<!--[\s\S]*?-->/g, '')
.replace(/```[\s\S]*?```/g, '');
// Match "Closes #N", "Fixes #N", "Resolves #N" (case-insensitive,
// with optional ing/d/s suffixes). Only same-repo references are mirrored.
const pattern = /\b(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)\s+#(\d+)\b/gi;
const issueNumbers = new Set();
let m;
while ((m = pattern.exec(stripped)) !== null) {
issueNumbers.add(parseInt(m[1], 10));
}
if (issueNumbers.size === 0) {
core.info('No "Closes #N" references found in PR body; nothing to mirror.');
return;
}
const prNumber = context.payload.pull_request.number;
const existingLabels = new Set(
(context.payload.pull_request.labels || []).map(l => l.name)
);
const toAdd = new Set();
for (const num of issueNumbers) {
try {
const { data: issue } = await github.rest.issues.get({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: num,
});
for (const label of issue.labels || []) {
const name = typeof label === 'string' ? label : label.name;
if (!name) continue;
if (existingLabels.has(name)) continue;
toAdd.add(name);
}
} catch (err) {
core.warning(`Failed to fetch issue #${num}: ${err.message}`);
}
}
if (toAdd.size === 0) {
core.info('Linked issue(s) had no new labels to mirror.');
return;
}
const labels = Array.from(toAdd);
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels,
});
core.info(`Mirrored ${labels.length} label(s) to PR: ${labels.join(', ')}`);