-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbash_github
492 lines (398 loc) · 13.2 KB
/
bash_github
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/bin/bash
# Most of the below utilites depends on the "jq" tool for parsing the
# See https://stedolan.github.io/jq/ for installing jq tool.
# JSON reponse from GitHub rest API. Below environment variables need to
# be configured for the tool to function.
# GITHUB_API_URL - GitHub REST api URL
# GITHUB_TOKEN - GitHub token of main DevOps account
# GITHUB_API_DELAY - Default value for applying delay between subsequence githup api calls.
# GITHUB_2PEER_APPROVAL_SIMULATION_ENABLED=true
# GITHUB_TOKEN_01 - GitHub token of subordinate DevOps account-1
# GITHUB_TOKEN_02 - GitHub token of subordinate DevOps account-2
# GITHUB_AUTO_APPROVE_TEXT - Auto approve text to be put in PR approvals and status approvals
# GITHUB_STATUS_CONTEXT - State context for the PR status approval
safe_merge() {
local project_name source_branch target_branch source_helper_branch target_helper_branch files_diff ts te
ts=`date +%s`
project_name=$1
source_branch=$2
target_branch=$3
echo "Checking $source_branch -> $target_branch in $project_name"
files_diff=`gh_get_changed_files_count $project_name $source_branch $target_branch`
if [[ files_diff -eq 0 ]]; then
echo "Nothing to merge. Skipping $source_branch -> $target_branch in $project_name"
return
fi
echo "Merging $source_branch -> $target_branch in $project_name"
source_helper_branch="temp/smh-$source_branch-$ts"
target_helper_branch="temp/tmh-$target_branch-$ts"
echo "Create helper branches $source_helper_branch and $target_helper_branch in $project_name"
create_branch $project_name $source_helper_branch $source_branch
create_branch $project_name $target_helper_branch $target_branch
direct_merge $project_name $source_helper_branch $target_helper_branch &&
direct_merge $project_name $target_helper_branch $target_branch
te=`date +%s`
echo "Time taken for merging $source_branch -> $target_branch in $project_name = $((te-ts)) s"
}
direct_merge() {
local project_name source_branch target_branch pr_url is_pb
project_name=$1
source_branch=$2
target_branch=$3
echo "Merging $source_branch to $target_branch in $project_name"
pr_url=`create_pr $project_name $source_branch $target_branch "[DevOps] Auto PR for merging $source_branch to $target_branch"`
if is_protected_branch $project_name $target_branch; then
is_pb="true"
else
is_pb="false"
fi
approve_and_merge_pr $pr_url $is_pb
}
approve_and_merge_pr() {
local web_pr_url pr_url is_pb merge_method
pr_url=$1
is_pb=$2
merge_method="${3:-merge}"
web_pr_url=`get_web_pr_url $pr_url`
echo "Processing PR = $pr_url and is_protected = $is_pb and merge_method = $merge_method"
if has_merge_conflicts $pr_url; then
echo "PR has conflicts: $web_pr_url"
echo $web_pr_url >> conflicts_to_review
return 1
fi
echo "No merge conflicts for the PR: $web_pr_url"
[[ $is_pb == "true" ]] && echo "need to approve" && approve_pr $pr_url
if exec_inc_delay 5 30 merge_pr $pr_url $merge_method; then
echo "Successfully merged $web_pr_url"
else
echo "Unable to merge the PR: $web_pr_url. Please check logs."
echo $web_pr_url >> conflicts_to_review
return 1
fi
}
create_branch() {
local project_name branch_name parent_branch url parent_sha
project_name=$1
branch_name=$2
parent_branch=$3
echo "Create $branch_name from $parent_branch in $project_name"
parent_sha=`get_branch_sha $project_name $parent_branch`
url="$GITHUB_API_URL/$project_name/git/refs"
call_github -u $url -m POST -p "{\"ref\": \"refs/heads/$branch_name\", \"sha\": \"$parent_sha\"}" > /dev/null
}
get_branch_sha() {
local project_name branch_name url
project_name=$1
branch_name=$2
url="$GITHUB_API_URL/$project_name/git/ref/heads/$branch_name"
call_github -u $url | jq -r '.object.sha'
}
create_pr() {
local project_name source target title
project_name=$1
source=$2
target=$3
title=$4
call_github -u "$GITHUB_API_URL/$project_name/pulls" -m POST \
-p "{\"title\": \"$title\", \"body\": \"$title\", \"head\": \"$source\", \"base\": \"$target\"}" | jq -r '.url'
}
has_merge_conflicts() {
local pr_url m_state;
pr_url=$1
m_state=`call_github -u $pr_url | jq -r '.mergeable_state'`
[[ $m_state == "dirty" ]]
}
is_protected_branch() {
local url project_name branch_name result
project_name=$1
branch_name=$2
url="$GITHUB_API_URL/$project_name/branches/$branch_name"
result=`call_github -u $url | jq -r '.protected'`
[[ $result == "true" ]]
}
approve_pr() {
local pr_url status_url pr_approve_payload status_approve_payload
pr_url=$1
echo "Approving the PR = `get_web_pr_url $pr_url`"
if [[ $GITHUB_2PEER_APPROVAL_SIMULATION_ENABLED == "true" ]]; then
pr_approve_payload="{\"body\": \"$GITHUB_AUTO_APPROVE_TEXT\", \"event\": \"APPROVE\"}"
call_github -u "$pr_url/reviews" -p "$pr_approve_payload" -m POST -a $GITHUB_TOKEN_01 > /dev/null
call_github -u "$pr_url/reviews" -p "$pr_approve_payload" -m POST -a $GITHUB_TOKEN_02 > /dev/null
fi
notify_status_for_pr $pr_url "success" "$GITHUB_STATUS_CONTEXT" "$GITHUB_AUTO_APPROVE_TEXT"
}
notify_status_for_pr() {
local pr_url state context description pr_url status_payload status_url
pr_url=$1;shift
state=$1;shift
context=$1;shift
description="$@"
status_payload="{\"state\": \"$state\", \"context\": \"$context\", \"description\": \"$description\"}"
status_url=$(call_github -u $pr_url | jq -r '._links.statuses.href')
call_github -u $status_url -p "$status_payload" -m POST > /dev/null
}
notify_status() {
local project_name pr_number pr_url
project_name=$1;shift
pr_number=$1;shift
args="$@"
pr_url=$(get_pr_url -p $project_name -n $pr_number)
notify_status_for_pr $pr_url $args
}
merge_pr() {
local pr_url response_code merge_method payload
pr_url=$1
merge_method="${2:-merge}"
echo "Merging the PR = `get_web_pr_url $pr_url` ($merge_method)"
payload="{\"merge_method\": \"$merge_method\"}"
response_code=`call_github -u "$pr_url/merge" -m PUT -f '-s -i' -p "$payload" | awk 'NR==1{print $2}'`
if [[ $response_code == "200" ]]; then
true
else
echo "Merging $pr_url - response code = $response_code"
false
fi
}
exec_inc_delay() {
local retry delay method args inc result
retry=$1;shift
delay=$1;shift
method=$1;shift
args="$@"
inc=$delay
result=false
while : ; do
if $method $args; then
result=true
break
fi
retry=$(($retry-1))
if [[ $retry -lt 0 ]]; then
break
fi
echo "waiting for $delay s..."
sleep $delay
delay=$(($delay+$inc))
done
$result
}
add_pr_labels() {
local project_name pr_number labels payload L a b url
project_name=$1;shift
pr_number=$1;shift
labels="$@"
echo "Labels = $labels"
url="$GITHUB_API_URL/$project_name/issues/$pr_number/labels"
for L in $labels; do a="$a \"$L\""; done
echo "a = $a"
b=`echo $a | sed 's/\ /,/g'`
echo "b = $b"
payload="{\"labels\": ["$b"]}"
echo "payload = $payload"
call_github -u $url -p "$payload" -m POST | jq -r '.[].name'
}
gh_get_pr_labels() {
local project_name pr_number url
project_name=$1
pr_number=$2
url="$GITHUB_API_URL/$project_name/issues/$pr_number/labels"
call_github -u $url | jq -r '.[].name'
}
gh_get_changed_files_count() {
local project_name source target url
project_name=$1
source=$2
target=$3
url="$GITHUB_API_URL/$project_name/compare/$target...$source"
call_github -u $url | jq -r '.files | length'
}
get_web_pr_url() {
echo $1 | sed 's/api\.//' | sed 's/repos\///' | sed 's/pulls/pull/'
}
get_pr_url() {
local project_name pr_number web_pr_url c OPTIND OPTARG
while getopts 'p:n:u:h' c
do
case $c in
p) project_name=$OPTARG ;;
n) pr_number=$OPTARG ;;
u) web_pr_url=$OPTARG ;;
h) echo "Usage: get_pr_url [ -u web_pr_url ] [ -p project_name -n pr_number ]"; return
esac
done
if [[ -z "$web_pr_url" ]] && [[ -z "$project_name" || -z "$pr_number" ]]; then
get_pr_url -h
return
fi
if ! [ -z "$web_pr_url" ]; then
project_name=`echo $web_pr_url | cut -d\/ -f5`
pr_number=`echo $web_pr_url | cut -d\/ -f7`
fi
echo "$GITHUB_API_URL/$project_name/pulls/$pr_number"
}
gh_add_pr_reviewers() {
local project_name pr_number reviewers payload R a b url
project_name=$1;shift
pr_number=$1;shift
reviewers="$@"
url="$GITHUB_API_URL/$project_name/pulls/$pr_number/requested_reviewers"
for R in $reviewers; do a="$a \"$R\""; done
b=`echo $a | sed 's/\ /,/g'`
payload="{\"reviewers\": ["$b"]}"
echo "payload = $payload"
call_github -u $url -p "$payload" -m POST #| jq -r '.requested_reviewers[].login'
}
gh_get_pr_participants() {
local project_name pr_number url pr_participants pr_json_data commenters additional_urls
project_name=$1
pr_number=$2
url="$GITHUB_API_URL/$project_name/pulls/$pr_number"
pr_json_data=`call_github -u $url`
pr_participants=`echo "$pr_json_data" | jq -r '.user.login,.requested_reviewers[].login,.assignees[].login'`
additional_urls=`echo "$pr_json_data" | jq -r '._links.review_comments.href,._links.comments.href'`
additional_urls=`echo "$additional_urls";echo "$url/reviews"`
for url in `echo "$additional_urls"`; do
commenters=`call_github -u $url | jq -r '.[].user.login'`
if ! [[ -z $commenters || $commenters == null ]]; then
pr_participants=`echo "$pr_participants";echo "$commenters"`
fi
done
echo "$pr_participants" | sort | uniq
}
gh_search_branches() {
local project_name search_key url
project_name=$1
search_key=$2
url="$GITHUB_API_URL/$project_name/git/matching-refs/heads/$search_key"
call_github -u $url | jq -r '.[].ref' | cut -d'/' -f3-
}
gh_delete_branch() {
local project_name branch_name url
project_name=$1
branch_name=$2
url="$GITHUB_API_URL/$project_name/git/refs/heads/$branch_name"
call_github -m DELETE -u $url
}
gh_extract_pr_details_from_webhook_payload() {
local x_github_event json_payload actor pr_number message comment review_state
x_github_event=$1
json_payload=$2
repo_name=`echo "$json_payload" | jq -r '.repository.name'`
actor=`echo "$json_payload" | jq -r '.sender.login'`
case $x_github_event in
issue_comment)
pr_number=`echo "$json_payload" | jq -r '.issue.number'`
comment=`echo "$json_payload" | jq -r '.comment.body'`
message="$actor commented that, $comment"
;;
pull_request_review)
pr_number=`echo "$json_payload" | jq -r '.pull_request.number'`
review_state=`echo "$json_payload" | jq -r '.review.state' | tr '_' ' '`
message="PR Review $review_state by $actor"
;;
pull_request)
gh_extract_pr_message_from_webhook_payload_pull_request "$json_payload"
return
;;
*) echo "$x_github_event is not supported!"
return
;;
esac
echo "$(jq -n \
--arg prn "$pr_number" \
--arg rn "$repo_name" \
--arg msg "$message" \
'{pr_number: $prn, repo_name: $rn, message: $msg}')"
}
gh_extract_pr_message_from_webhook_payload_pull_request() {
local json_payload pr_number action actor message is_merged
json_payload=$1
repo_name=`echo "$json_payload" | jq -r '.repository.name'`
pr_number=`echo "$json_payload" | jq -r '.pull_request.number'`
action=`echo "$json_payload" | jq -r '.action'`
actor=`echo "$json_payload" | jq -r '.sender.login'`
case $action in
review_requested)
reviewer=`echo "$json_payload" | jq -r '.requested_reviewer.login'`
message="$reviewer's review requested for this PR by $actor"
;;
assigned)
assignee=`echo "$json_payload" | jq -r '.assignee.login'`
message="$assignee assigned for this PR by $actor"
;;
opened|reopened|synchronize)
message="PR has been updated by $actor. Kindly check if any action required from you. Thanks."
;;
closed)
is_merged=`echo "$json_payload" | jq -r '.pull_request.merged'`
if [[ $is_merged == "true" ]]; then
message="PR has been merged by $actor"
else
message="PR has been closed by $actor"
fi
;;
*)
a=`echo $action | tr '_' ' '`
message="PR $a by $actor"
;;
esac
echo "$(jq -n \
--arg prn "$pr_number" \
--arg rn "$repo_name" \
--arg msg "$message" \
'{pr_number: $prn, repo_name: $rn, message: $msg}')"
}
gh_compare_branches() {
local project source target url
project=$1
source=$2
target=$3
url=`get_web_pr_url "$GITHUB_API_URL/$project/compare/$source...$target"`
open_url "$url"
}
gh_get_commit_date() {
local project_nme commit_sha url
project_name=$1
commit_sha=$2
url="$GITHUB_API_URL/$project_name/commits/$commit_sha"
call_github -u $url | jq -r '.commit.committer.date'
}
gh_get_all_branches_in_open_prs() {
local project_name url
project_name=$1
url="$GITHUB_API_URL/$project_name/pulls"
call_github -u $url | jq -r '.[].head.ref, .[].base.ref' | sort | uniq
}
call_github() {
local url method payload delay flags auth_token c OPTIND OPTARG
while getopts 'u:m:p:d:f:a:h' c
do
case $c in
u) url=$OPTARG ;;
m) method=$OPTARG ;;
p) payload=$OPTARG ;;
d) delay=$OPTARG ;;
f) flags=$OPTARG ;;
a) auth_token=$OPTARG ;;
h) echo "Usage: call_github -u url [ -m method ] [ -p payload ] [ -d delay ] [ -f flags ] [ -a auth_token ]"; return
esac
done
if [ -z $delay ]; then
delay=${GITHUB_API_DELAY:-10}
fi
if [ -z $method ]; then
method='GET'
fi
if [ -z "$flags" ]; then
flags="-s"
fi
if [ -z $auth_token ]; then
auth_token="$GITHUB_TOKEN"
fi
sleep $delay
if [ -z "$payload" ]; then
curl $flags -X $method -H "Authorization: token $auth_token" -H "Content-Type: application/json" "$url"
else
curl $flags -X $method -H "Authorization: token $auth_token" -H "Content-Type: application/json" "$url" -d "$payload"
fi
}