-
Notifications
You must be signed in to change notification settings - Fork 30
/
generate-update-forks.sh
executable file
·201 lines (162 loc) · 4.7 KB
/
generate-update-forks.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
#!/bin/bash
# This script generates DSLs to update each of my public forks.
set -e
set -o pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [[ -z "$GITHUB_USER" ]]; then
echo "Set the GITHUB_USER env variable."
exit 1
fi
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "Set the GITHUB_TOKEN env variable."
exit 1
fi
URI=https://api.github.com
API_VERSION=v3
API_HEADER="Accept: application/vnd.github.${API_VERSION}+json"
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
DEFAULT_PER_PAGE=20
LAST_PAGE=1
ignore_repos=( mac-dev-setup )
# get the last page from the headers
get_last_page(){
local header=${1%%" rel=\"last\""*}
header=${header#*"rel=\"next\""}
header=${header%%">;"*}
LAST_PAGE=$(echo "${header#*'&page='}" | bc 2>/dev/null)
}
generate_dsl(){
local forked_repo=$1
local name
name=$(basename "$forked_repo")
local upstream_repo=$2
local primary_branch=$3
if [[ "${ignore_repos[*]}" =~ ${name} ]]; then
return
fi
if [[ "${upstream_repo}" == "moby/docker" ]]; then
upstream_repo="moby/moby"
elif [[ "${upstream_repo}" == "kata-containers/kata-runtime" ]]; then
upstream_repo="kata-containers/runtime"
fi
rname=${name//-/_}
file="${DIR}/projects/forks/${rname//./_}.groovy"
echo "${file} | ${forked_repo} | ${upstream_repo}"
cat <<-EOF > "$file"
freeStyleJob('update_fork_${rname//./_}') {
displayName('update-fork-${name}')
description('Rebase the primary branch (${primary_branch}) in ${forked_repo} fork.')
checkoutRetryCount(3)
properties {
githubProjectUrl('https://github.com/${forked_repo}')
sidebarLinks {
link('https://github.com/${upstream_repo}', 'UPSTREAM: ${upstream_repo}', 'notepad.png')
}
}
logRotator {
numToKeep(100)
daysToKeep(15)
}
scm {
git {
remote {
url('git@github.com:${forked_repo}.git')
name('origin')
credentials('ssh-github-key')
refspec('+refs/heads/${primary_branch}:refs/remotes/origin/${primary_branch}')
}
remote {
url('https://github.com/${upstream_repo}.git')
name('upstream')
refspec('+refs/heads/${primary_branch}:refs/remotes/upstream/${primary_branch}')
}
branches('${primary_branch}', 'upstream/${primary_branch}')
extensions {
disableRemotePoll()
wipeOutWorkspace()
cleanAfterCheckout()
}
}
}
triggers {
cron('H H * * *')
}
wrappers { colorizeOutput() }
steps {
shell('git rebase upstream/${primary_branch}')
}
publishers {
git {
branch('origin', '${primary_branch}')
pushOnlyIfSuccess()
}
extendedEmail {
recipientList('\$DEFAULT_RECIPIENTS')
contentType('text/plain')
triggers {
stillFailing {
attachBuildLog(true)
}
}
}
wsCleanup()
}
}
EOF
}
get_repos(){
local page=$1
# send the request
local response
response=$(curl -i -sSL -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/users/${GITHUB_USER}/repos?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 repos
repos=$(echo "$body" | jq --raw-output '.[] | {fullname:.full_name,repo:.name,fork:.fork} | @base64')
for r in $repos; do
raw="$(echo "$r" | base64 -d)"
local fullname
fullname=$(echo "$raw" | jq --raw-output '.fullname')
local repo
repo=$(echo "$raw" | jq --raw-output '.repo')
local fork
fork=$(echo "$raw" | jq --raw-output '.fork')
if [[ "$fork" == "true" ]]; then
local response
response=$(curl -sSL -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/repos/${fullname}")
local upstream_user
upstream_user=$(echo "$response" | jq --raw-output '.parent.owner.login')
local primary_branch
primary_branch=$(echo "$response" | jq --raw-output '.default_branch')
generate_dsl "${fullname}" "${upstream_user}/${repo}" "${primary_branch}"
fi
done
}
main(){
rm -rf "$DIR/projects/forks"
mkdir -p "$DIR/projects/forks"
echo "FILE | FORK | UPSTREAM"
local page=1
get_repos "$page"
if [ -n "$LAST_PAGE" ] && [ "$LAST_PAGE" -ge "$page" ]; then
for page in $(seq $((page + 1)) 1 "${LAST_PAGE}"); do
# echo "[debug]: on repo page ${page} of ${LAST_PAGE}"
get_repos "${page}"
done
fi
}
main