-
-
Notifications
You must be signed in to change notification settings - Fork 0
241 lines (220 loc) · 8.51 KB
/
issue-automation.yml
File metadata and controls
241 lines (220 loc) · 8.51 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
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
name: Issue Automation
on:
issues:
types: [opened, labeled, closed]
jobs:
assign-on-open:
if: github.event.action == 'opened'
runs-on: ubuntu-latest
permissions:
issues: write
contents: read
steps:
- name: Assign issue to creator
run: |
gh issue edit ${{ github.event.issue.number }} \
--add-assignee ${{ github.event.issue.user.login }} \
--repo ${{ github.repository }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Add issue to project and set status to Backlog
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
ISSUE_ID: ${{ github.event.issue.node_id }}
PROJECT_NUMBER: 4
run: |
# Get project data
PROJECT_DATA=$(gh api graphql -f query='
query($owner: String!, $number: Int!) {
user(login: $owner) {
projectV2(number: $number) {
id
fields(first: 20) {
nodes {
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
}
}
' -f owner="${{ github.repository_owner }}" -F number="$PROJECT_NUMBER")
PROJECT_ID=$(echo "$PROJECT_DATA" | jq -r '.data.user.projectV2.id')
STATUS_FIELD_ID=$(echo "$PROJECT_DATA" | jq -r '.data.user.projectV2.fields.nodes[] | select(.name == "Status") | .id')
BACKLOG_OPTION_ID=$(echo "$PROJECT_DATA" | jq -r '.data.user.projectV2.fields.nodes[] | select(.name == "Status") | .options[] | select(.name == "Backlog") | .id')
# Add issue to project
ITEM_ID=$(gh api graphql -f query='
mutation($projectId: ID!, $contentId: ID!) {
addProjectV2ItemById(input: {projectId: $projectId, contentId: $contentId}) {
item {
id
}
}
}
' -f projectId="$PROJECT_ID" -f contentId="$ISSUE_ID" | jq -r '.data.addProjectV2ItemById.item.id')
# Set status to Backlog
gh api graphql -f query='
mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $optionId: String!) {
updateProjectV2ItemFieldValue(input: {
projectId: $projectId
itemId: $itemId
fieldId: $fieldId
value: {singleSelectOptionId: $optionId}
}) {
projectV2Item {
id
}
}
}
' -f projectId="$PROJECT_ID" -f itemId="$ITEM_ID" -f fieldId="$STATUS_FIELD_ID" -f optionId="$BACKLOG_OPTION_ID"
bug-to-in-progress:
if: github.event.action == 'labeled' && github.event.label.name == 'bug'
runs-on: ubuntu-latest
permissions:
issues: write
contents: read
steps:
- name: Move bug issue to In Progress
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
ISSUE_ID: ${{ github.event.issue.node_id }}
PROJECT_NUMBER: 4
run: |
echo "Looking for issue: $ISSUE_ID"
# Get project ID and status field
PROJECT_QUERY=$(gh api graphql -f query='
query($owner: String!, $number: Int!) {
user(login: $owner) {
projectV2(number: $number) {
id
fields(first: 20) {
nodes {
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
}
}
' -f owner="${{ github.repository_owner }}" -F number="$PROJECT_NUMBER")
PROJECT_ID=$(echo "$PROJECT_QUERY" | jq -r '.data.user.projectV2.id')
STATUS_FIELD_ID=$(echo "$PROJECT_QUERY" | jq -r '.data.user.projectV2.fields.nodes[] | select(.name == "Status") | .id')
IN_PROGRESS_OPTION_ID=$(echo "$PROJECT_QUERY" | jq -r '.data.user.projectV2.fields.nodes[] | select(.name == "Status") | .options[] | select(.name == "Progress") | .id')
echo "Project ID: $PROJECT_ID"
echo "Status Field ID: $STATUS_FIELD_ID"
echo "In Progress Option ID: $IN_PROGRESS_OPTION_ID"
# Search for item with pagination
ITEM_ID=""
CURSOR=""
FOUND=false
for i in {1..10}; do
if [ -z "$CURSOR" ]; then
ITEMS_QUERY=$(gh api graphql -f query='
query($projectId: ID!) {
node(id: $projectId) {
... on ProjectV2 {
items(first: 100) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
content {
... on Issue {
id
}
}
}
}
}
}
}
' -f projectId="$PROJECT_ID")
else
ITEMS_QUERY=$(gh api graphql -f query='
query($projectId: ID!, $cursor: String!) {
node(id: $projectId) {
... on ProjectV2 {
items(first: 100, after: $cursor) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
content {
... on Issue {
id
}
}
}
}
}
}
}
' -f projectId="$PROJECT_ID" -f cursor="$CURSOR")
fi
ITEM_ID=$(echo "$ITEMS_QUERY" | jq -r --arg ISSUE_ID "$ISSUE_ID" '.data.node.items.nodes[] | select(.content.id == $ISSUE_ID) | .id')
if [ -n "$ITEM_ID" ] && [ "$ITEM_ID" != "null" ]; then
FOUND=true
break
fi
HAS_NEXT=$(echo "$ITEMS_QUERY" | jq -r '.data.node.items.pageInfo.hasNextPage')
if [ "$HAS_NEXT" = "false" ]; then
break
fi
CURSOR=$(echo "$ITEMS_QUERY" | jq -r '.data.node.items.pageInfo.endCursor')
done
if [ "$FOUND" = false ] || [ -z "$ITEM_ID" ] || [ "$ITEM_ID" = "null" ]; then
echo "Issue not found in project. This might happen if the issue was just created and hasn't been added to the project yet."
exit 0
fi
echo "Found item ID: $ITEM_ID"
# Update status to In Progress
gh api graphql -f query='
mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $optionId: String!) {
updateProjectV2ItemFieldValue(input: {
projectId: $projectId
itemId: $itemId
fieldId: $fieldId
value: {singleSelectOptionId: $optionId}
}) {
projectV2Item {
id
}
}
}
' -f projectId="$PROJECT_ID" -f itemId="$ITEM_ID" -f fieldId="$STATUS_FIELD_ID" -f optionId="$IN_PROGRESS_OPTION_ID"
echo "Successfully moved issue to In Progress"
remove-needs-thinking-on-close:
if: github.event.action == 'closed'
runs-on: ubuntu-latest
permissions:
issues: write
contents: read
steps:
- name: Remove needs thinking label if exists
run: |
if gh issue view ${{ github.event.issue.number }} --repo ${{ github.repository }} --json labels --jq '.labels[].name' | grep -q "needs thinking"; then
gh issue edit ${{ github.event.issue.number }} \
--remove-label "needs thinking" \
--repo ${{ github.repository }}
echo "Removed needs thinking label"
else
echo "No needs thinking label found"
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}