-
Notifications
You must be signed in to change notification settings - Fork 1
459 lines (419 loc) · 20.5 KB
/
manage-issues.yaml
File metadata and controls
459 lines (419 loc) · 20.5 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# Copyright 2024 Zaphiro Technologies
#
# 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: Manage Issues and PRs
on:
schedule:
- cron: "0 0 * * *" # Run daily at midnight
workflow_dispatch:
inputs:
dry-run:
description: "Enable dry-run mode for stale issue detection (no actual changes)"
required: false
default: false
type: boolean
stale-days:
description: "Number of days before an issue is considered stale"
required: false
default: 60
type: number
close-after-days:
description: "Number of days after marking stale before closing"
required: false
default: 7
type: number
skip-label:
description: "Label to skip stale processing (issues with this label won't be marked stale or closed)"
required: false
default: "keep"
type: string
workflow_call:
inputs:
dry-run:
description: "Enable dry-run mode for stale issue detection (no actual changes)"
required: false
default: false
type: boolean
stale-days:
description: "Number of days before an issue is considered stale"
required: false
default: 60
type: number
close-after-days:
description: "Number of days after marking stale before closing"
required: false
default: 7
type: number
skip-label:
description: "Label to skip stale processing (issues with this label won't be marked stale or closed)"
required: false
default: "keep"
type: string
jobs:
manage-stale-issues:
name: Manage Stale Unplanned Issues
runs-on: ubuntu-latest
steps:
- uses: actions/create-github-app-token@v3
id: generate-token
with:
app-id: ${{ secrets.APP_ID }}
private-key: ${{ secrets.APP_SECRET }}
- name: Manage Stale Issues
env:
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
STALE_DAYS: ${{ inputs.stale-days || 60 }}
CLOSE_AFTER_DAYS: ${{ inputs.close-after-days || 7 }}
SKIP_LABEL: ${{ inputs.skip-label || 'keep' }}
DRY_RUN: ${{ inputs.dry-run || 'false' }}
run: |
# Calculate the date based on STALE_DAYS
STALE_DATE=$(date -u -d "${STALE_DAYS} days ago" +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date -u -v-${STALE_DAYS}d +%Y-%m-%dT%H:%M:%SZ)
if [ "$DRY_RUN" = "true" ]; then
echo "🔍 DRY-RUN MODE ENABLED - No changes will be made"
echo "═══════════════════════════════════════════════════"
fi
echo "Looking for issues older than $STALE_DATE (${STALE_DAYS} days ago)..."
echo "Issues will be closed ${CLOSE_AFTER_DAYS} days after being marked stale..."
# Calculate close threshold once (used for all stale issues)
CLOSE_THRESHOLD=$(date -d "${CLOSE_AFTER_DAYS} days ago" +%s 2>/dev/null || date -v-${CLOSE_AFTER_DAYS}d +%s)
# Query to get all open issues with their project data
CURSOR=""
while :; do
if [ -z "$CURSOR" ]; then
QUERY='
query($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
issues(first: 100, states: OPEN) {
pageInfo {
hasNextPage
endCursor
}
nodes {
number
title
createdAt
updatedAt
labels(first: 20) {
nodes { name }
}
projectItems(first: 5) {
nodes {
id
iteration: fieldValueByName(name: "Iteration") {
... on ProjectV2ItemFieldIterationValue {
title
startDate
duration
}
}
status: fieldValueByName(name: "Status") {
... on ProjectV2ItemFieldSingleSelectValue { name }
}
}
}
}
}
}
}
'
else
QUERY='
query($owner: String!, $repo: String!, $cursor: String!) {
repository(owner: $owner, name: $repo) {
issues(first: 100, states: OPEN, after: $cursor) {
pageInfo {
hasNextPage
endCursor
}
nodes {
number
title
createdAt
updatedAt
labels(first: 20) {
nodes { name }
}
projectItems(first: 5) {
nodes {
id
iteration: fieldValueByName(name: "Iteration") {
... on ProjectV2ItemFieldIterationValue {
title
startDate
duration
}
}
status: fieldValueByName(name: "Status") {
... on ProjectV2ItemFieldSingleSelectValue { name }
}
}
}
}
}
}
}
'
fi
if [ -z "$CURSOR" ]; then
PAGE=$(gh api graphql -f owner="$OWNER" -f repo="$REPO" -f query="$QUERY")
else
PAGE=$(gh api graphql -f owner="$OWNER" -f repo="$REPO" -f cursor="$CURSOR" -f query="$QUERY")
fi
# Process issues from this page
echo "$PAGE" | jq -c '.data.repository.issues.nodes[]' | while read -r issue; do
NUMBER=$(echo "$issue" | jq -r '.number')
TITLE=$(echo "$issue" | jq -r '.title')
CREATED_AT=$(echo "$issue" | jq -r '.createdAt')
UPDATED_AT=$(echo "$issue" | jq -r '.updatedAt') # updatedAt changes on comments, label changes (including from this workflow), edits, etc., so bot actions can reset staleness.
# Check if issue has skip label - if so, skip all stale processing
HAS_SKIP_LABEL=$(echo "$issue" | jq -r --arg SKIP_LABEL "$SKIP_LABEL" '.labels.nodes[].name | select(. == $SKIP_LABEL)')
if [ -n "$HAS_SKIP_LABEL" ]; then
echo "Issue #$NUMBER: Has skip label '$SKIP_LABEL' - skipping stale processing"
continue
fi
# Check if issue already has stale label
HAS_STALE_LABEL=$(echo "$issue" | jq -r '.labels.nodes[].name | select(. == "stale")')
# Check timestamps
CREATED_TIMESTAMP=$(date -d "$CREATED_AT" +%s 2>/dev/null || date -j -f "%Y-%m-%dT%H:%M:%SZ" "$CREATED_AT" +%s)
UPDATED_TIMESTAMP=$(date -d "$UPDATED_AT" +%s 2>/dev/null || date -j -f "%Y-%m-%dT%H:%M:%SZ" "$UPDATED_AT" +%s)
STALE_TIMESTAMP=$(date -d "$STALE_DATE" +%s 2>/dev/null || date -j -f "%Y-%m-%dT%H:%M:%SZ" "$STALE_DATE" +%s)
# If issue has stale label, check if a plan was added or if it should be closed
if [ -n "$HAS_STALE_LABEL" ]; then
# First, check if issue now has a current or future iteration (was planned meanwhile)
HAS_PLANNED_ITERATION=false
CURRENT_DATE=${CURRENT_DATE:-$(date -u +%Y-%m-%d)}
# Use process substitution to avoid subshell issues
while read -r projectItem; do
ITERATION_TITLE=$(echo "$projectItem" | jq -r '.iteration.title // empty')
ITERATION_START=$(echo "$projectItem" | jq -r '.iteration.startDate // empty')
ITERATION_DURATION=$(echo "$projectItem" | jq -r '.iteration.duration // empty')
if [ -n "$ITERATION_START" ] && [ -n "$ITERATION_DURATION" ]; then
# Calculate iteration end date
ITERATION_END=$(date -d "$ITERATION_START + $ITERATION_DURATION days" +%Y-%m-%d 2>/dev/null || date -j -v+${ITERATION_DURATION}d -f "%Y-%m-%d" "$ITERATION_START" +%Y-%m-%d)
# Check if iteration is current or future
if [[ "$ITERATION_END" > "$CURRENT_DATE" ]] || [[ "$ITERATION_END" == "$CURRENT_DATE" ]]; then
HAS_PLANNED_ITERATION=true
echo "Issue #$NUMBER: Was stale but now planned for iteration '$ITERATION_TITLE' (ends: $ITERATION_END)"
break
fi
fi
done < <(echo "$issue" | jq -c '.projectItems.nodes[]')
# If issue is now planned, remove stale label
if [ "$HAS_PLANNED_ITERATION" = true ]; then
if [ "$DRY_RUN" = "true" ]; then
echo "[DRY-RUN] Issue #$NUMBER: Would remove stale label (now has planned iteration)"
else
echo "Issue #$NUMBER: Removing stale label (now has planned iteration)"
gh issue edit "$NUMBER" --repo "$OWNER/$REPO" --remove-label "stale"
fi
continue
fi
# If not planned, check if it should be closed
if [ "$UPDATED_TIMESTAMP" -lt "$CLOSE_THRESHOLD" ]; then
if [ "$DRY_RUN" = "true" ]; then
echo "[DRY-RUN] Issue #$NUMBER: Would close stale issue (no updates in ${CLOSE_AFTER_DAYS}+ days)"
echo "[DRY-RUN] - Would close as 'not planned' with comment"
else
echo "Issue #$NUMBER: Closing stale issue (no updates in ${CLOSE_AFTER_DAYS}+ days)"
gh issue close "$NUMBER" --repo "$OWNER/$REPO" --reason "not planned" --comment "Closing this issue as it has been stale for more than ${CLOSE_AFTER_DAYS} days without updates or being added to an iteration. Feel free to reopen if this is still relevant."
fi
else
echo "Issue #$NUMBER: Has stale label but recently updated (updated: $UPDATED_AT) - will check again later"
fi
continue
fi
# If issue doesn't have stale label, check if it should be marked as stale
# Check both creation and last update - if either is recent, don't mark as stale
if [ "$CREATED_TIMESTAMP" -gt "$STALE_TIMESTAMP" ]; then
echo "Issue #$NUMBER: Not old enough (created: $CREATED_AT)"
continue
fi
if [ "$UPDATED_TIMESTAMP" -gt "$STALE_TIMESTAMP" ]; then
echo "Issue #$NUMBER: Recently updated (updated: $UPDATED_AT) - not marking as stale"
continue
fi
# Check if issue has a current or future iteration
HAS_PLANNED_ITERATION=false
# Use process substitution to avoid subshell issues
while read -r projectItem; do
ITERATION_TITLE=$(echo "$projectItem" | jq -r '.iteration.title // empty')
ITERATION_START=$(echo "$projectItem" | jq -r '.iteration.startDate // empty')
ITERATION_DURATION=$(echo "$projectItem" | jq -r '.iteration.duration // empty')
if [ -n "$ITERATION_START" ] && [ -n "$ITERATION_DURATION" ]; then
# Calculate iteration end date
ITERATION_END=$(date -d "$ITERATION_START + $ITERATION_DURATION days" +%Y-%m-%d 2>/dev/null || date -j -v+${ITERATION_DURATION}d -f "%Y-%m-%d" "$ITERATION_START" +%Y-%m-%d)
# Check if iteration is current or future
if [[ "$ITERATION_END" > "$CURRENT_DATE" ]] || [[ "$ITERATION_END" == "$CURRENT_DATE" ]]; then
HAS_PLANNED_ITERATION=true
echo "Issue #$NUMBER: Planned for iteration '$ITERATION_TITLE' (ends: $ITERATION_END)"
break
fi
fi
done < <(echo "$issue" | jq -c '.projectItems.nodes[]')
# If old and not planned for current/future iteration, mark as stale
if [ "$HAS_PLANNED_ITERATION" = false ]; then
if [ "$DRY_RUN" = "true" ]; then
echo "[DRY-RUN] Issue #$NUMBER: Would mark as stale (created: $CREATED_AT, no planned iteration)"
echo "[DRY-RUN] - Would add label: stale"
echo "[DRY-RUN] - Would add comment about ${CLOSE_AFTER_DAYS}-day grace period"
else
echo "Issue #$NUMBER: Marking as stale (created: $CREATED_AT, no planned iteration)"
# Create stale label if it doesn't exist
gh label create "stale" --repo "$OWNER/$REPO" --color "EDEDED" --description "This issue has not been updated in ${STALE_DAYS}+ days and is not planned" || true
# Add stale label and comment
gh issue edit "$NUMBER" --repo "$OWNER/$REPO" --add-label "stale"
gh issue comment "$NUMBER" --repo "$OWNER/$REPO" --body "This issue has been automatically marked as stale because it has been open for more than ${STALE_DAYS} days without being planned for a current or future iteration. If this issue is still relevant, please update it or add it to an iteration. Otherwise, it will be closed in ${CLOSE_AFTER_DAYS} days."
fi
fi
done
HAS_NEXT=$(printf '%s\n' "$PAGE" | jq -r '.data.repository.issues.pageInfo.hasNextPage')
if [ "$HAS_NEXT" != "true" ]; then
break
fi
CURSOR=$(printf '%s\n' "$PAGE" | jq -r '.data.repository.issues.pageInfo.endCursor')
done
sync-priority:
name: Sync Priority Label
runs-on: ubuntu-latest
steps:
- uses: actions/create-github-app-token@v3
id: generate-token
with:
app-id: ${{ secrets.APP_ID }}
private-key: ${{ secrets.APP_SECRET }}
- name: Sync Priority to Label
env:
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
run: |
# Get all open issues with 'security' label (with pagination)
ISSUES=""
CURSOR=""
while :; do
if [ -z "$CURSOR" ]; then
QUERY='
query($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
issues(first: 100, states: OPEN, labels: ["security"]) {
pageInfo {
hasNextPage
endCursor
}
nodes {
number
labels(first: 20) {
nodes { name }
}
projectItems(first: 5) {
nodes {
fieldValueByName(name: "Priority") {
... on ProjectV2ItemFieldSingleSelectValue { name }
}
}
}
}
}
}
}
'
else
QUERY='
query($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
issues(first: 100, states: OPEN, labels: ["security"], after: "'$CURSOR'") {
pageInfo {
hasNextPage
endCursor
}
nodes {
number
labels(first: 20) {
nodes { name }
}
projectItems(first: 5) {
nodes {
fieldValueByName(name: "Priority") {
... on ProjectV2ItemFieldSingleSelectValue { name }
}
}
}
}
}
}
}
'
fi
PAGE=$(gh api graphql -f owner="$OWNER" -f repo="$REPO" -f query="$QUERY")
ISSUES="${ISSUES}${PAGE}"
HAS_NEXT=$(printf '%s\n' "$PAGE" | jq -r '.data.repository.issues.pageInfo.hasNextPage')
if [ "$HAS_NEXT" != "true" ]; then
break
fi
CURSOR=$(printf '%s\n' "$PAGE" | jq -r '.data.repository.issues.pageInfo.endCursor')
done
# Loop through issues
echo "$ISSUES" | jq -c '.data.repository.issues.nodes[]' | while read -r issue; do
NUMBER=$(echo "$issue" | jq -r '.number')
# Get Priority from project items (take the first one found)
RAW_PRIORITY=$(echo "$issue" | jq -r '.projectItems.nodes[].fieldValueByName.name // empty' | head -n 1)
# Remove non-ASCII characters (icons/emojis) to get clean label name
CLEAN_PRIORITY=$(echo "$RAW_PRIORITY" | tr -cd '\000-\177' | xargs)
if [ -z "$CLEAN_PRIORITY" ]; then
echo "Issue #$NUMBER: No priority found in project (or empty after cleanup)."
continue
fi
# Map Priority to P-labels
case "$CLEAN_PRIORITY" in
"Low")
TARGET_LABEL="P3"
LABEL_COLOR="EDEDED"
;;
"Medium")
TARGET_LABEL="P2"
LABEL_COLOR="FBCA04"
;;
"High")
TARGET_LABEL="P1"
LABEL_COLOR="FF9F1C"
;;
"Urgent")
TARGET_LABEL="P0"
LABEL_COLOR="D73A4A"
;;
*)
TARGET_LABEL="P3"
LABEL_COLOR="EDEDED"
;;
esac
# Check if label already exists
HAS_LABEL=$(echo "$issue" | jq -r --arg TARGET_LABEL "$TARGET_LABEL" '.labels.nodes[].name | select(. == $TARGET_LABEL)')
# Remove other priority labels if they exist
echo "$issue" | jq -r '.labels.nodes[].name' | while read -r EXISTING_LABEL; do
if [[ "$EXISTING_LABEL" =~ ^P[0-9]+$ ]] && [ "$EXISTING_LABEL" != "$TARGET_LABEL" ]; then
echo "Issue #$NUMBER: Removing conflicting label '$EXISTING_LABEL'."
gh issue edit "$NUMBER" --repo "$OWNER/$REPO" --remove-label "$EXISTING_LABEL"
fi
done
if [ -n "$HAS_LABEL" ]; then
echo "Issue #$NUMBER: Already has label '$TARGET_LABEL'."
else
echo "Issue #$NUMBER: Adding label '$TARGET_LABEL'."
# Create label if it doesn't exist (ignores error if exists)
gh label create "$TARGET_LABEL" --repo "$OWNER/$REPO" --color "$LABEL_COLOR" || true
gh issue edit "$NUMBER" --repo "$OWNER/$REPO" --add-label "$TARGET_LABEL"
fi
done