This repository has been archived by the owner on Aug 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
rebuild_prs.sh
executable file
·100 lines (79 loc) · 2.5 KB
/
rebuild_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
#!/bin/bash
set -e
: ${LEEROY_URI:=https://leeroy.dockerproject.org}
URI=https://api.github.com
API_VERSION=v3
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "Set the GITHUB_TOKEN env variable."
return 1
fi
if [[ -z "$LEEROY_AUTH" ]]; then
echo "Set the LEEROY_AUTH env variable."
return 1
fi
API_HEADER="Accept: application/vnd.github.${API_VERSION}+json"
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
DEFAULT_PER_PAGE=10
LAST_PAGE=1
# get the last page from the headers
get_last_page(){
header=${1%%" rel=\"last\""*}
header=${header#*"rel=\"next\""}
header=${header%%">;"*}
LAST_PAGE=$(echo ${header#*"&page="} | bc 2>/dev/null)
}
rebuild_pulls(){
local repo=$1
local page=$2
# send the request
local response=$(curl -i -sSL -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/repos/${repo}/pulls?per_page=${DEFAULT_PER_PAGE}&page=${page}")
# seperate the headers and body into 2 variables
local head=true
local header=
local body=
while read -r line; do
if $head; then
if [[ $line = $'\r' ]]; then
head=false
else
header="$header"$'\n'"$line"
fi
else
body="$body"$'\n'"$line"
fi
done < <(echo "${response}")
get_last_page "${header}"
local shas=$(echo $body | jq --raw-output '.[] | {id: .number, sha: .head.sha} | tostring')
for s in $shas; do
local id=$(echo $s | jq --raw-output '.id')
local sha=$(echo $s | jq --raw-output '.sha')
# send the request
response=$(curl -sSL -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/repos/${repo}/commits/${sha}/status")
# find the statuses that are not success
local statuses=$(echo $response | jq --raw-output '. | select(.state != "success") | .statuses | .[] | select(.state != "success") | {context: .context, state: .state} | tostring')
for status in $statuses; do
local context=$(echo $status | jq --raw-output '.context')
local state=$(echo $status | jq --raw-output '.state')
if [[ "$context" != docker* ]]; then
echo "Rebuilding pull request ${id} for context $context, build had state ${state}"
data='{"repo":"'${repo}'","context":"'${context}'","number":'${id}'}'
curl -ssL -X POST --user "${LEEROY_AUTH}" --data "${data}" ${LEEROY_URI}/build/custom
fi
done
done
}
main(){
local repo=$1
: ${page:=1}
if [[ -z "$repo" ]]; then
repo="docker/docker"
fi
rebuild_pulls "${repo}" "${page}"
if [ ! -z "$LAST_PAGE" ] && [ "$LAST_PAGE" -ge "$page" ]; then
for page in $(seq $((page + 1)) 1 ${LAST_PAGE}); do
echo "On page ${page} of ${LAST_PAGE}"
rebuild_pulls "${repo}" "${page}"
done
fi
}
main $@