-
Notifications
You must be signed in to change notification settings - Fork 8.6k
109 lines (98 loc) · 3.81 KB
/
add-rna-closes-label.yml
File metadata and controls
109 lines (98 loc) · 3.81 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
name: Add closes:rna label to PRs
on:
pull_request_target:
types: [opened, edited]
jobs:
label-rna-project:
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: read
steps:
- name: Label if PR closes an issue on the RNA Program Board
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const label = "closes:rna";
const rnaBoardNumber = 2076;
const prNumber = context.payload.pull_request.number;
const existingLabels = context.payload.pull_request.labels.map(l => l.name);
if (existingLabels.includes(label)) {
return;
}
const { repository } = await github.graphql(`
query($owner: String!, $repo: String!, $prNumber: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $prNumber) {
body
closingIssuesReferences(first: 10) {
nodes {
projectItems(first: 20) {
nodes {
project { number }
}
}
}
}
}
}
}
`, {
owner: context.repo.owner,
repo: context.repo.repo,
prNumber,
});
const pr = repository.pullRequest;
const isOnRnaBoard = (issue) =>
issue.projectItems.nodes.some(item => item.project.number === rnaBoardNumber);
// Check closingIssuesReferences first (works for PRs targeting default branch)
if (pr.closingIssuesReferences.nodes.some(isOnRnaBoard)) {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: [label],
});
return;
}
// Fallback: parse issue references from PR body
const body = pr.body || "";
const pattern = /(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)[\s:]*(?:https:\/\/github\.com\/([^/]+\/[^/]+)\/issues\/(\d+)|#(\d+))/gi;
const issueRefs = [];
let match;
while ((match = pattern.exec(body)) !== null) {
if (match[1] && match[2]) {
const [owner, repo] = match[1].split("/");
issueRefs.push({ owner, repo, number: parseInt(match[2]) });
} else if (match[3]) {
issueRefs.push({ owner: context.repo.owner, repo: context.repo.repo, number: parseInt(match[3]) });
}
}
for (const ref of issueRefs) {
try {
const { repository: issueRepo } = await github.graphql(`
query($owner: String!, $repo: String!, $number: Int!) {
repository(owner: $owner, name: $repo) {
issue(number: $number) {
projectItems(first: 20) {
nodes {
project { number }
}
}
}
}
}
`, ref);
if (isOnRnaBoard(issueRepo.issue)) {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: [label],
});
return;
}
} catch (e) {
// Issue may not exist or be inaccessible
}
}