/
ceph-backport.sh
executable file
·1722 lines (1579 loc) · 59.9 KB
/
ceph-backport.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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash -e
#
# ceph-backport.sh - Ceph backporting script
#
# Credits: This script is based on work done by Loic Dachary
#
#
# This script automates the process of staging a backport starting from a
# Backport tracker issue.
#
# Setup:
#
# ceph-backport.sh --setup
#
# Usage and troubleshooting:
#
# ceph-backport.sh --help
# ceph-backport.sh --usage | less
# ceph-backport.sh --troubleshooting | less
#
full_path="$0"
SCRIPT_VERSION="15.0.0.6869"
active_milestones=""
backport_pr_labels=""
backport_pr_number=""
backport_pr_title=""
backport_pr_url=""
deprecated_backport_common="$HOME/bin/backport_common.sh"
existing_pr_milestone_number=""
github_token=""
github_token_file="$HOME/.github_token"
github_user=""
milestone=""
non_interactive=""
original_issue=""
original_issue_url=""
original_pr=""
original_pr_url=""
redmine_key=""
redmine_key_file="$HOME/.redmine_key"
redmine_login=""
redmine_user_id=""
setup_ok=""
this_script=$(basename "$full_path")
if [[ $* == *--debug* ]]; then
set -x
fi
# associative array keyed on "component" strings from PR titles, mapping them to
# GitHub PR labels that make sense in backports
declare -A comp_hash=(
["auth"]="core"
["bluestore"]="bluestore"
["build/ops"]="build/ops"
["ceph.spec"]="build/ops"
["ceph-volume"]="ceph-volume"
["cephfs"]="cephfs"
["cmake"]="build/ops"
["config"]="config"
["client"]="cephfs"
["common"]="common"
["core"]="core"
["dashboard"]="dashboard"
["deb"]="build/ops"
["doc"]="documentation"
["grafana"]="monitoring"
["mds"]="cephfs"
["messenger"]="core"
["mon"]="core"
["msg"]="core"
["mgr/dashboard"]="dashboard"
["mgr/prometheus"]="monitoring"
["mgr"]="core"
["monitoring"]="monitoring"
["orch"]="orchestrator"
["osd"]="core"
["perf"]="performance"
["prometheus"]="monitoring"
["pybind"]="pybind"
["py3"]="python3"
["python3"]="python3"
["qa"]="tests"
["rbd"]="rbd"
["rgw"]="rgw"
["rpm"]="build/ops"
["tests"]="tests"
["tool"]="tools"
)
declare -A flagged_pr_hash=()
function abort_due_to_setup_problem {
error "problem detected in your setup"
info "Run \"${this_script} --setup\" to fix"
false
}
function assert_fail {
local message="$1"
error "(internal error) $message"
info "This could be reported as a bug!"
false
}
function backport_pr_needs_label {
local check_label="$1"
local label
local needs_label="yes"
while read -r label ; do
if [ "$label" = "$check_label" ] ; then
needs_label=""
fi
done <<< "$backport_pr_labels"
echo "$needs_label"
}
function backport_pr_needs_milestone {
if [ "$existing_pr_milestone_number" ] ; then
echo ""
else
echo "yes"
fi
}
function bail_out_github_api {
local api_said="$1"
local hint="$2"
info "GitHub API said:"
log bare "$api_said"
if [ "$hint" ] ; then
info "(hint) $hint"
fi
abort_due_to_setup_problem
}
function blindly_set_pr_metadata {
local pr_number="$1"
local json_blob="$2"
curl --silent --data-binary "$json_blob" "https://api.github.com/repos/ceph/ceph/issues/${pr_number}?access_token=${github_token}" >/dev/null 2>&1 || true
}
function check_milestones {
local milestones_to_check
milestones_to_check="$(echo "$1" | tr '\n' ' ' | xargs)"
info "Active milestones: $milestones_to_check"
for m in $milestones_to_check ; do
info "Examining all PRs targeting base branch \"$m\""
vet_prs_for_milestone "$m"
done
dump_flagged_prs
}
function check_tracker_status {
local -a ok_statuses=("new" "need more info")
local ts="$1"
local error_msg
local tslc="${ts,,}"
local tslc_is_ok=
for oks in "${ok_statuses[@]}"; do
if [ "$tslc" = "$oks" ] ; then
debug "Tracker status $ts is OK for backport to proceed"
tslc_is_ok="yes"
break
fi
done
if [ "$tslc_is_ok" ] ; then
true
else
if [ "$tslc" = "in progress" ] ; then
error_msg="backport $redmine_url is already in progress"
else
error_msg="backport $redmine_url is closed (status: ${ts})"
fi
if [ "$FORCE" ] || [ "$EXISTING_PR" ] ; then
warning "$error_msg"
else
error "$error_msg"
fi
fi
echo "$tslc_is_ok"
}
function cherry_pick_phase {
local base_branch
local default_val
local i
local merged
local number_of_commits
local offset
local sha1_to_cherry_pick
local singular_or_plural_commit
local yes_or_no_answer
populate_original_issue
if [ -z "$original_issue" ] ; then
error "Could not find original issue"
info "Does ${redmine_url} have a \"Copied from\" relation?"
false
fi
info "Parent issue: ${original_issue_url}"
populate_original_pr
if [ -z "$original_pr" ]; then
error "Could not find original PR"
info "Is the \"Pull request ID\" field of ${original_issue_url} populated?"
false
fi
info "Parent issue ostensibly fixed by: ${original_pr_url}"
verbose "Examining ${original_pr_url}"
remote_api_output=$(curl --silent "https://api.github.com/repos/ceph/ceph/pulls/${original_pr}?access_token=${github_token}")
base_branch=$(echo "${remote_api_output}" | jq -r '.base.label')
if [ "$base_branch" = "ceph:master" ] ; then
true
else
if [ "$FORCE" ] ; then
warning "base_branch ->$base_branch<- is something other than \"ceph:master\""
info "--force was given, so continuing anyway"
else
error "${original_pr_url} is targeting ${base_branch}: cowardly refusing to perform automated cherry-pick"
info "Out of an abundance of caution, the script only automates cherry-picking of commits from PRs targeting \"ceph:master\"."
info "You can still use the script to stage the backport, though. Just prepare the local branch \"${local_branch}\" manually and re-run the script."
false
fi
fi
merged=$(echo "${remote_api_output}" | jq -r '.merged')
if [ "$merged" = "true" ] ; then
true
else
error "${original_pr_url} is not merged yet"
info "Cowardly refusing to perform automated cherry-pick"
false
fi
number_of_commits=$(echo "${remote_api_output}" | jq '.commits')
if [ "$number_of_commits" -eq "$number_of_commits" ] 2>/dev/null ; then
# \$number_of_commits is set, and is an integer
if [ "$number_of_commits" -eq "1" ] ; then
singular_or_plural_commit="commit"
else
singular_or_plural_commit="commits"
fi
else
error "Could not determine the number of commits in ${original_pr_url}"
bail_out_github_api "$remote_api_output"
fi
info "Found $number_of_commits $singular_or_plural_commit in $original_pr_url"
set -x
git fetch "$upstream_remote"
if git show-ref --verify --quiet "refs/heads/$local_branch" ; then
if [ "$FORCE" ] ; then
if [ "$non_interactive" ] ; then
git checkout "$local_branch"
git reset --hard "${upstream_remote}/${milestone}"
else
echo
echo "A local branch $local_branch already exists and the --force option was given."
echo "If you continue, any local changes in $local_branch will be lost!"
echo
default_val="y"
echo -n "Do you really want to overwrite ${local_branch}? (default: ${default_val}) "
yes_or_no_answer="$(get_user_input "$default_val")"
[ "$yes_or_no_answer" ] && yes_or_no_answer="${yes_or_no_answer:0:1}"
if [ "$yes_or_no_answer" = "y" ] ; then
git checkout "$local_branch"
git reset --hard "${upstream_remote}/${milestone}"
else
info "OK, bailing out!"
false
fi
fi
else
set +x
error "Cannot initialize $local_branch - local branch already exists"
false
fi
else
git checkout "${upstream_remote}/${milestone}" -b "$local_branch"
fi
git fetch "$upstream_remote" "pull/$original_pr/head:pr-$original_pr"
set +x
info "Attempting to cherry pick $number_of_commits commits from ${original_pr_url} into local branch $local_branch"
offset="$((number_of_commits - 1))" || true
for ((i=offset; i>=0; i--)) ; do
info "Running \"git cherry-pick -x\" on $(git log --oneline --max-count=1 --no-decorate "pr-${original_pr}~${i}")"
sha1_to_cherry_pick=$(git rev-parse --verify "pr-${original_pr}~${i}")
set -x
if git cherry-pick -x "$sha1_to_cherry_pick" ; then
set +x
else
set +x
[ "$VERBOSE" ] && git status
error "Cherry pick failed"
info "Next, manually fix conflicts and complete the current cherry-pick"
if [ "$i" -gt "0" ] >/dev/null 2>&1 ; then
info "Then, cherry-pick the remaining commits from ${original_pr_url}, i.e.:"
for ((j=i-1; j>=0; j--)) ; do
info "-> missing commit: $(git log --oneline --max-count=1 --no-decorate "pr-${original_pr}~${j}")"
done
info "Finally, re-run the script"
else
info "Then re-run the script"
fi
false
fi
done
info "Cherry picking completed without conflicts"
}
function clear_line {
log overwrite " \r"
}
function debug {
log debug "$@"
}
function display_version_message_and_exit {
echo "$this_script: Ceph backporting script, version $SCRIPT_VERSION"
exit 0
}
function dump_flagged_prs {
local url=
clear_line
if [ "${#flagged_pr_hash[@]}" -eq "0" ] ; then
info "All backport PRs appear to have milestone set correctly"
else
warning "Some backport PRs had problematic milestone settings"
log bare "==========="
log bare "Flagged PRs"
log bare "==========="
for url in "${!flagged_pr_hash[@]}" ; do
log bare "$url - ${flagged_pr_hash[$url]}"
done
log bare "==========="
fi
}
function eol {
local mtt="$1"
error "$mtt is EOL"
false
}
function error {
log error "$@"
}
function existing_pr_routine {
local base_branch
local clipped_pr_body
local new_pr_body
local new_pr_title
local pr_body
local pr_json_tempfile
local remote_api_output
local update_pr_body
remote_api_output="$(curl --silent "https://api.github.com/repos/ceph/ceph/pulls/${backport_pr_number}?access_token=${github_token}")"
backport_pr_title="$(echo "$remote_api_output" | jq -r '.title')"
if [ "$backport_pr_title" = "null" ] ; then
error "could not get PR title of existing PR ${backport_pr_number}"
bail_out_github_api "$remote_api_output"
fi
existing_pr_milestone_number="$(echo "$remote_api_output" | jq -r '.milestone.number')"
if [ "$existing_pr_milestone_number" = "null" ] ; then
existing_pr_milestone_number=""
fi
backport_pr_labels="$(echo "$remote_api_output" | jq -r '.labels[].name')"
pr_body="$(echo "$remote_api_output" | jq -r '.body')"
if [ "$pr_body" = "null" ] ; then
error "could not get PR body of existing PR ${backport_pr_number}"
bail_out_github_api "$remote_api_output"
fi
base_branch=$(echo "${remote_api_output}" | jq -r '.base.label')
base_branch="${base_branch#ceph:}"
if [ -z "$(is_active_milestone "$base_branch")" ] ; then
error "existing PR $backport_pr_url is targeting $base_branch which is not an active milestone"
info "Cowardly refusing to work on a backport to $base_branch"
false
fi
pr_json_tempfile=$(mktemp)
echo "$pr_body" | sed -n '/<!--.*/q;p' > "$pr_json_tempfile"
# clipped_pr_body=$(cat "$pr_json_tempfile" | xargs)
clipped_pr_body="$(xargs < "$pr_json_tempfile")"
rm "$pr_json_tempfile"
verbose_en "Clipped body of existing PR ${backport_pr_number}:\n${clipped_pr_body}\n"
if [[ "$backport_pr_title" =~ ^${milestone}: ]] ; then
verbose "Existing backport PR ${backport_pr_number} title has ${milestone} prepended"
else
warning "Existing backport PR ${backport_pr_number} title does NOT have ${milestone} prepended"
new_pr_title="${milestone}: $backport_pr_title"
if [[ "$new_pr_title" =~ \" ]] ; then
new_pr_title="${new_pr_title//\"/\\\"}"
fi
verbose "New PR title: ${new_pr_title}"
fi
redmine_url_without_scheme="${redmine_url//http?:\/\//}"
verbose "Redmine URL without scheme: $redmine_url_without_scheme"
if [[ "$clipped_pr_body" =~ $redmine_url_without_scheme ]] ; then
info "Existing backport PR ${backport_pr_number} already mentions $redmine_url"
if [ "$FORCE" ] ; then
warning "--force was given, so updating the PR body anyway"
update_pr_body="yes"
fi
else
warning "Existing backport PR ${backport_pr_number} does NOT mention $redmine_url - adding it"
update_pr_body="yes"
fi
if [ "$update_pr_body" ] ; then
new_pr_body="backport tracker: ${redmine_url}"
if [ "${original_pr_url}" ] ; then
new_pr_body="${new_pr_body}
possibly a backport of ${original_pr_url}"
fi
if [ "${original_issue_url}" ] ; then
new_pr_body="${new_pr_body}
parent tracker: ${original_issue_url}"
fi
new_pr_body="${new_pr_body}
---
original PR body:
$clipped_pr_body
---
updated using ceph-backport.sh version ${SCRIPT_VERSION}"
fi
maybe_update_pr_title_body "${new_pr_title}" "${new_pr_body}"
}
function failed_mandatory_var_check {
local varname="$1"
local error="$2"
verbose "$varname $error"
setup_ok=""
}
function flag_pr {
local pr_num="$1"
local pr_url="$2"
local flag_reason="$3"
warning "flagging PR#${pr_num} because $flag_reason"
flagged_pr_hash["${pr_url}"]="$flag_reason"
}
function from_file {
local what="$1"
xargs 2>/dev/null < "$HOME/.${what}" || true
}
function get_user_input {
local default_val="$1"
local user_input=
read -r user_input
if [ "$user_input" ] ; then
echo "$user_input"
else
echo "$default_val"
fi
}
# takes a string and a substring - returns position of substring within string,
# or -1 if not found
# NOTE: position of first character in string is 0
function grep_for_substr {
local str="$1"
local look_for_in_str="$2"
str="${str,,}"
munged="${str%%${look_for_in_str}*}"
if [ "$munged" = "$str" ] ; then
echo "-1"
else
echo "${#munged}"
fi
}
# takes PR title, attempts to guess component
function guess_component {
local comp=
local pos="0"
local pr_title="$1"
local winning_comp=
local winning_comp_pos="9999"
for comp in "${!comp_hash[@]}" ; do
pos=$(grep_for_substr "$pr_title" "$comp")
# echo "$comp: $pos"
[ "$pos" = "-1" ] && continue
if [ "$pos" -lt "$winning_comp_pos" ] ; then
winning_comp_pos="$pos"
winning_comp="$comp"
fi
done
[ "$winning_comp" ] && echo "${comp_hash["$winning_comp"]}" || echo ""
}
function info {
log info "$@"
}
function init_endpoints {
verbose "Initializing remote API endpoints"
redmine_endpoint="${redmine_endpoint:-"https://tracker.ceph.com"}"
github_endpoint="${github_endpoint:-"https://github.com/ceph/ceph"}"
}
function init_fork_remote {
[ "$github_user" ] || assert_fail "github_user not set"
fork_remote="${fork_remote:-$(maybe_deduce_remote fork)}"
}
function init_github_token {
github_token="$(from_file github_token)"
if [ "$github_token" ] ; then
true
else
warning "$github_token_file not populated: initiating interactive setup routine"
INTERACTIVE_SETUP_ROUTINE="yes"
fi
}
function init_redmine_key {
redmine_key="$(from_file redmine_key)"
if [ "$redmine_key" ] ; then
true
else
warning "$redmine_key_file not populated: initiating interactive setup routine"
INTERACTIVE_SETUP_ROUTINE="yes"
fi
}
function init_upstream_remote {
upstream_remote="${upstream_remote:-$(maybe_deduce_remote upstream)}"
}
function interactive_setup_routine {
local default_val
local original_github_token
local original_redmine_key
local total_steps
local yes_or_no_answer
original_github_token="$github_token"
original_redmine_key="$redmine_key"
total_steps="4"
if [ -e "$deprecated_backport_common" ] ; then
github_token=""
redmine_key=""
# shellcheck disable=SC1090
source "$deprecated_backport_common" 2>/dev/null || true
total_steps="$((total_steps+1))"
fi
echo
echo "Welcome to the ${this_script} interactive setup routine!"
echo
echo "---------------------------------------------------------------------"
echo "Setup step 1 of $total_steps - GitHub token"
echo "---------------------------------------------------------------------"
echo "For information on how to generate a GitHub personal access token"
echo "to use with this script, go to https://github.com/settings/tokens"
echo "then click on \"Generate new token\" and make sure the token has"
echo "\"Full control of private repositories\" scope."
echo
echo "For more details, see:"
echo "https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line"
echo
echo -n "What is your GitHub token? "
default_val="$github_token"
[ "$github_token" ] && echo "(default: ${default_val})"
github_token="$(get_user_input "$default_val")"
if [ "$github_token" ] ; then
set_github_user_from_github_token
if [ "$setup_ok" ] ; then
true
else
info "You must provide a valid GitHub personal access token"
abort_due_to_setup_problem
fi
else
error "You must provide a valid GitHub personal access token"
abort_due_to_setup_problem
fi
[ "$github_token" ] || assert_fail "github_token not set, even after completing Step 1 of interactive setup"
[ "$github_user" ] || assert_fail "github_user not set, even after completing Step 1 of interactive setup"
echo
echo "---------------------------------------------------------------------"
echo "Setup step 2 of $total_steps - GitHub user"
echo "---------------------------------------------------------------------"
echo "Based on the GitHub token set in the previous step, the script thinks"
echo "your GitHub username (login) is:"
echo
echo " $github_user"
echo
echo -n "Is this GitHub username (login) correct? "
default_val="y"
[ "$github_token" ] && echo "(default: ${default_val})"
yes_or_no_answer="$(get_user_input "$default_val")"
[ "$yes_or_no_answer" ] && yes_or_no_answer="${yes_or_no_answer:0:1}"
if [ "$yes_or_no_answer" = "y" ] ; then
if [ "$github_token" = "$original_github_token" ] ; then
true
else
debug "GitHub personal access token changed"
echo "$github_token" > "$github_token_file"
chmod 0600 "$github_token_file"
info "Wrote GitHub personal access token to $github_token_file"
fi
else
error "GitHub user does not look right"
abort_due_to_setup_problem
fi
[ "$github_token" ] || assert_fail "github_token not set, even after completing Steps 1 and 2 of interactive setup"
[ "$github_user" ] || assert_fail "github_user not set, even after completing Steps 1 and 2 of interactive setup"
echo
echo "---------------------------------------------------------------------"
echo "Setup step 3 of $total_steps - remote repos"
echo "---------------------------------------------------------------------"
echo "Searching \"git remote -v\" for remote repos"
echo
init_upstream_remote
init_fork_remote
vet_remotes
[ "$github_token" ] || assert_fail "github_token not set, even after completing Steps 1-3 of interactive setup"
[ "$github_user" ] || assert_fail "github_user not set, even after completing Steps 1-3 of interactive setup"
[ "$upstream_remote" ] || assert_fail "upstream_remote not set, even after completing Steps 1-3 of interactive setup"
[ "$fork_remote" ] || assert_fail "fork_remote not set, even after completing Steps 1-3 of interactive setup"
echo
echo "---------------------------------------------------------------------"
echo "Setup step 4 of $total_steps - Redmine key"
echo "---------------------------------------------------------------------"
echo "To generate a Redmine API access key, go to https://tracker.ceph.com"
echo "After signing in, click: \"My account\""
echo "Now, find \"API access key\"."
echo "Once you know the API access key, enter it below."
echo
echo -n "What is your Redmine key? "
default_val="$redmine_key"
[ "$redmine_key" ] && echo "(default: ${default_val})"
redmine_key="$(get_user_input "$default_val")"
if [ "$redmine_key" ] ; then
set_redmine_user_from_redmine_key
if [ "$setup_ok" ] ; then
true
else
info "You must provide a valid Redmine API access key"
abort_due_to_setup_problem
fi
if [ "$redmine_key" = "$original_redmine_key" ] ; then
true
else
debug "Redmine API access key changed"
echo "$redmine_key" > "$redmine_key_file"
chmod 0600 "$redmine_key_file"
info "Wrote Redmine API access key to $redmine_key_file"
fi
else
error "You must provide a valid Redmine API access key"
abort_due_to_setup_problem
fi
[ "$github_token" ] || assert_fail "github_token not set, even after completing Steps 1-4 of interactive setup"
[ "$github_user" ] || assert_fail "github_user not set, even after completing Steps 1-4 of interactive setup"
[ "$upstream_remote" ] || assert_fail "upstream_remote not set, even after completing Steps 1-4 of interactive setup"
[ "$fork_remote" ] || assert_fail "fork_remote not set, even after completing Steps 1-4 of interactive setup"
[ "$redmine_key" ] || assert_fail "redmine_key not set, even after completing Steps 1-4 of interactive setup"
[ "$redmine_user_id" ] || assert_fail "redmine_user_id not set, even after completing Steps 1-4 of interactive setup"
[ "$redmine_login" ] || assert_fail "redmine_login not set, even after completing Steps 1-4 of interactive setup"
if [ "$total_steps" -gt "4" ] ; then
echo
echo "---------------------------------------------------------------------"
echo "Step 5 of $total_steps - delete deprecated $deprecated_backport_common file"
echo "---------------------------------------------------------------------"
fi
maybe_delete_deprecated_backport_common
vet_setup --interactive
}
function is_active_milestone {
local is_active=
local milestone_under_test="$1"
for m in $active_milestones ; do
if [ "$milestone_under_test" = "$m" ] ; then
verbose "Milestone $m is active"
is_active="yes"
break
fi
done
echo "$is_active"
}
function log {
local level="$1"
local trailing_newline="yes"
local in_hex=""
shift
local msg="$*"
prefix="${this_script}: "
verbose_only=
case $level in
bare)
prefix=
;;
debug)
prefix="${prefix}DEBUG: "
verbose_only="yes"
;;
err*)
prefix="${prefix}ERROR: "
;;
hex)
in_hex="yes"
;;
info)
:
;;
overwrite)
trailing_newline=
prefix=
;;
verbose)
verbose_only="yes"
;;
verbose_en)
verbose_only="yes"
trailing_newline=
;;
warn|warning)
prefix="${prefix}WARNING: "
;;
esac
if [ "$in_hex" ] ; then
print_in_hex "$msg"
elif [ "$verbose_only" ] && [ -z "$VERBOSE" ] ; then
true
else
msg="${prefix}${msg}"
if [ "$trailing_newline" ] ; then
echo "${msg}" >&2
else
echo -en "${msg}" >&2
fi
fi
}
function maybe_deduce_remote {
local remote_type="$1"
local remote=""
local url_component=""
if [ "$remote_type" = "upstream" ] ; then
url_component="ceph"
elif [ "$remote_type" = "fork" ] ; then
url_component="$github_user"
else
assert_fail "bad remote_type ->$remote_type<- in maybe_deduce_remote"
fi
remote=$(git remote -v | grep --extended-regexp --ignore-case '(://|@)github.com(/|:)'${url_component}'/ceph(\s|\.|\/)' | head -n1 | cut -f 1)
echo "$remote"
}
function maybe_delete_deprecated_backport_common {
local default_val
local user_inp
if [ -e "$deprecated_backport_common" ] ; then
echo "You still have a $deprecated_backport_common file,"
echo "which was used to store configuration parameters in version"
echo "15.0.0.6270 and earlier versions of ${this_script}."
echo
echo "Since $deprecated_backport_common has been deprecated in favor"
echo "of the interactive setup routine, which has been completed"
echo "successfully, the file should be deleted now."
echo
echo -n "Delete it now? (default: y) "
default_val="y"
user_inp="$(get_user_input "$default_val")"
user_inp="$(echo "$user_inp" | tr '[:upper:]' '[:lower:]' | xargs)"
if [ "$user_inp" ] ; then
user_inp="${user_inp:0:1}"
if [ "$user_inp" = "y" ] ; then
set -x
rm -f "$deprecated_backport_common"
set +x
fi
fi
if [ -e "$deprecated_backport_common" ] ; then
error "$deprecated_backport_common still exists. Bailing out!"
false
fi
fi
}
function maybe_update_pr_milestone_labels {
local component
local data_binary
local data_binary
local label
local needs_milestone
if [ "$EXPLICIT_COMPONENT" ] ; then
debug "Component given on command line: using it"
component="$EXPLICIT_COMPONENT"
else
debug "Attempting to guess component"
component=$(guess_component "$backport_pr_title")
fi
data_binary="{"
needs_milestone="$(backport_pr_needs_milestone)"
if [ "$needs_milestone" ] ; then
debug "Attempting to set ${milestone} milestone in ${backport_pr_url}"
data_binary="${data_binary}\"milestone\":${milestone_number}"
else
info "Backport PR ${backport_pr_url} already has ${milestone} milestone"
fi
if [ "$(backport_pr_needs_label "$component")" ] ; then
debug "Attempting to add ${component} label to ${backport_pr_url}"
if [ "$needs_milestone" ] ; then
data_binary="${data_binary},"
fi
data_binary="${data_binary}\"labels\":[\"${component}\""
while read -r label ; do
if [ "$label" ] ; then
data_binary="${data_binary},\"${label}\""
fi
done <<< "$backport_pr_labels"
data_binary="${data_binary}]}"
else
info "Backport PR ${backport_pr_url} already has label ${component}"
data_binary="${data_binary}}"
fi
if [ "$data_binary" = "{}" ] ; then
true
else
blindly_set_pr_metadata "$backport_pr_number" "$data_binary"
fi
}
function maybe_update_pr_title_body {
local new_title="$1"
local new_body="$2"
local data_binary
if [ "$new_title" ] && [ "$new_body" ] ; then
data_binary="{\"title\":\"${new_title}\", \"body\":\"$(munge_body "${new_body}")\"}"
elif [ "$new_title" ] ; then
data_binary="{\"title\":\"${new_title}\"}"
backport_pr_title="${new_title}"
elif [ "$new_body" ] ; then
data_binary="{\"body\":\"$(munge_body "${new_body}")\"}"
#log hex "${data_binary}"
#echo -n "${data_binary}"
fi
if [ "$data_binary" ] ; then
blindly_set_pr_metadata "${backport_pr_number}" "$data_binary"
fi
}
function milestone_number_from_remote_api {
local mtt="$1" # milestone to try
local mn="" # milestone number
local milestones
warning "Milestone ->$mtt<- unknown to script - falling back to GitHub API"
remote_api_output=$(curl --silent -X GET "https://api.github.com/repos/ceph/ceph/milestones?access_token=${github_token}")
mn=$(echo "$remote_api_output" | jq --arg milestone "$mtt" '.[] | select(.title==$milestone) | .number')
if [ "$mn" -gt "0" ] >/dev/null 2>&1 ; then
echo "$mn"
else
error "Could not determine milestone number of ->$milestone<-"
verbose_en "GitHub API said:\n${remote_api_output}\n"
remote_api_output=$(curl --silent -X GET "https://api.github.com/repos/ceph/ceph/milestones?access_token=${github_token}")
milestones=$(echo "$remote_api_output" | jq '.[].title')
info "Valid values are ${milestones}"
info "(This probably means the Release field of ${redmine_url} is populated with"
info "an unexpected value - i.e. it does not match any of the GitHub milestones.)"
false
fi
}
function munge_body {
echo "$new_body" | tr '\r' '\n' | sed 's/$/\\n/' | tr -d '\n'
}
function number_to_url {
local number_type="$1"
local number="$2"
if [ "$number_type" = "github" ] ; then
echo "${github_endpoint}/pull/${number}"
elif [ "$number_type" = "redmine" ] ; then
echo "${redmine_endpoint}/issues/${number}"
else
assert_fail "internal error in number_to_url: bad type ->$number_type<-"
fi
}
function populate_original_issue {
if [ -z "$original_issue" ] ; then
original_issue=$(curl --silent "${redmine_url}.json?include=relations" |
jq '.issue.relations[] | select(.relation_type | contains("copied_to")) | .issue_id')
original_issue_url="$(number_to_url "redmine" "${original_issue}")"
fi
}
function populate_original_pr {
if [ "$original_issue" ] ; then
if [ -z "$original_pr" ] ; then
original_pr=$(curl --silent "${original_issue_url}.json" |
jq -r '.issue.custom_fields[] | select(.id | contains(21)) | .value')
original_pr_url="$(number_to_url "github" "${original_pr}")"
fi
fi
}
function print_in_hex {
local str="$1"
local c
for (( i=0; i < ${#str}; i++ ))
do
c=${str:$i:1}
if [[ $c == ' ' ]]
then
printf "[%s] 0x%X\n" " " \'\ \' >&2
else
printf "[%s] 0x%X\n" "$c" \'"$c"\' >&2
fi
done
}
function set_github_user_from_github_token {
[ "$github_token" ] || assert_fail "set_github_user_from_github_token: git_token not set"
#
# $ curl --silent https://api.github.com/repos/ceph/ceph/pulls/19999?access_token=invalid
# {
# "message": "Bad credentials",
# "documentation_url": "https://developer.github.com/v3"
# }
#
remote_api_output=$(curl --silent "https://api.github.com/user?access_token=${github_token}")
github_user=$(echo "${remote_api_output}" | jq -r '.login')
if [ "$github_user" = "null" ] 2>/dev/null ; then
error "GitHub token ${github_token} is invalid"
github_user=""
setup_ok=""
else
[ "$github_user" ] || assert_fail "set_github_user_from_github_token: failed to set github_user"
info "my GitHub username is $github_user"
setup_ok="yes"
fi
}
function set_redmine_user_from_redmine_key {
[ "$redmine_key" ] || assert_fail "set_redmine_user_from_redmine_key was called, but redmine_key not set"
local api_key_from_api
remote_api_output="$(curl --silent "https://tracker.ceph.com/users/current.json?key=$redmine_key")"
redmine_login="$(echo "$remote_api_output" | jq -r '.user.login')"
redmine_user_id="$(echo "$remote_api_output" | jq -r '.user.id')"
api_key_from_api="$(echo "$remote_api_output" | jq -r '.user.api_key')"
if [ "$redmine_login" ] && [ "$redmine_user_id" ] && [ "$api_key_from_api" = "$redmine_key" ] ; then
[ "$redmine_user_id" ] || assert_fail "set_redmine_user_from_redmine_key: failed to set redmine_user_id"
[ "$redmine_login" ] || assert_fail "set_redmine_user_from_redmine_key: failed to set redmine_login"
info "my Redmine username is $redmine_login (ID $redmine_user_id)"
setup_ok="yes"
else
error "Redmine API access key $redmine_key is invalid"
redmine_login=""
redmine_user_id=""
setup_ok=""
fi
}
function tracker_component_is_in_desired_state {
local comp="$1"
local val_is="$2"
local val_should_be="$3"
local in_desired_state
if [ "$val_is" = "$val_should_be" ] ; then
debug "Tracker $comp is in the desired state"
in_desired_state="yes"
fi
echo "$in_desired_state"
}
function tracker_component_was_updated {
local comp="$1"
local val_old="$2"
local val_new="$3"
local was_updated
if [ "$val_old" = "$val_new" ] ; then
true
else
debug "Tracker $comp was updated!"
was_updated="yes"
fi
echo "$was_updated"
}
function troubleshooting_advice {
cat <<EOM