-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrole-manage-prs.sh
executable file
·330 lines (312 loc) · 9.97 KB
/
role-manage-prs.sh
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
#!/bin/bash
# -*- coding: utf-8 -*-
# Copyright: (c) 2023, Red Hat, Inc.
# SPDX-License-Identifier: MIT
set -euo pipefail
repo=${repo:-$(git remote get-url origin | awk -F'/' '{print $NF}')}
AUTHOR="${AUTHOR:-"@me"}"
APP="${APP:-""}"
declare -a searchargs=("--search" "draft:false")
if [ -n "$APP" ]; then
searchargs=("--app" "$APP")
elif [ "$AUTHOR" != "@all" ]; then
searchargs=("--author" "$AUTHOR")
fi
declare -a mergeargs=()
DELETE_BRANCH="${DELETE_BRANCH:-true}"
if [ "$DELETE_BRANCH" = true ]; then
mergeargs+=("-d")
fi
# gh pr merge arguments:
# -m, --merge Merge the commits with the base branch
# -r, --rebase Rebase the commits onto the base branch
# -s, --squash Squash the commits into one commit and merge it into the base branch
# ask - prompt user for merge type
MERGE_TYPE="${MERGE_TYPE:-r}"
if [ "$MERGE_TYPE" != ask ]; then
mergeargs+=("-$MERGE_TYPE")
fi
# Typically used in conjunction with manage-role-repos.sh
# This script is called from every role - it uses `gh` to display
# a list of open PRs, then prompts to merge one using `gh`, then
# repeats until the user moves to the next role
list_prs() {
# set in manage-role-repos.sh
# shellcheck disable=SC2154
gh pr list -R "$upstream_org/$repo" "${searchargs[@]}" "$@"
}
get_checks() {
local pr total success failure pending cancelled error conclusion state name status display_name
pr="$1"
gh pr view "$pr" -R "$upstream_org/$repo" --json statusCheckRollup \
--template '{{range .statusCheckRollup}}{{print (or .conclusion "<nil>")}}#{{print (or .state "<nil>")}}#{{print .name}}#{{println .context}}{{end}}' | \
while IFS="#" read -r conclusion state name context; do
if [ "$conclusion" = '<nil>' ] && [ "$state" = '<nil>' ]; then
status=PENDING
elif [ "$conclusion" = '<nil>' ] && [ "$state" = PENDING ]; then
status=PENDING
elif [ "$conclusion" = FAILURE ] || [ "$state" = FAILURE ]; then
status=FAILURE
elif [ "$conclusion" = SUCCESS ] || [ "$state" = SUCCESS ]; then
status=SUCCESS
elif [ "$conclusion" = CANCELLED ] || [ "$state" = CANCELLED ]; then
status=CANCELLED
elif [ "$conclusion" = ERROR ] || [ "$state" = ERROR ]; then
status=ERROR
elif [ "$conclusion" = NEUTRAL ]; then # usually a python check if python code did not change
status=SUCCESS
elif [ "$conclusion" = ACTION_REQUIRED ]; then # DCO check failed
status=FAILURE
else
echo ERROR: check "$name" has unknown conclusion "$conclusion" state "$state" 1>&2
exit 1
fi
if [ "$name" != '<nil>' ]; then
display_name="$name"
elif [ "$context" != '<nil>' ]; then
display_name="$context"
fi
echo "${display_name}#${status}"
done
}
get_check_detail() {
local pr name status
pr="$1"; shift
get_checks "$pr" | while IFS="#" read -r name status; do
# shellcheck disable=SC1009
# shellcheck disable=SC1072
# shellcheck disable=SC1073
if [ "$status" "$@" ]; then
echo name "$name" status "$status"
fi
done
}
get_check_summary() {
local pr
pr="$1"
get_checks "$pr" | {
local -A counts
local total status name
while IFS="#" read -r name status; do
counts[$status]=$(("${counts[$status]:-0}" + 1))
total=$(("${total:-0}" + 1))
done
echo "${total:-0} ${counts[SUCCESS]:-0} ${counts[FAILURE]:-0} ${counts[PENDING]:-0} ${counts[CANCELLED]:-0} ${counts[ERROR]:-0}"
}
}
# if all reviews are approved, then return APPROVED
# if there any CHANGES_REQUESTED return CHANGES_REQUESTED
# otherwise if there are any comments return COMMENTED
# otherwise, return NO_REVIEWS
get_reviews() {
local pr review rev found_cr found_c found_ap
pr="$1"
for rev in $(gh pr view "$pr" -R "$upstream_org/$repo" --json reviews \
--template '{{range .reviews}}{{println .state}}{{end}}'); do
if [ "$rev" == CHANGES_REQUESTED ]; then
found_cr=true
break
elif [ "$rev" == COMMENTED ]; then
found_c=true
elif [ "$rev" == APPROVED ]; then
found_ap=true
fi
done
if [ "${found_cr:-false}" = true ]; then
echo CHANGES_REQUESTED
elif [ "${found_c:-false}" = true ]; then
echo COMMENTED
elif [ "${found_ap:-false}" = true ]; then
echo APPROVED
else
echo NO_REVIEWS
fi
}
show_pr() {
local pr total success failure pending state review
pr="$1"
read -r total success failure pending cancelled error <<< "$(get_check_summary "$pr")"
if [ "$total" -eq 0 ]; then
state=UNKNOWN
elif [ "$total" = "$success" ]; then
state=SUCCESS
elif [ "$failure" -gt 0 ]; then
state=FAILED
elif [ "$pending" -gt 0 ]; then
state=PENDING
elif [ "$cancelled" -gt 0 ]; then
state=CANCELLED
elif [ "$error" -gt 0 ]; then
state=ERROR
else
state=UNKNOWN
fi
review="$(get_reviews "$pr")"
gh pr view "$pr" -R "$upstream_org/$repo" --json number,title,updatedAt \
--template '#{{tablerow .number .updatedAt "'"$state"'" "'"$review"'" .title}}'
echo "checks: total $total successful $success failed $failure pending $pending cancelled $cancelled error $error"
}
merge_pr() {
local pr
pr="$1"; shift
gh pr merge "$pr" -R "$upstream_org/$repo" "${mergeargs[@]}" "$@"
}
approve_pr() {
local pr
pr="$1"
gh pr review --approve "$pr" -R "$upstream_org/$repo"
}
get_pr_list() {
local pr prs
unset PR_LIST
prs="$(list_prs --json number -q .[].number)"
# shellcheck disable=SC2086
if [ -z "$prs" ]; then
return 1
fi
for pr in $prs; do
PR_LIST["$pr"]=$(show_pr "$pr")
done
return 0
}
show_pr_list() {
local data
for data in "${PR_LIST[@]}"; do
echo "$data"
done
}
if [ "${PR_LIST_DECLARED:-false}" = false ]; then
declare -A PR_LIST
PR_LIST_DECLARED=true
fi
done=false
if ! get_pr_list; then
echo INFO: role "$repo" has no PRs matching search/author criteria - skipping
done=true
fi
while [ "$done" = false ]; do
echo ""
show_pr_list
cat <<EOF
Actions:
"NUM" - merge PR | "w" - list PRs in browser
"l" - refresh the list | "v NUM" - view PR in browser
"a NUM" - merge with admin priv. | "ci NUM" - add "[citest]" comment to PR
"t NUM" - view test/check detail | "s NUM /path/to/script" - run script
"c NUM [comment]" - close PR with optional comment
"d NUM [diff args]" - gh pr diff NUM [args]
"e NUM [edit args]" - gh pr edit NUM [args]
"p NUM" - approve PR - cannot approve your own PRs
"pm NUM" - approve and merge PR - cannot approve your own PRs
Press Enter to skip to next role
EOF
read -r -p "Action? " input
if [[ "$input" =~ ^[0-9]+$ ]]; then
if merge_pr "$input"; then
unset PR_LIST["$input"]
fi
elif [ "$input" = w ]; then
list_prs -w
elif [ "$input" = l ]; then
if get_pr_list; then
done=false
else
done=true
echo INFO: role "$repo" has no more PRs matching search/author criteria - skipping
fi
elif [[ "$input" =~ ^v\ ([0-9]+)$ ]]; then
gh pr view "${BASH_REMATCH[1]}" --web -R "$upstream_org/$repo"
elif [[ "$input" =~ ^a\ ([0-9]+)$ ]]; then
if merge_pr "${BASH_REMATCH[1]}" --admin; then
unset PR_LIST["${BASH_REMATCH[1]}"]
fi
elif [[ "$input" =~ ^ci\ ([0-9]+)$ ]]; then
gh pr comment "${BASH_REMATCH[1]}" --body "[citest]" -R "$upstream_org/$repo"
elif [[ "$input" =~ ^t\ ([0-9]+)$ ]]; then
get_check_detail "${BASH_REMATCH[1]}" "!=" SUCCESS
elif [[ "$input" =~ ^s\ ([0-9]+)\ (.+)$ ]]; then
"${BASH_REMATCH[2]}" "$repo" "${BASH_REMATCH[1]}"
elif [[ "$input" =~ ^c\ ([0-9]+)(\ (.+))?$ ]]; then
args=("${BASH_REMATCH[1]}" -R "$upstream_org/$repo" -d)
if [ -n "${BASH_REMATCH[2]}" ]; then
args+=(-c "${BASH_REMATCH[2]}")
fi
if gh pr close "${args[@]}"; then
unset PR_LIST["${BASH_REMATCH[1]}"]
fi
elif [[ "$input" =~ ^d\ ([0-9]+)(\ (.+))?$ ]]; then
args=("${BASH_REMATCH[1]}" -R "$upstream_org/$repo")
if [ -n "${BASH_REMATCH[2]}" ]; then
args+=("${BASH_REMATCH[2]}")
fi
gh pr diff "${args[@]}"
elif [[ "$input" =~ ^e\ ([0-9]+)(\ (.+))?$ ]]; then
args=(gh pr edit "${BASH_REMATCH[1]}" -R "$upstream_org/$repo")
if [ -n "${BASH_REMATCH[3]:-}" ]; then
"${args[@]}" ${BASH_REMATCH[3]}
else
"${args[@]}"
fi
elif [[ "$input" =~ ^p\ ([0-9]+)$ ]]; then
approve_pr "${BASH_REMATCH[1]}"
elif [[ "$input" =~ ^pm\ ([0-9]+)$ ]]; then
approve_pr "${BASH_REMATCH[1]}"
sleep 1 # wait for approval to be applied
if merge_pr "${BASH_REMATCH[1]}" --admin; then
unset PR_LIST["${BASH_REMATCH[1]}"]
fi
elif [ -z "$input" ]; then
done=true
else
echo ERROR: invalid input ["$input"]
fi
if [ "$done" = false ] && [ "${#PR_LIST[*]}" = 0 ]; then
done=true
echo INFO: role "$repo" has no more PRs matching search/author criteria - skipping
fi
done
# PR fields
# Specify one or more comma-separated fields for `--json`:
# additions
# assignees
# author
# autoMergeRequest
# baseRefName
# body
# changedFiles
# closed
# closedAt
# comments
# commits
# createdAt
# deletions
# files
# headRefName
# headRefOid
# headRepository
# headRepositoryOwner
# id
# isCrossRepository
# isDraft
# labels
# latestReviews
# maintainerCanModify
# mergeCommit
# mergeStateStatus
# mergeable
# mergedAt
# mergedBy
# milestone
# number
# potentialMergeCommit
# projectCards
# projectItems
# reactionGroups
# reviewDecision
# reviewRequests
# reviews
# state
# statusCheckRollup
# title
# updatedAt
# url