-
Notifications
You must be signed in to change notification settings - Fork 145
248 lines (230 loc) · 9.05 KB
/
project_automation_sync_pr_issues.yml
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# SPDX-FileCopyrightText: Copyright (c) 2023-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Project - Synchronize Linked Issues
on:
pull_request_target:
# Run this action when a PR is opened or edited
# Issues do not have a graphQL connection to linked PRs so we can't use that event
types: [edited]
env:
ORG: ${{ github.event.repository.owner.login }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.event.repository.name }}
# The environment vars below are hard-coded from external queries to save time + complexity here
# Note: PVT means Project V2, not "Private"
PROJECT_ID: "PVT_kwDOBkAsks4ACeio"
STATUS_FIELD_ID: "PVTSSF_lADOBkAsks4ACeiozgBbno0"
WORKING_SPRINT_FIELD_ID: "PVTIF_lADOBkAsks4ACeiozgJ_KCY"
START_SPRINT_FIELD_ID: "PVTIF_lADOBkAsks4ACeiozgI90p0"
jobs:
project_automation_sync_pr_with_issues:
runs-on: ubuntu-latest
steps:
- name: Generate Token
uses: actions/create-github-app-token@v1
id: generate-token
with:
app-id: ${{ secrets.ACTIONS_APP_ID }}
private-key: ${{ secrets.ACTIONS_APP_KEY }}
- name: Wait 1 Second
id: sleep
run: sleep 1
- name: Get PR Project ID
id: get_pr_id
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
run: |
# Query up to 10 projects for the PR
gh api graphql -f query='
query {
organization(login: "${{ env.ORG }}") {
repository(name: "${{ env.REPO }}") {
issueOrPullRequest(number: ${{ env.PR_NUMBER }}) {
... on PullRequest {
id
projectItems(first: 10) {
edges {
node {
id
project {
id
}
}
}
}
}
}
}
}
}' > project_data.json
# Filter the json result to only the project-specific ID for the PR
# A PR can be in multiple projects so we need to filter by the project ID we want
pr_id=$(jq -r '.data.organization.repository.issueOrPullRequest.projectItems.edges[] |
select(.node.project.id == "${{ env.PROJECT_ID }}") |
.node.id' project_data.json)
echo "PR_ID=$pr_id" >> $GITHUB_ENV
continue-on-error: true
- name: Get Status, Start Sprint, and Working Sprint Fields from the PR
id: get_pr_status
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
run: |
# Query the PR's status from the project
gh api graphql -f query='
query {
node(id: "${{ env.PR_ID }}") {
... on ProjectV2Item {
id
fieldValueByName(name: "Status") {
... on ProjectV2ItemFieldSingleSelectValue {
id
}
}
}
}
}' > status_field_data.json
status_option_id=$(jq -r '.data.node.fieldValueByName.id' status_field_data.json)
echo "STATUS_OPTION_ID=$status_option_id" >> $GITHUB_ENV
# Query the PR's start sprint from the project
gh api graphql -f query='
query {
node(id: "${{ env.PR_ID }}") {
... on ProjectV2Item {
id
fieldValueByName(name: "Start Sprint") {
... on ProjectV2ItemFieldIterationValue {
id
}
}
}
}
}' > start_sprint_data.json
start_sprint_option_id=$(jq -r '.data.node.fieldValueByName.id' start_sprint_data.json)
echo "START_SPRINT_OPTION_ID=$start_sprint_option_id" >> $GITHUB_ENV
# Query the PR's working sprint from the project
gh api graphql -f query='
query {
node(id: "${{ env.PR_ID }}") {
... on ProjectV2Item {
id
fieldValueByName(name: "Working Sprint") {
... on ProjectV2ItemFieldIterationValue {
id
}
}
}
}
}' > working_sprint_data.json
working_sprint_option_id=$(jq -r '.data.node.fieldValueByName.id' working_sprint_data.json)
echo "WORKING_SPRINT_OPTION_ID=$working_sprint_option_id" >> $GITHUB_ENV
continue-on-error: true
- name: Sync Linked Issues
id: update_linked_issues
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
run: |
gh api graphql -f query='
query {
organization(login: "${{ env.ORG }}") {
repository(name: "${{ env.REPO }}") {
issueOrPullRequest(number: ${{ env.PR_NUMBER }}) {
... on PullRequest {
id
closingIssuesReferences(first: 10) {
edges {
node {
id
projectItems(first: 10) {
nodes {
id
}
edges {
node {
id
project {
id
}
}
}
}
}
}
}
}
}
}
}
}' > project_data.json
issue_ids=$(jq -r '.data.organization.repository.issueOrPullRequest.closingIssuesReferences.edges[].node.projectItems.edges[] |
select(.node.project.id == "${{ env.PROJECT_ID }}") |
.node.id' project_data.json)
for issue_id in $issue_ids; do
# The query below is constructed differently than the others due to bash variable syntax + github actions syntax interactions
# Update Status
QUERY="mutation {
updateProjectV2ItemFieldValue(
input: {
projectId: \"$PROJECT_ID\"
itemId: \"$issue_id\"
fieldId: \"$STATUS_FIELD_ID\"
value: {
singleSelectOptionId: \"$STATUS_OPTION_ID\"
}
}
) {
projectV2Item {
id
}
}
}"
gh api graphql --field query="$QUERY"
# Update Start Sprint
QUERY="mutation {
updateProjectV2ItemFieldValue(
input: {
projectId: \"$PROJECT_ID\"
itemId: \"$issue_id\"
fieldId: \"$START_SPRINT_FIELD_ID\"
value: {
iterationId: \"$START_SPRINT_OPTION_ID\"
}
}
) {
projectV2Item {
id
}
}
}"
gh api graphql --field query="$QUERY"
# Update Working Sprint
QUERY="mutation {
updateProjectV2ItemFieldValue(
input: {
projectId: \"$PROJECT_ID\"
itemId: \"$issue_id\"
fieldId: \"$WORKING_SPRINT_FIELD_ID\"
value: {
iterationId: \"$WORKING_SPRINT_OPTION_ID\"
}
}
) {
projectV2Item {
id
}
}
}"
gh api graphql --field query="$QUERY"
done
continue-on-error: true