-
Notifications
You must be signed in to change notification settings - Fork 243
208 lines (196 loc) · 9.29 KB
/
issue-management.yaml
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
name: Issue Management
on:
issues:
types:
- opened
- reopened
- labeled
- unlabeled
env:
ORGANIZATION: redhat-developer
# See https://github.com/redhat-developer/odo/projects?query=is%3Aopen
PROJECT_NUMBER: 16
jobs:
manage_issue_labels:
name: Label issue
if: ${{ github.event.action == 'opened' || github.event.action == 'reopened' }}
runs-on: ubuntu-latest
concurrency: issue_labels-${{ github.event.issue.number }}
permissions:
issues: write
steps:
- name: Add needs-triage label
# Action recommended in https://docs.github.com/en/actions/managing-issues-and-pull-requests/adding-labels-to-issues
# Recommended to pin unofficial Actions to a specific commit SHA
uses: andymckay/labeler@3a4296e9dcdf9576b0456050db78cfd34853f260
with:
add-labels: "needs-triage"
repo-token: ${{ secrets.GITHUB_TOKEN }}
manage_issue_in_project:
name: Manage issue in Project
runs-on: ubuntu-latest
if: ${{ always() }}
needs: manage_issue_labels
concurrency: issue_management_in_project-${{ github.event.issue.number }}-${{ github.event.action }}
env:
# Personal Access Token (PAT) to be created with 'repo' and 'project' scopes and be added as repository secret.
GITHUB_TOKEN: ${{ secrets.PROJECT_MANAGEMENT_TOKEN }}
steps:
- name: Get project data
run: |
gh api graphql -f query='
query($org: String!, $number: Int!) {
organization(login: $org){
projectV2(number: $number) {
id
fields(first:20) {
nodes {
... on ProjectV2Field {
id
name
}
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
}
}' -f org=$ORGANIZATION -F number=$PROJECT_NUMBER > project_data.json
cat project_data.json
echo 'PROJECT_ID='$(jq '.data.organization.projectV2.id' project_data.json) >> $GITHUB_ENV
echo 'PRIORITY_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Priority") |.id' project_data.json) >> $GITHUB_ENV
echo 'PRIORITY_URGENT_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Priority") | .options[] | select(.name | startswith("Urgent")) |.id' project_data.json) >> $GITHUB_ENV
echo 'PRIORITY_HIGH_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Priority") | .options[] | select(.name | startswith("High")) |.id' project_data.json) >> $GITHUB_ENV
echo 'PRIORITY_MEDIUM_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Priority") | .options[] | select(.name | startswith("Medium")) |.id' project_data.json) >> $GITHUB_ENV
echo 'PRIORITY_LOW_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Priority") | .options[] | select(.name | startswith("Low")) |.id' project_data.json) >> $GITHUB_ENV
- name: Add issue to Project
env:
ISSUE_ID: ${{ github.event.issue.node_id }}
run: |
gh api graphql -f query='
mutation($project: ID!, $issue: ID!) {
addProjectV2ItemById(
input: {
projectId: $project
contentId: $issue
}
) {
item {
id
}
}
}' -f project=${{ env.PROJECT_ID }} -f issue=$ISSUE_ID > project_mutation_result.json
cat project_mutation_result.json
echo 'ITEM_ID='$(jq '.data.addProjectV2ItemById.item.id' project_mutation_result.json) >> $GITHUB_ENV
- name: Set Priority field in Project based on label added
if: ${{ github.event.action == 'labeled' && startsWith(github.event.label.name, 'priority/') }}
env:
ISSUE_ID: ${{ github.event.issue.node_id }}
PRIORITY_LABEL: ${{ github.event.label.name }}
run: |
if [[ "$PRIORITY_LABEL" == "priority/critical-urgent" ]] || [[ "$PRIORITY_LABEL" == "priority/Critical" ]]; then
echo Setting Urgent priority value: ${{ env.PRIORITY_URGENT_OPTION_ID }}
export priority_field_value=${{ env.PRIORITY_URGENT_OPTION_ID }}
elif [[ "$PRIORITY_LABEL" == "priority/High" ]]; then
echo Setting High priority value: ${{ env.PRIORITY_HIGH_OPTION_ID }}
export priority_field_value=${{ env.PRIORITY_HIGH_OPTION_ID }}
elif [[ "$PRIORITY_LABEL" == "priority/Medium" ]]; then
echo Setting Medium priority value: ${{ env.PRIORITY_MEDIUM_OPTION_ID }}
export priority_field_value=${{ env.PRIORITY_MEDIUM_OPTION_ID }}
elif [[ "$PRIORITY_LABEL" == "priority/Low" ]]; then
echo Setting Low priority value: ${{ env.PRIORITY_LOW_OPTION_ID }}
export priority_field_value=${{ env.PRIORITY_LOW_OPTION_ID }}
else
echo "Ignoring unknown priority label value: $PRIORITY_LABEL"
fi
echo "priority_field_value: $priority_field_value"
if [ -n "$priority_field_value" ]; then
gh api graphql -f query='
mutation($project: ID!, $item: ID!, $priority_field: ID!, $priority_value: String!) {
updateProjectV2ItemFieldValue(
input: {
projectId: $project
itemId: $item
fieldId: $priority_field
value: {
singleSelectOptionId: $priority_value
}
}
) {
projectV2Item {
id
}
}
}' -f project=${{ env.PROJECT_ID }} -f item=${{ env.ITEM_ID }} -f priority_field=${{ env.PRIORITY_FIELD_ID }} -f priority_value=$priority_field_value
fi
- name: Set Priority field in Project based on label removed
if: ${{ github.event.action == 'unlabeled' && startsWith(github.event.label.name, 'priority/') }}
env:
ISSUE_ID: ${{ github.event.issue.node_id }}
run: |
# Find an already existing label for that issue, and set the field in the Project. Otherwise, clear the field.
priorityLabels=$(gh issue view ${{ github.event.issue.number }} -R ${GITHUB_REPOSITORY} --json labels --jq '.labels.[] | select(.name | startswith("priority/")) |.name')
if [ -n "$priorityLabels" ]; then
echo "Handling priority labels: $priorityLabels"
for priorityLabel in $priorityLabels; do
# The last value wins
if [[ "$priorityLabel" == "priority/critical-urgent" ]] || [[ "$priorityLabel" == "priority/Critical" ]]; then
echo Setting Urgent priority value: ${{ env.PRIORITY_URGENT_OPTION_ID }}
export priority_field_value=${{ env.PRIORITY_URGENT_OPTION_ID }}
elif [[ "$priorityLabel" == "priority/High" ]]; then
echo Setting High priority value: ${{ env.PRIORITY_HIGH_OPTION_ID }}
export priority_field_value=${{ env.PRIORITY_HIGH_OPTION_ID }}
elif [[ "$priorityLabel" == "priority/Medium" ]]; then
echo Setting Medium priority value: ${{ env.PRIORITY_MEDIUM_OPTION_ID }}
export priority_field_value=${{ env.PRIORITY_MEDIUM_OPTION_ID }}
elif [[ "$priorityLabel" == "priority/Low" ]]; then
echo Setting Low priority value: ${{ env.PRIORITY_LOW_OPTION_ID }}
export priority_field_value=${{ env.PRIORITY_LOW_OPTION_ID }}
else
echo "Ignoring unknown priority label value: $priorityLabel"
fi
echo "priority_field_value: $priority_field_value"
if [ -n "$priority_field_value" ]; then
gh api graphql -f query='
mutation($project: ID!, $item: ID!, $priority_field: ID!, $priority_value: String!) {
updateProjectV2ItemFieldValue(
input: {
projectId: $project
itemId: $item
fieldId: $priority_field
value: {
singleSelectOptionId: $priority_value
}
}
) {
projectV2Item {
id
}
}
}' -f project=${{ env.PROJECT_ID }} -f item=${{ env.ITEM_ID }} -f priority_field=${{ env.PRIORITY_FIELD_ID }} -f priority_value=$priority_field_value
fi
done
else
# Clear the field
echo "Found no priority labels => clearing the field in the Project"
gh api graphql -f query='
mutation($project: ID!, $item: ID!, $priority_field: ID!) {
clearProjectV2ItemFieldValue(
input: {
projectId: $project
itemId: $item
fieldId: $priority_field
}
) {
projectV2Item {
id
}
}
}' -f project=${{ env.PROJECT_ID }} -f item=${{ env.ITEM_ID }} -f priority_field=${{ env.PRIORITY_FIELD_ID }}
fi