|
1
|
name: Close Linked PRs on Merge |
|
2
|
|
|
3
|
# When a PR that closes an issue is merged, this workflow finds other open PRs |
|
4
|
# that are linked to the same issue (via a closing keyword such as "Fixes #123") |
|
5
|
# and closes them with a comment pointing at the PR that fixed the issue. |
|
6
|
# |
|
7
|
# Uses `pull_request_target` so the GITHUB_TOKEN has write access to |
|
8
|
# pull requests/issues even when the merged PR came from a fork. The workflow |
|
9
|
# never checks out or executes PR code, so this is safe. |
|
10
|
|
|
11
|
on: |
|
12
|
pull_request_target: |
|
13
|
types: [closed] |
|
14
|
|
|
15
|
concurrency: |
|
16
|
group: close-linked-prs-${{ github.event.pull_request.number }} |
|
17
|
cancel-in-progress: false |
|
18
|
|
|
19
|
permissions: |
|
20
|
contents: read |
|
21
|
pull-requests: write |
|
22
|
issues: write |
|
23
|
|
|
24
|
jobs: |
|
25
|
close-linked-prs: |
|
26
|
if: github.event.pull_request.merged == true |
|
27
|
runs-on: ubuntu-latest |
|
28
|
steps: |
|
29
|
- name: Close PRs linked to the same issue |
|
30
|
uses: actions/github-script@v7 |
|
31
|
with: |
|
32
|
github-token: ${{ secrets.GITHUB_TOKEN }} |
|
33
|
script: | |
|
34
|
const { owner, repo } = context.repo |
|
35
|
const mergedPr = context.payload.pull_request.number |
|
36
|
|
|
37
|
// 1. Find every issue the merged PR closes (via closing keywords). |
|
38
|
const mergedPrData = await github.graphql( |
|
39
|
`query ($owner: String!, $repo: String!, $pr: Int!) { |
|
40
|
repository(owner: $owner, name: $repo) { |
|
41
|
pullRequest(number: $pr) { |
|
42
|
closingIssuesReferences(first: 50) { |
|
43
|
nodes { number } |
|
44
|
} |
|
45
|
} |
|
46
|
} |
|
47
|
}`, |
|
48
|
{ owner, repo, pr: mergedPr } |
|
49
|
) |
|
50
|
|
|
51
|
const issueNumbers = |
|
52
|
mergedPrData.repository.pullRequest.closingIssuesReferences.nodes.map( |
|
53
|
(n) => n.number |
|
54
|
) |
|
55
|
|
|
56
|
if (issueNumbers.length === 0) { |
|
57
|
core.info(`PR #${mergedPr} does not close any issue. Nothing to do.`) |
|
58
|
return |
|
59
|
} |
|
60
|
|
|
61
|
core.info( |
|
62
|
`PR #${mergedPr} closes issue(s): ${issueNumbers |
|
63
|
.map((n) => `#${n}`) |
|
64
|
.join(", ")}` |
|
65
|
) |
|
66
|
|
|
67
|
// 2. For each linked issue, collect other open PRs that also link to it |
|
68
|
// with a closing keyword. Dedupe across issues and map each PR to the |
|
69
|
// issue it shares with the merged PR (for a clearer comment). |
|
70
|
const prToIssue = new Map() |
|
71
|
|
|
72
|
for (const issue of issueNumbers) { |
|
73
|
const issueData = await github.graphql( |
|
74
|
`query ($owner: String!, $repo: String!, $issue: Int!) { |
|
75
|
repository(owner: $owner, name: $repo) { |
|
76
|
issue(number: $issue) { |
|
77
|
timelineItems( |
|
78
|
first: 100 |
|
79
|
itemTypes: [CROSS_REFERENCED_EVENT, CONNECTED_EVENT] |
|
80
|
) { |
|
81
|
nodes { |
|
82
|
__typename |
|
83
|
... on CrossReferencedEvent { |
|
84
|
source { |
|
85
|
... on PullRequest { |
|
86
|
number |
|
87
|
state |
|
88
|
closingIssuesReferences(first: 50) { nodes { number } } |
|
89
|
} |
|
90
|
} |
|
91
|
} |
|
92
|
... on ConnectedEvent { |
|
93
|
subject { |
|
94
|
... on PullRequest { |
|
95
|
number |
|
96
|
state |
|
97
|
closingIssuesReferences(first: 50) { nodes { number } } |
|
98
|
} |
|
99
|
} |
|
100
|
} |
|
101
|
} |
|
102
|
} |
|
103
|
} |
|
104
|
} |
|
105
|
}`, |
|
106
|
{ owner, repo, issue } |
|
107
|
) |
|
108
|
|
|
109
|
const nodes = issueData.repository.issue.timelineItems.nodes |
|
110
|
for (const node of nodes) { |
|
111
|
const pr = node.source ?? node.subject |
|
112
|
if (!pr || !pr.number) continue |
|
113
|
if (pr.number === mergedPr) continue |
|
114
|
if (pr.state !== "OPEN") continue |
|
115
|
|
|
116
|
// Only close PRs that actually target the same issue with a |
|
117
|
// closing keyword, not ones that merely mention it. |
|
118
|
const closesSameIssue = pr.closingIssuesReferences.nodes.some( |
|
119
|
(n) => n.number === issue |
|
120
|
) |
|
121
|
if (!closesSameIssue) continue |
|
122
|
|
|
123
|
if (!prToIssue.has(pr.number)) { |
|
124
|
prToIssue.set(pr.number, issue) |
|
125
|
} |
|
126
|
} |
|
127
|
} |
|
128
|
|
|
129
|
if (prToIssue.size === 0) { |
|
130
|
core.info("No other open PRs are linked to the same issue(s).") |
|
131
|
return |
|
132
|
} |
|
133
|
|
|
134
|
// 3. Comment on and close each linked PR. |
|
135
|
for (const [prNumber, issue] of prToIssue) { |
|
136
|
const body = |
|
137
|
`Closing this PR because #${issue} was fixed by #${mergedPr}, ` + |
|
138
|
`which has now been merged. Thank you for your contribution! ` + |
|
139
|
`If you believe this PR is still needed, feel free to reopen it.` |
|
140
|
|
|
141
|
await github.rest.issues.createComment({ |
|
142
|
owner, |
|
143
|
repo, |
|
144
|
issue_number: prNumber, |
|
145
|
body, |
|
146
|
}) |
|
147
|
|
|
148
|
await github.rest.pulls.update({ |
|
149
|
owner, |
|
150
|
repo, |
|
151
|
pull_number: prNumber, |
|
152
|
state: "closed", |
|
153
|
}) |
|
154
|
|
|
155
|
core.info(`Closed PR #${prNumber} (linked to issue #${issue}).`) |
|
156
|
} |