-
Notifications
You must be signed in to change notification settings - Fork 0
272 lines (237 loc) · 10.9 KB
/
workflow-issue-status.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
name: Workflow issue status
on:
workflow_call:
issues:
types:
- opened
pull_request:
types:
- opened
- synchronize
- reopened
- closed
- ready_for_review
- converted_to_draft
env:
OWNER: ai-cfia
REPO_PROJECT: github-workflows/repo_project.txt
jobs:
handle_issue_events:
runs-on: ubuntu-latest
permissions: write-all
steps:
# https://docs.github.com/en/issues/planning-and-tracking-with-projects/automating-your-project/automating-projects-using-actions
# GITHUB_TOKEN is scoped to the repository level and cannot access projects.
# To access projects you can either create a GitHub App (recommended for organization projects)
# or a personal access token (recommended for user projects).
# Workflow examples for both approaches are shown below.
- name: Generate token from Github application (GH app for workflows)
id: generate-token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ secrets.GH_WORKFLOW_APP_ID }}
private-key: ${{ secrets.GH_WORKFLOW_APP_PEM }}
- name: Checkout current repository
uses: actions/checkout@v3
- name: Checkout ai-cfia/github-workflows repo (for repo_project.txt file)
uses: actions/checkout@v3
with:
repository: ai-cfia/github-workflows
token: ${{ secrets.GITHUB_TOKEN }}
path: github-workflows
- name: Extract issue number
run: |
ISSUE_NUMBER=""
if [ "$GITHUB_EVENT_NAME" == "issues" ] && [ "$GITHUB_EVENT_ACTION" == "opened" ]; then
ISSUE_NUMBER="$GITHUB_ISSUE_NUMBER"
elif [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then
BRANCH_NAME="$GITHUB_HEAD_REF"
if [[ "$BRANCH_NAME" =~ ^[0-9]{1,} ]]; then
ISSUE_NUMBER=$(echo "$BRANCH_NAME" | grep -oE '^[0-9]+')
else
echo "1. No issue found. The branch must be created from the issue page so that its name is in the correct format (e.g., 123-my-new-feature)."
echo "2. This pull request was generated by a bot (e.g., Renovate or Dependabot), so it is expected that there isn't an associated issue linked to the pull request branch."
exit 0
fi
fi
if [ -n "$ISSUE_NUMBER" ]; then
echo "Issue number '$ISSUE_NUMBER'"
echo "ISSUE_NUMBER=$ISSUE_NUMBER" >> $GITHUB_ENV
fi
env:
GITHUB_EVENT_NAME: ${{ github.event_name }}
GITHUB_EVENT_ACTION: ${{ github.event.action }}
GITHUB_ISSUE_NUMBER: ${{ github.event.issue.number }}
GITHUB_HEAD_REF: ${{ github.event.pull_request.head.ref }}
- name: Fetch issue project(s) information(s)
if: ${{ env.ISSUE_NUMBER != '' }}
run: |
ISSUE_NUMBER="${ISSUE_NUMBER}"
OWNER="${OWNER}"
REPO_NAME="${{ github.repository }}"
FILE="${REPO_PROJECT}"
declare -A PROJECT_NUMBER_ID_MAP ITEM_ID_MAP STATUS_FIELD_ID_MAP STATUS_OPTIONS_MAP
echo "1. Project associated with this issue number #$ISSUE_NUMBER"
PROJECT_ITEMS_JSON=$(gh issue view "$ISSUE_NUMBER" --json projectItems)
if [ "$(echo "$PROJECT_ITEMS_JSON" | jq '.projectItems | length')" -eq 0 ]; then
echo "The projectItems list is empty. Assigning project from '$REPO_PROJECT'"
PROJECT=$(grep "^${REPO_NAME}_" "$FILE" | cut -d'_' -f2)
if [ -n "$PROJECT" ]; then
gh issue edit "$ISSUE_NUMBER" --add-project "$PROJECT"
PROJECT_ITEMS_JSON=$(gh issue view "$ISSUE_NUMBER" --json projectItems)
echo "Assigned project $PROJECT to issue $ISSUE_NUMBER."
else
echo "No matching project found for repository $REPO_NAME."
fi
fi
PROJECT_TITLES=$(echo "$PROJECT_ITEMS_JSON" | jq -r '.projectItems[].title')
echo "$PROJECT_TITLES"
echo "2. Project information"
PROJECT_LIST=$(gh project list --owner "$OWNER")
while read -r TITLE; do
PROJECT_LINE=$(echo "$PROJECT_LIST" | grep -F "$TITLE")
if [ -z "$PROJECT_LINE" ]; then
echo "Project '$TITLE' not found. Skipping..."
continue
fi
PROJECT_NUMBER=$(echo "$PROJECT_LINE" | awk '{print $1}')
PROJECT_ID=$(echo "$PROJECT_LINE" | awk '{print $NF}')
echo "Project '$TITLE' found with # '$PROJECT_NUMBER' and ID '$PROJECT_ID'"
PROJECT_NUMBER_ID_MAP[$PROJECT_NUMBER]=$PROJECT_ID
done <<< "$PROJECT_TITLES"
echo "3. Item id associated to each project for a specific issue and keep the associated ID of the related-project"
CURRENT_REPOSITORY="${{ github.repository }}"
echo "3.5 Current repository: '$CURRENT_REPOSITORY'"
for PROJECT_NUMBER in "${!PROJECT_NUMBER_ID_MAP[@]}"; do
PROJECT_ID=${PROJECT_NUMBER_ID_MAP[$PROJECT_NUMBER]}
echo "Searching #$ISSUE_NUMBER in '$PROJECT_NUMBER'"
ITEMS_JSON=$(gh project item-list "$PROJECT_NUMBER" --limit 2000 --owner "$OWNER" --format json)
ITEM_ENTRY=$(echo "$ITEMS_JSON" | jq -r --argjson ISSUE_NUMBER "$ISSUE_NUMBER" --arg REPO "$CURRENT_REPOSITORY" \
'.items[] | select(.content.number == $ISSUE_NUMBER and (.content.repository == $REPO))')
ITEM_ID=$(echo "$ITEM_ENTRY" | jq -r '.id')
if [ -z "$ITEM_ENTRY" ]; then
echo "Issue #$ISSUE_NUMBER with repository '$CURRENT_REPOSITORY' not found in '$PROJECT_NUMBER'. Skipping..."
continue
fi
echo "Item found #$ISSUE_NUMBER in '$PROJECT_NUMBER' with ID: '$ITEM_ID'"
ITEM_ID_MAP[$PROJECT_ID]=$ITEM_ID
done
echo "4. Find 'Status' field"
for PROJECT_ID in "${!ITEM_ID_MAP[@]}"; do
FIELDS_JSON=$(gh api graphql -f query='
query {
node(id: "'"$PROJECT_ID"'") {
... on ProjectV2 {
fields(first: 20) {
nodes {
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
}
}
')
STATUS_FIELD_ENTRY=$(echo "$FIELDS_JSON" | jq '.data.node.fields.nodes[] | select(.name=="Status")')
STATUS_FIELD_ID=$(echo "$STATUS_FIELD_ENTRY" | jq -r '.id')
if [ -z "$STATUS_FIELD_ID" ] || [ "$STATUS_FIELD_ID" == "null" ]; then
echo "Status not found in '$PROJECT_ID'. Skipping..."
continue
fi
echo "'Status' field found for project ID '$PROJECT_ID': '$STATUS_FIELD_ID'"
OPTIONS_JSON=$(echo "$STATUS_FIELD_ENTRY" | jq -c '.options')
STATUS_FIELD_ID_MAP[$PROJECT_ID]=$STATUS_FIELD_ID
STATUS_OPTIONS_MAP[$PROJECT_ID]=$OPTIONS_JSON
done
echo "5. Update item card for each project"
EVENT_NAME="${{ github.event_name }}"
EVENT_ACTION="${{ github.event.action }}"
for PROJECT_ID in "${!ITEM_ID_MAP[@]}"; do
ITEM_ID=${ITEM_ID_MAP[$PROJECT_ID]}
STATUS_FIELD_ID=${STATUS_FIELD_ID_MAP[$PROJECT_ID]}
OPTIONS=${STATUS_OPTIONS_MAP[$PROJECT_ID]}
if [ -z "$STATUS_FIELD_ID" ]; then
echo "STATUS_FIELD_ID for project $PROJECT_ID not found. Skipping..."
continue
fi
TARGET_STATUS_NAME=""
case "$EVENT_NAME" in
"issues")
case "$EVENT_ACTION" in
"opened")
echo "Targeted field name: Todo"
TARGET_STATUS_NAME="Todo"
;;
esac
;;
"pull_request")
case "$EVENT_ACTION" in
"opened")
if [ "${{ github.event.pull_request.draft }}" == "true" ]; then
echo "Targeted field name: In Progress"
TARGET_STATUS_NAME="In Progress"
else
echo "Targeted field name: In Review"
TARGET_STATUS_NAME="In Review"
fi
;;
"ready_for_review")
echo "Targeted field name: In Review"
TARGET_STATUS_NAME="In Review"
;;
"converted_to_draft")
echo "Targeted field name: In Progress"
TARGET_STATUS_NAME="In Progress"
;;
"reopened")
echo "Targeted field name: In Progress"
TARGET_STATUS_NAME="In Progress"
;;
"closed")
if [ "${{ github.event.pull_request.merged }}" == "true" ]; then
echo "Targeted field name: Done"
TARGET_STATUS_NAME="Done"
else
echo "Targeted field name: Won't do"
TARGET_STATUS_NAME="Won't do"
fi
;;
"synchronize")
STATUS=$(gh pr view ${{ github.event.pull_request.number }} --json isDraft --jq '.isDraft')
if [ "$STATUS" == "false" ]; then
echo "Targeted field name: In Review"
TARGET_STATUS_NAME="In Review"
else
echo "Targeted field name: In Progress"
TARGET_STATUS_NAME="In Progress"
fi
;;
esac
;;
esac
if [ -z "$TARGET_STATUS_NAME" ]; then
echo "No appropriate status for event $EVENT_NAME/$EVENT_ACTION. Use case not managed."
exit 1
fi
TARGET_STATUS_NAME_LOWER=$(echo "$TARGET_STATUS_NAME" | tr '[:upper:]' '[:lower:]')
NEW_OPTION_ID=$(echo "$OPTIONS" | jq -r --arg name "$TARGET_STATUS_NAME_LOWER" '.[] | select(.name | ascii_downcase == $name) | .id')
if [ -z "$NEW_OPTION_ID" ]; then
echo "Could not find status '$TARGET_STATUS_NAME'. Using default ID."
exit 1
else
echo "Found status '$TARGET_STATUS_NAME' with ID: $NEW_OPTION_ID"
fi
echo "Updating..."
gh project item-edit --id "$ITEM_ID" --project-id "$PROJECT_ID" --field-id "$STATUS_FIELD_ID" --single-select-option-id "$NEW_OPTION_ID"
echo "Done!"
done
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
ISSUE_NUMBER: ${{ env.ISSUE_NUMBER }}
OWNER: ${{ env.OWNER }}