-
Notifications
You must be signed in to change notification settings - Fork 0
165 lines (145 loc) · 5.23 KB
/
move-issue-on-pullrequest.yml
File metadata and controls
165 lines (145 loc) · 5.23 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
name: Move Issue to In Review on PR
on:
pull_request:
types: [opened, reopened]
jobs:
move_issue:
runs-on: ubuntu-latest
steps:
- name: Move issue to In Review and remove PR card
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GH_TOKEN }}
script: |
const prBody = context.payload.pull_request.body ?? ''
const prNodeId = context.payload.pull_request.node_id
const owner = context.repo.owner
const repo = context.repo.repo
const match = prBody.match(/#(\d+)/)
if (!match) {
core.info('No linked issue found in PR body')
return
}
const issueNumber = parseInt(match[1])
// Resolve the issue node ID
const issueData = await github.graphql(`
query($owner: String!, $repo: String!, $number: Int!) {
repository(owner: $owner, name: $repo) {
issue(number: $number) {
id
}
}
}
`, { owner, repo, number: issueNumber })
const issueNodeId = issueData.repository.issue.id
// Fetch project metadata: id, Status field and its options
const projectData = await github.graphql(`
query($owner: String!) {
user(login: $owner) {
projectV2(number: 2) {
id
fields(first: 20) {
nodes {
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
}
}
`, { owner })
const project = projectData.user.projectV2
const projectId = project.id
const statusField = project.fields.nodes.find(f => f.name === 'Status')
if (!statusField) {
core.setFailed('Status field not found in project')
return
}
const inReviewOption = statusField.options.find(o => o.name === 'In Review')
if (!inReviewOption) {
core.setFailed('"In Review" option not found in Status field')
return
}
// Find the project item that corresponds to the issue
const issueItemData = await github.graphql(`
query($nodeId: ID!) {
node(id: $nodeId) {
... on Issue {
projectItems(first: 50) {
nodes {
id
project {
id
}
}
}
}
}
}
`, { nodeId: issueNodeId })
const issueItem = issueItemData.node.projectItems.nodes.find(
item => item.project.id === projectId
)
if (!issueItem) {
core.info(`Issue #${issueNumber} is not tracked in the project board`)
return
}
// Move the issue to "In Review"
await github.graphql(`
mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $optionId: String!) {
updateProjectV2ItemFieldValue(input: {
projectId: $projectId
itemId: $itemId
fieldId: $fieldId
value: { singleSelectOptionId: $optionId }
}) {
projectV2Item {
id
}
}
}
`, {
projectId,
itemId: issueItem.id,
fieldId: statusField.id,
optionId: inReviewOption.id
})
core.info(`Issue #${issueNumber} moved to "In Review"`)
// Remove the PR card from the board if GitHub auto-added it
const prItemData = await github.graphql(`
query($nodeId: ID!) {
node(id: $nodeId) {
... on PullRequest {
projectItems(first: 50) {
nodes {
id
project {
id
}
}
}
}
}
}
`, { nodeId: prNodeId })
const prItem = prItemData.node.projectItems.nodes.find(
item => item.project.id === projectId
)
if (prItem) {
await github.graphql(`
mutation($projectId: ID!, $itemId: ID!) {
deleteProjectV2Item(input: { projectId: $projectId, itemId: $itemId }) {
deletedItemId
}
}
`, { projectId, itemId: prItem.id })
core.info('PR card removed from project board')
} else {
core.info('PR was not present as a card on the project board')
}