-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathautomator.sh
executable file
·385 lines (333 loc) · 10 KB
/
automator.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
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
#!/usr/bin/env bash
# shellcheck disable=SC2016
# Copyright Istio Authors
#
# 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.
set -euo pipefail
ROOT="$(cd -P "$(dirname -- "$0")" && pwd -P)"
# shellcheck disable=SC1090,SC1091
source "$ROOT/utils.sh"
# shellcheck disable=SC2317
cleanup() {
rm -rf "${tmp_dir:-}" "${tmp_token:-}" "${tmp_script:-}" "${tmp_git:-}"
}
get_opts() {
if opt="$(getopt -o '' -l branch:,org:,repo:,title:,match-title:,body:,labels:,user:,email:,modifier:,script-path:,cmd:,token-path:,token:,token-env,merge-repository:,merge-branch:,git-exclude:,signoff,strict,dry-run,verbose -n "$(basename "$0")" -- "$@")"; then
eval set -- "$opt"
else
print_error_and_exit "unable to parse options"
fi
while true; do
case "$1" in
--branch)
branch="$2"
shift 2
;;
--org)
org_tmpl="$2"
shift 2
;;
--repo)
repos="$(split_on_commas "$2")"
shift 2
;;
--title)
title_tmpl="$2"
shift 2
;;
--match-title)
match_title_tmpl="$2"
shift 2
;;
--body)
body_tmpl="$2"
shift 2
;;
--labels)
labels="$(echo "$2" | jq --raw-input --compact-output 'split(",")')"
shift 2
;;
--user)
user="$2"
shift 2
;;
--email)
email="$2"
shift 2
;;
--modifier)
modifier="$2"
shift 2
;;
--script-path)
script_path="$(realpath "$2")"
shift 2
;;
--cmd)
tmp_script="$(mktemp -t script-XXXXXXXXXX)"
echo "$2" >"$tmp_script"
script_path="$tmp_script"
shift 2
;;
--token-path)
token_path="$2"
token="$(cat "$token_path")"
shift 2
;;
--token)
token="$2"
tmp_token="$(mktemp -t token-XXXXXXXXXX)"
echo "$token" >"$tmp_token"
token_path="$tmp_token"
shift 2
;;
--token-env)
token="$GH_TOKEN"
tmp_token="$(mktemp -t token-XXXXXXXXXX)"
echo "$token" >"$tmp_token"
token_path="$tmp_token"
shift
;;
--merge-repository)
merge_repository="$2"
shift 2
;;
--merge-branch)
merge_branch="$2"
shift 2
;;
--verbose)
shell_args+=("-x")
shift
;;
--signoff)
commit_args+=("--signoff")
shift
;;
--strict)
strict=true
shift
;;
--dry-run)
dry_run=true
shift
;;
--git-exclude)
git_exclude="$2"
shift 2
;;
--)
shift
script_args=("$@")
break
;;
*)
print_error_and_exit "unknown option: $1"
;;
esac
done
}
validate_opts() {
sha="$(current_sha)"
sha_short="$(current_sha --short)"
commit_date="$(commit_date)"
merge=false
if [ -z "${strict:-}" ]; then
strict=false
fi
if [ -z "${dry_run:-}" ]; then
dry_run=false
fi
if [ -z "${branch:-}" ]; then
branch="$(current_branch)"
fi
if [ -n "${merge_repository:-}" ] && [ -n "${merge_branch:-}" ]; then
merge=true
fi
if [ -z "${title_tmpl:-}" ]; then
title_tmpl='Automator: update $AUTOMATOR_ORG/$AUTOMATOR_REPO@$AUTOMATOR_BRANCH-$AUTOMATOR_MODIFIER'
fi
if [ -z "${match_title_tmpl:-}" ]; then
match_title_tmpl="$title_tmpl"
fi
if [ -z "${body_tmpl:-}" ]; then
body_tmpl='Generated by Automator - $(date -uIseconds)'
fi
if [ -z "${org_tmpl:-}" ]; then
org_tmpl="istio"
fi
if [ -z "${repos:-}" ]; then
print_error_and_exit "repo is a required option"
fi
if [ ! -f "${token_path:-}" ] || [ -z "${token:-}" ] && ! $dry_run; then
print_error_and_exit "token_path or token is a required option"
fi
if [ ! -f "${script_path:-}" ] && ! $merge; then
print_error_and_exit "either script-path, cmd, or merge-repository and merge-branch are required"
fi
if [ -z "${modifier:-}" ]; then
modifier="automator"
fi
if [ -z "${user:-}" ] && ! $dry_run; then
user="$(curl -sSfLH "Authorization: token $token" "https://api.github.com/user" | jq --raw-output ".login")"
fi
if [ -z "${email:-}" ] && ! $dry_run; then
email="$(curl -sSfLH "Authorization: token $token" "https://api.github.com/user" | jq --raw-output ".email")"
fi
if [ -z "${git_exclude:-}" ]; then
git_exclude=""
fi
}
evaluate_opts() {
org="$(evaluate_tmpl "$org_tmpl")"
AUTOMATOR_SRC_ORG="${REPO_OWNER:-}" AUTOMATOR_SRC_REPO="${REPO_NAME:-}" AUTOMATOR_SRC_BRANCH="${PULL_BASE_REF:-}"
AUTOMATOR_SHA="$sha" AUTOMATOR_SHA_SHORT="$sha_short"
AUTOMATOR_SHA_COMMIT_DATE="$commit_date"
AUTOMATOR_ORG="$org" AUTOMATOR_REPO="$repo" AUTOMATOR_BRANCH="$branch" AUTOMATOR_MODIFIER="$modifier"
title="$(evaluate_tmpl "$title_tmpl")"
match_title="$(evaluate_tmpl "$match_title_tmpl")"
body="$(evaluate_tmpl "$body_tmpl")"
}
export_globals() {
export AUTOMATOR_SRC_ORG AUTOMATOR_SRC_REPO AUTOMATOR_SRC_BRANCH AUTOMATOR_SHA AUTOMATOR_SHA_SHORT AUTOMATOR_SHA_COMMIT_DATE \
AUTOMATOR_ORG AUTOMATOR_REPO AUTOMATOR_BRANCH AUTOMATOR_MODIFIER AUTOMATOR_ROOT_DIR AUTOMATOR_REPO_DIR
}
create_pr() {
pr-creator \
--github-token-path="$token_path" \
--org="$org" \
--repo="$repo" \
--branch="$branch" \
--title="$title" \
--match-title="\"$match_title\"" \
--body="$body" \
--source="$user:$fork_name" \
--confirm
}
add_labels() {
if [ "${labels:-}" ]; then
curl -XPOST -sSfLH "Authorization: token $token" "https://api.github.com/repos/$org/$repo/issues/$pull_request/labels" --data "{\"labels\": $labels}" >/dev/null
fi
}
commit() {
if $dry_run; then
git diff --cached
return 0
fi
local src_branch="${AUTOMATOR_SRC_BRANCH:-none}"
fork_name="$src_branch-$branch-$modifier-$(hash "$title")"
if ! git diff --cached --quiet --exit-code; then
git -c "user.name=$user" -c "user.email=$email" commit --message "$title" --author="$user <$email>" "${commit_args[@]}"
else
echo "No changes to commit. Assuming the commit was already made."
fi
git show --shortstat
git push --force "https://$user:[email protected]/$user/$repo.git" "HEAD:$fork_name"
pull_request="$(create_pr)"
add_labels
}
merge() {
local src_branch="${AUTOMATOR_SRC_BRANCH:-none}"
fork_name="$src_branch-$branch-$modifier-$(hash "$title")"
git remote add -f -t "$merge_branch" upstream "$merge_repository"
set +e # git return a non-zero exit code on merge failure, which fails the script
git -c "user.name=$user" -c "user.email=$email" merge --no-ff -m "$title" --log upstream/"$merge_branch"
local code=$?
set -e
if [ "$code" -ne 0 ]; then
export GITHUB_TOKEN="$token"
local issue_exists
issue_exists=$(gh issue list -S "Automatic merge of $merge_branch into $branch failed." -R "istio/istio" | wc -l)
if [ "$issue_exists" -eq 0 ]; then
gh issue create -b "Automatic merge of $merge_branch into $branch failed. @istio/wg-networking-maintainers" -t "Automatic merge of upstream envoy release branch failed" -l "area/networking/envoy" -R "istio/istio"
print_error "Conflicts detected, manual merge is required. An issue in istio/istio has been created." 0
else
print_error "Conflicts detected, manual merge is required. An issue in istio/istio already exists." 0
fi
else
if [[ "$(git show --shortstat)" =~ $title ]]; then
git show --shortstat
git push --force "https://$user:[email protected]/$user/$repo.git" "HEAD:$fork_name"
pull_request="$(create_pr)"
add_labels
else
print_error "No changes to merge" 0
fi
fi
}
# validate_changes_exist_in_latest_commit validates changes exist in the prior commit after removing files specified
# in the git_exclude list. If no files remain after exclusion, the automation script exits.
validate_changes_exist_in_latest_commit() {
if [ -n "$git_exclude" ]; then
set +e # grep will have an exit code of 1 if all files are excluded causing the script to fail
changes=$(git show --name-only --pretty=oneline | sed 1d | grep -cvE "$git_exclude") # need to remove first line
set -e
if [ "${changes}" -eq 0 ]
then
print_error_and_exit "No changes remaining in upstream PR after excluding" 0 # not really an error so return 0
fi
fi
}
work() { (
set -e
evaluate_opts
if ! $dry_run; then
curl -XPOST -sSfLH "Authorization: token $token" "https://api.github.com/repos/$org/$repo/forks" >/dev/null
fi
# Some jobs expect GOPATH setup, so simulate this
mkdir -p src/istio.io/"$repo"
if ! $dry_run; then
git clone --single-branch --branch "$branch" "https://$user:[email protected]/$org/$repo.git" src/istio.io/"$repo"
else
git clone --single-branch --branch "$branch" "https://github.com/$org/$repo.git" src/istio.io/"$repo"
fi
gopath=${PWD}
pushd src/istio.io/"$repo"
AUTOMATOR_REPO_DIR="$(pwd)"
local initialSHA
initialSHA=$(git rev-parse HEAD)
if $merge; then
merge
else
GOPATH="${gopath}" bash "${shell_args[@]}" "$script_path" "${script_args[@]}"
git add --all
local currentSHA
currentSHA=$(git rev-parse HEAD)
if ! git diff --cached --quiet --exit-code || [ "${initialSHA}" != "${currentSHA}" ]; then
commit
elif $strict; then
print_error "no diff for $repo" 1
fi
fi
popd
); }
main() {
trap cleanup EXIT
tmp_dir=$(mktemp -d -t ci-XXXXXXXXXX)
get_opts "$@"
validate_opts
export_globals
validate_changes_exist_in_latest_commit
AUTOMATOR_ROOT_DIR="$(pwd)"
pushd "$tmp_dir" || print_error_and_exit "invalid dir: $tmp_dir"
set +e
for repo in $repos; do
work
local code="$?"
[ "$code" -ne 0 ] && exit_code="$code"
done
set -e
popd || print_error_and_exit "invalid dir: $tmp_dir"
}
main "$@"
exit "${exit_code:-0}"