forked from apache/yetus
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgithub.sh
More file actions
executable file
·1234 lines (1053 loc) · 32.4 KB
/
github.sh
File metadata and controls
executable file
·1234 lines (1053 loc) · 32.4 KB
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
#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
# no public APIs here
# SHELLDOC-IGNORE
# This bug system provides github integration
add_bugsystem github
# personalities can override the following settings:
# Web interface URL.
GITHUB_BASE_URL="https://github.com"
# API interface URL.
GITHUB_API_URL="https://api.github.com"
# user/repo
GITHUB_REPO=""
# user settings
GITHUB_TOKEN="${GITHUB_TOKEN-}"
GITHUB_ISSUE=""
GITHUB_USE_EMOJI_VOTE=false
GITHUB_STATUS_RECOVERY_COUNTER=1
GITHUB_STATUS_RECOVER_TOOL=false
GITHUB_WRITE_COMMENT=false
GITHUB_ANNOTATION_LIMIT=50
GITHUB_STATUS_USE_HTMLREPORT=${GITHUB_STATUS_USE_HTMLREPORT:-true}
declare -a GITHUB_AUTH
# private globals...
GITHUB_BRIDGED=false
function github_usage
{
yetus_add_option "--github-annotation-limit=<int>" "The maximum number of annotations to send to GitHub (default: '${GITHUB_ANNOTATION_LIMIT}')"
yetus_add_option "--github-api-url=<url>" "The URL of the API for github (default: '${GITHUB_API_URL}')"
yetus_add_option "--github-base-url=<url>" "The URL of the github server (default:'${GITHUB_BASE_URL}')"
yetus_add_option "--github-repo=<repo>" "github repo to use (default:'${GITHUB_REPO}')"
yetus_add_option "--github-token=<token>" "The token to use to read/write to github"
yetus_add_option "--github-write-comment" "Write final report as github comment (default: '${GITHUB_WRITE_COMMENT}')"
yetus_add_option "--github-use-emoji-vote" "Whether to use emoji to represent the vote result on github [default: ${GITHUB_USE_EMOJI_VOTE}]"
yetus_add_option "--github-status-use-htmlreport=<bool>" "Use htmlout for Github Status 'Details' link [default: ${GITHUB_STATUS_USE_HTMLREPORT}]"
}
function github_parse_args
{
declare i
for i in "$@"; do
case ${i} in
--github-annotation-limit=*)
delete_parameter "${i}"
GITHUB_ANNOTATION_LIMIT=${i#*=}
;;
--github-api-url=*)
delete_parameter "${i}"
GITHUB_API_URL=${i#*=}
;;
--github-base-url=*)
delete_parameter "${i}"
GITHUB_BASE_URL=${i#*=}
;;
--github-repo=*)
delete_parameter "${i}"
GITHUB_REPO=${i#*=}
;;
--github-token=*)
delete_parameter "${i}"
GITHUB_TOKEN=${i#*=}
;;
--github-write-comment)
delete_parameter "${i}"
GITHUB_WRITE_COMMENT=true
;;
--github-use-emoji-vote)
delete_parameter "${i}"
GITHUB_USE_EMOJI_VOTE=true
;;
--github-status-use-htmlreport=*)
delete_parameter "${i}"
GITHUB_STATUS_USE_HTMLREPORT=${i#*=}
;;
esac
done
}
## @description this gets called when JIRA thinks this
## @description issue is just a pointer to github
## @description WARNING: Called from JIRA plugin!
function github_jira_bridge
{
declare jsonloc=$1
declare patchout=$2
declare diffout=$3
declare urlfromjira
# shellcheck disable=SC2016
urlfromjira=$("${AWK}" "match(\$0,\"${GITHUB_BASE_URL}/[^ ]*patch[ &\\\"]\"){url=substr(\$0,RSTART,RLENGTH-1)}
END{if (url) print url}" "${jsonloc}" )
if [[ -z $urlfromjira ]]; then
# This is currently the expected path, as github pull requests are not common
return 1
fi
# we use this to prevent loops later on
GITHUB_BRIDGED=true
yetus_debug "github_jira_bridge: Checking url ${urlfromjira}"
github_breakup_url "${urlfromjira}"
github_locate_patch GH:"${GITHUB_ISSUE}" "${patchout}" "${diffout}"
}
## @description given a URL, break it up into github plugin globals
## @description this will *override* any personality or yetus defaults
## @description WARNING: Called from various robots!
## @param url
function github_breakup_url
{
declare url=$1
declare count
declare pos1
declare pos2
if [[ "${url}" =~ \@ ]]; then
url=${url//:/\/}
url=${url//git@/https://}
fi
url=${url%\.git}
count=${url//[^\/]}
count=${#count}
if [[ ${count} -gt 4 ]]; then
((pos2=count-3))
((pos1=pos2))
GITHUB_BASE_URL=$(echo "${url}" | cut "-f1-${pos2}" -d/)
((pos1=pos1+1))
((pos2=pos1+1))
GITHUB_REPO=$(echo "${url}" | cut "-f${pos1}-${pos2}" -d/)
((pos1=pos2+2))
unset pos2
GITHUB_ISSUE=$(echo "${url}" | cut "-f${pos1}-${pos2}" -d/ | cut -f1 -d.)
else
GITHUB_BASE_URL=$(echo "${url}" | cut -f1-3 -d/)
GITHUB_REPO=$(echo "${url}" | cut -f4- -d/)
fi
}
# @description guess the repo
function github_brute_force_repo_on_remote
{
declare remote=$1
declare domain=${GITHUB_BASE_URL##*/}
declare repo
declare remoteurl
remoteurl=$("${GIT}" remote get-url "${remote}")
if [[ ${remoteurl} =~ ${domain} ]]; then
# chop off (protocol)(hostname)
repo=${remoteurl#*"${domain}"}
# chop off / or : in the front
repo=${repo:1}
# chop off ending .git
GITHUB_REPO=${repo%%\.git}
fi
}
## @description initialize github
function github_initialize
{
declare url
if [[ -n "${GITHUB_TOKEN}" ]]; then
GITHUB_AUTH=(-H "Authorization: token ${GITHUB_TOKEN}") # pragma: allowlist secret
fi
GITHUB_REPO=${GITHUB_REPO:-${GITHUB_REPO_DEFAULT}}
if [[ -z "${GITHUB_REPO}" ]]; then
yetus_error "WARNING: --github-repo not autodetermined or provided. Brute forcing."
pushd "${BASEDIR}" >/dev/null || return 1
github_brute_force_repo_on_remote origin
if [[ -z "${GITHUB_REPO}" ]]; then
while read -r; do
github_brute_force_repo_on_remote "${REPLY}"
if [[ -n "${GITHUB_REPO}" ]]; then
break
fi
done < <("${GIT}" remote)
fi
popd >/dev/null|| return 1
if [[ -n "${GITHUB_REPO}" ]]; then
yetus_error "WARNING: Brute force says ${GITHUB_BASE_URL}/${GITHUB_REPO}"
fi
fi
if [[ ${GITHUB_STATUS_USE_HTMLREPORT} == true ]]; then
if ! verify_plugin_enabled htmlout; then
GITHUB_STATUS_USE_HTMLREPORT=false
yetus_error "WARNING: Disabling --github-status-use-htmlreport. htmlout plug-in is not enabled."
else
url=$(htmlout_reportname)
if [[ -z ${url} ]]; then
GITHUB_STATUS_USE_HTMLREPORT=false
yetus_error "WARNING: Disabling --github-status-use-htmlreport. Artifacts URL does not resolve/report is not relative."
fi
fi
fi
# if the default branch hasn't been set yet, ask GitHub
if [[ -z "${PATCH_BRANCH_DEFAULT}" && -n "${GITHUB_REPO}" && "${OFFLINE}" == false ]]; then
if [[ ! -f "${PATCH_DIR}/github-repo.json" ]]; then
"${CURL}" --silent --fail \
-H "Accept: application/vnd.github.v3.full+json" \
"${GITHUB_AUTH[@]}" \
--output "${PATCH_DIR}/github-repo.json" \
--location \
"${GITHUB_API_URL}/repos/${GITHUB_REPO}" \
> /dev/null
fi
if [[ -f "${PATCH_DIR}/github-repo.json" ]]; then
PATCH_BRANCH_DEFAULT=$("${GREP}" default_branch "${PATCH_DIR}/github-repo.json" | head -1 | cut -d\" -f4)
fi
fi
if [[ "${PROJECT_NAME}" == "unknown" ]]; then
PROJECT_NAME=${GITHUB_REPO##*/}
fi
}
## @description based upon a github PR, attempt to link back to JIRA
function github_find_jira_title
{
declare title
declare maybe
declare retval
if [[ ! -f "${PATCH_DIR}/github-pull.json" ]]; then
return 1
fi
title=$(${GREP} title "${PATCH_DIR}/github-pull.json" \
| cut -f4 -d\")
# people typically do two types: JIRA-ISSUE: and [JIRA-ISSUE]
# JIRA_ISSUE_RE is pretty strict so we need to chop that stuff
# out first
maybe=$(echo "${title}" | cut -f2 -d\[ | cut -f1 -d\])
jira_determine_issue "${maybe}"
retval=$?
if [[ ${retval} == 0 ]]; then
return 0
fi
maybe=$(echo "${title}" | cut -f1 -d:)
jira_determine_issue "${maybe}"
retval=$?
if [[ ${retval} == 0 ]]; then
return 0
fi
return 1
}
function github_determine_issue
{
declare input=$1
if [[ ${input} =~ ^[0-9]+$
&& -n ${GITHUB_REPO} ]]; then
# shellcheck disable=SC2034
ISSUE=${input}
if [[ -z ${GITHUB_ISSUE} ]]; then
GITHUB_ISSUE=${input}
fi
fi
# if JIRA didn't call us, should we call it?
if [[ ${GITHUB_BRIDGED} == false ]]; then
if github_find_jira_title; then
return 0
fi
fi
if [[ -n ${GITHUB_ISSUE} ]]; then
return 0
fi
return 1
}
## @description Try to guess the branch being tested using a variety of heuristics
## @audience private
## @stability evolving
## @replaceable no
## @return 0 on success, with PATCH_BRANCH updated appropriately
## @return 1 on failure
function github_determine_branch
{
if [[ ! -f "${PATCH_DIR}/github-pull.json" ]]; then
return 1
fi
# shellcheck disable=SC2016
PATCH_BRANCH=$("${AWK}" 'match($0,"\"ref\": \""){print $2}' "${PATCH_DIR}/github-pull.json"\
| cut -f2 -d\"\
| tail -1 )
yetus_debug "Github determine branch: starting with ${PATCH_BRANCH}"
verify_valid_branch "${PATCH_BRANCH}"
}
## @description Given input = GH:##, download a patch to output.
## @description Also sets GITHUB_ISSUE to the raw number.
## @audience private
## @stability evolving
## @replaceable no
## @param input
## @param output
## @return 0 on success
## @return 1 on failure
function github_locate_pr_patch
{
declare input=$1
declare patchout=$2
declare diffout=$3
declare apiurl
declare line
declare sha
declare foundhead=false
input=${input#GH:}
# https://github.com/your/repo/pull/##
if [[ ${input} =~ ^${GITHUB_BASE_URL}.*/pull/[0-9]+$ ]]; then
github_breakup_url "${input}.patch"
input=${GITHUB_ISSUE}
fi
# https://github.com/your/repo/pulls/##.patch
if [[ ${input} =~ ^${GITHUB_BASE_URL}.*patch$ ]]; then
github_breakup_url "${input}"
input=${GITHUB_ISSUE}
fi
# https://github.com/your/repo/pulls/##.diff
if [[ ${input} =~ ^${GITHUB_BASE_URL}.*diff$ ]]; then
github_breakup_url "${input}"
input=${GITHUB_ISSUE}
fi
# if it isn't a number at this point, no idea
# how to process
if [[ ! ${input} =~ ^[0-9]+$ ]]; then
yetus_debug "github: ${input} is not a pull request #"
return 1
fi
# we always pull both the .patch and .diff versions
# but set the default to be .patch so that binary files work.
# The downside of this is that the patch files are
# significantly larger and therefore take longer to process
apiurl="${GITHUB_API_URL}/repos/${GITHUB_REPO}/pulls/${input}"
# shellcheck disable=SC2034
PATCHURL="${GITHUB_BASE_URL}/${GITHUB_REPO}/pull/${input}.patch"
echo "GITHUB PR #${input} is being downloaded from"
echo "${apiurl}"
echo " JSON data at $(date)"
# Let's pull the PR JSON for later use
if ! "${CURL}" --silent --fail \
-H "Accept: application/vnd.github.v3.full+json" \
"${GITHUB_AUTH[@]}" \
--output "${PATCH_DIR}/github-pull.json" \
--location \
"${apiurl}"; then
yetus_debug "github_locate_patch: cannot download json"
return 1
fi
echo " Patch data at $(date)"
# the actual patch file
if ! "${CURL}" --silent --fail \
-H "Accept: application/vnd.github.v3.patch" \
--output "${patchout}" \
--location \
"${GITHUB_AUTH[@]}" \
"${GITHUB_API_URL}/repos/${GITHUB_REPO}/pulls/${input}"; then
yetus_debug "github_locate_patch: not a github pull request."
return 1
fi
echo " Diff data at $(date)"
if ! "${CURL}" --silent --fail \
-H "Accept: application/vnd.github.v3.diff" \
--output "${diffout}" \
--location \
"${GITHUB_AUTH[@]}" \
"${apiurl}"; then
yetus_debug "github_locate_patch: cannot download diff"
return 1
fi
if [[ -z "${GIT_BRANCH_SHA}" ]]; then
while read -r line; do
if [[ "${line}" =~ \"head\": ]]; then
foundhead=true
fi
if [[ "${foundhead}" == true ]]; then
if [[ "${line}" =~ \"sha\": ]]; then
sha=${line##* \"}
GIT_BRANCH_SHA=${sha%%\"*}
break
fi
fi
done < <(cat "${PATCH_DIR}/github-pull.json")
fi
GITHUB_ISSUE=${input}
# github will translate this to be #(xx) !
add_footer_table "GITHUB PR" "${GITHUB_BASE_URL}/${GITHUB_REPO}/pull/${input}"
return 0
}
## @description a wrapper for github_locate_pr_patch that
## @description that takes a (likely checkout'ed) github commit
## @description sha and turns into the the github pr
## @audience private
## @stability evolving
## @replaceable no
## @param input
## @param output
## @return 0 on success
## @return 1 on failure
function github_locate_sha_patch
{
declare input=$1
declare patchout=$2
declare diffout=$3
declare gitsha
declare number
gitsha=${input#GHSHA:}
# locate the PR number via GitHub API v3
#curl https://api.github.com/search/issues?q=sha:40a7af3377d8087779bf8ad66397947b7270737a\&type:pr\&repo:apache/yetus
# Let's pull the PR JSON for later use
if ! "${CURL}" --silent --fail \
-H "Accept: application/vnd.github.v3.full+json" \
"${GITHUB_AUTH[@]}" \
--output "${PATCH_DIR}/github-search.json" \
--location \
"${GITHUB_API_URL}/search/issues?q=${gitsha}&type:pr&repo:${GITHUB_REPO}"; then
return 1
fi
# shellcheck disable=SC2016
number=$("${GREP}" number "${PATCH_DIR}/github-search.json" | \
head -1 | \
"${AWK}" '{print $NF}')
number=${number//\s}
number=${number%,}
github_locate_pr_patch "GH:${number}" "${patchout}" "${diffout}"
}
## @description Handle the various ways to reference a github PR
## @audience private
## @stability evolving
## @replaceable no
## @param input
## @param output
## @return 0 on success
## @return 1 on failure
function github_locate_patch
{
declare input=$1
declare patchout=$2
declare diffout=$3
if [[ "${OFFLINE}" == true ]]; then
yetus_debug "github_locate_patch: offline, skipping"
return 1
fi
case "${input}" in
GHSHA:*)
github_locate_sha_patch "${input}" "${patchout}" "${diffout}"
;;
*)
github_locate_pr_patch "${input}" "${patchout}" "${diffout}"
;;
esac
}
## @description Generate a Github Check Run ID
## @stability evolving
## @audience private
function github_start_checkrun
{
declare tempfile="${PATCH_DIR}/ghcheckrun.$$.${RANDOM}"
declare output="${PATCH_DIR}/ghcheckrun.json"
if [[ -z "${GITHUB_SHA}" ]]; then
GITHUB_SHA=$("${GREP}" \"sha\" "${PATCH_DIR}/github-pull.json" 2>/dev/null \
| head -1 \
| cut -f4 -d\")
fi
if [[ -z "${GITHUB_SHA}" ]]; then
return 0
fi
# don't need this under GHA
if [[ "${ROBOTTYPE}" == 'githubactions' ]]; then
return 0
fi
if [[ "${OFFLINE}" == true ]]; then
return 0
fi
if [[ "${#GITHUB_AUTH[@]}" -eq 0 ]]; then
return 0
fi
{
printf "{"
echo "\"name\":\"Apache Yetus ${ROBOTTYPE}\","
echo "\"head_sha\": \"${GITHUB_SHA}\","
echo "\"details_url\": \"${BUILD_URL}${BUILD_URL_CONSOLE}\","
echo "\"external_id\": \"${INSTANCE}\","
echo "\"status\": \"in_progress\","
echo "\"started_at\": \"${ISODATESTART}\""
# external_id instance_id?
# status queued, in_progress, completed
# started_at ISO-8601
# conclusion , required for status=completed, completed_at=value
# success, failure, neutral, cancelled, skipped, timed_out, action_required
# completed_at ISO-8601
# output see github docs @ https://docs.github.com/en/rest/reference/checks#update-a-check-run
echo "}"
} > "${tempfile}"
"${CURL}" --silent --fail -X POST \
-H "Accept: application/vnd.github.antiope-preview+json" \
-H "Content-Type: application/json" \
"${GITHUB_AUTH[@]}" \
-d @"${tempfile}" \
--location \
"${GITHUB_API_URL}/repos/${GITHUB_REPO}/check-runs" \
--output "${output}" 2>/dev/null
GITHUB_CHECK_RUN_ID=$("${GREP}" \"id\" "${output}" 2>/dev/null \
| head -1 \
| cut -f2 -d: \
| cut -f1 -d,)
GITHUB_CHECK_RUN_ID=${GITHUB_CHECK_RUN_ID// /}
}
## @description Generate a Github Check Run ID
## @stability evolving
## @audience private
function github_end_checkrun
{
declare result=$1
declare tempfile="${PATCH_DIR}/ghcheckrun.$$.${RANDOM}"
declare output="${PATCH_DIR}/ghcheckrun-final.json"
declare conclusion
declare detailslink
# don't need this under GHA
if [[ "${ROBOTTYPE}" == 'githubactions' ]]; then
return 0
fi
if [[ "${OFFLINE}" == true ]]; then
return 0
fi
if [[ "${#GITHUB_AUTH[@]}" -eq 0 ]]; then
return 0
fi
if [[ "${result}" -eq 0 ]]; then
conclusion="success"
else
conclusion="failure"
fi
finishdate=$(date +"%Y-%m-%dT%H:%M:%SZ")
# link to the html report if possible
if [[ ${GITHUB_STATUS_USE_HTMLREPORT} == true ]]; then
detailslink=$(htmlout_reportname)
else
detailslink="${BUILD_URL}${BUILD_URL_CONSOLE}"
fi
{
printf "{"
echo "\"conclusion\":\"${conclusion}\","
echo "\"status\": \"completed\","
echo "\"details_url\": \"${detailslink}\","
echo "\"completed_at\": \"${finishdate}\""
echo "}"
} > "${tempfile}"
"${CURL}" --fail --silent -X PATCH \
-H "Accept: application/vnd.github.antiope-preview+json" \
-H "Content-Type: application/json" \
"${GITHUB_AUTH[@]}" \
-d @"${tempfile}" \
--location \
"${GITHUB_API_URL}/repos/${GITHUB_REPO}/check-runs/${GITHUB_CHECK_RUN_ID}" \
--output "${output}" 2>/dev/null
rm "${tempfile}"
}
## @description Write a Github Checks Annotation
## @param filename
## @param linenum
## @param column
## @param plugin
## @param text
## @stability evolving
## @audience private
function github_linecomments
{
declare file=$1
declare linenum=$2
declare column=$3
declare plugin=$4
shift 4
declare text=$*
declare tempfile="${PATCH_DIR}/ghcomment.$$.${RANDOM}"
declare header
declare -a linehandler
declare -a colhandler
if [[ "${ROBOTTYPE}" == 'githubactions' ]]; then
if [[ -z "${column}" ]] || [[ "${column}" == 0 ]]; then
echo "::error file=${file},line=${linenum}::${plugin}:${text}"
else
echo "::error file=${file},line=${linenum},col=${column}::${plugin}:${text}"
fi
return 0
fi
if [[ "${OFFLINE}" == true ]]; then
yetus_error "WARNING: Running offline, GitHub annotations skipped."
return 0
fi
if [[ "${#GITHUB_AUTH[@]}" -eq 0 ]]; then
return 0
fi
if [[ ${GITHUB_ANNOTATION_LIMIT} -eq 0 ]]; then
return 0
fi
((GITHUB_ANNOTATION_LIMIT=GITHUB_ANNOTATION_LIMIT - 1))
if [[ ${GITHUB_ANNOTATION_LIMIT} -eq 0 ]]; then
yetus_error "WARNING: GitHub annotations limit reached."
return 0
fi
if [[ -z "${GITHUB_REPO}" ]]; then
yetus_error "ERROR: --github-repo is not defined."
return 1
fi
if [[ -z "${GITHUB_CHECK_RUN_ID}" ]]; then
if ! github_start_checkrun; then
yetus_error "ERROR: Cannot generate a Github Check Run ID"
return 1
fi
fi
linehandler=(\"start_line\": "${linenum},")
linehandler+=(\"end_line\": "${linenum},")
if [[ -z "${column}" ]] || [[ "${column}" == 0 ]]; then
colhandler=()
else
colhandler=(\"start_column\": "${column},")
colhandler+=(\"end_column\": "${column},")
fi
newtext=$(echo "${text[*]}" | "${SED}" -e 's,\\,\\\\,g' \
-e 's,\",\\\",g' \
-e 's,$,\\r\\n,g' \
| tr -d '\n')
if [[ "${ROBOTTYPE}" ]]; then
header="Apache Yetus(${ROBOTTYPE})"
else
header="Apache Yetus"
fi
cat <<EOF > "${tempfile}"
{
"output": {
"title": "${header}",
"summary": "Precommit Problem",
"annotations" : [{
"path": "${file}",
${linehandler[@]}
${colhandler[@]}
"annotation_level": "failure",
"message": "${plugin}: ${newtext}"
}]
}
}
EOF
"${CURL}" --silent --fail -X PATCH \
-H "Accept: application/vnd.github.antiope-preview+json" \
-H "Content-Type: application/json" \
"${GITHUB_AUTH[@]}" \
--output "${PATCH_DIR}/github-check-annotation-response.json" \
-d @"${tempfile}" \
--location \
"${GITHUB_API_URL}/repos/${GITHUB_REPO}/check-runs/${GITHUB_CHECK_RUN_ID}" \
2>/dev/null
rm "${tempfile}"
}
## @description Write the contents of a file to github
## @param filename
## @stability stable
## @audience public
function github_write_comment
{
declare -r commentfile=${1}
declare retval=0
declare restfile="${PATCH_DIR}/ghcomment.$$"
if [[ "${OFFLINE}" == true ]]; then
yetus_error "WARNING: Running offline, GitHub comment skipped."
return 0
fi
if [[ -z "${GITHUB_REPO}" ]]; then
yetus_error "ERROR: --github-repo is not defined."
return 0
fi
{
printf "{\"body\":\""
"${SED}" -e 's,\\,\\\\,g' \
-e 's,\",\\\",g' \
-e 's,$,\\r\\n,g' "${commentfile}" \
| tr -d '\n'
echo "\"}"
} > "${restfile}"
if [[ "${#GITHUB_AUTH[@]}" -lt 1 ]]; then
yetus_error "ERROR: No GitHub credentials defined."
return 0
fi
"${CURL}" --silent --fail -X POST \
-H "Accept: application/vnd.github.v3.full+json" \
-H "Content-Type: application/json" \
"${GITHUB_AUTH[@]}" \
-d @"${restfile}" \
--output "${PATCH_DIR}/github-comment-write-response.json" \
--location \
"${GITHUB_API_URL}/repos/${GITHUB_REPO}/issues/${GITHUB_ISSUE}/comments" \
>/dev/null
retval=$?
rm "${restfile}"
return ${retval}
}
## @description Print out the finished details to the Github PR
## @audience private
## @stability evolving
## @replaceable no
## @param runresult
function github_finalreport_as_comment
{
declare result=$1
declare i
declare commentfile=${PATCH_DIR}/gitcommentfile.$$
declare comment
declare url
declare ela
declare subs
declare logfile
declare calctime
declare vote
declare emoji
rm "${commentfile}" 2>/dev/null
if [[ ${ROBOT} = "false"
|| -z ${GITHUB_ISSUE} ]] ; then
return 0
fi
url=$(get_artifact_url)
big_console_header "Adding comment to Github"
if [[ ${result} == 0 ]]; then
echo ":confetti_ball: **+1 overall**" >> "${commentfile}"
else
echo ":broken_heart: **-1 overall**" >> "${commentfile}"
fi
printf '\n\n\n\n' >> "${commentfile}"
i=0
until [[ ${i} -ge ${#TP_HEADER[@]} ]]; do
printf '%s\n\n' "${TP_HEADER[${i}]}" >> "${commentfile}"
((i=i+1))
done
{
printf '\n\n'
echo "| Vote | Subsystem | Runtime | Logfile | Comment |"
echo "|:----:|----------:|--------:|:--------:|:-------:|"
} >> "${commentfile}"
i=0
until [[ ${i} -ge ${#TP_VOTE_TABLE[@]} ]]; do
ourstring=$(echo "${TP_VOTE_TABLE[${i}]}" | tr -s ' ')
vote=$(echo "${ourstring}" | cut -f2 -d\| | tr -d ' ')
subs=$(echo "${ourstring}" | cut -f3 -d\|)
ela=$(echo "${ourstring}" | cut -f4 -d\|)
calctime=$(clock_display "${ela}")
logfile=$(echo "${ourstring}" | cut -f5 -d\| | tr -d ' ')
comment=$(echo "${ourstring}" | cut -f6 -d\|)
if [[ "${vote}" = "H" ]]; then
echo "|||| _${comment}_ |" >> "${commentfile}"
((i=i+1))
continue
fi
if [[ ${GITHUB_USE_EMOJI_VOTE} == true ]]; then
emoji=""
case ${vote} in
1|"+1")
emoji="+1 :green_heart:"
;;
-1)
emoji="-1 :x:"
;;
0)
emoji="+0 :ok:"
;;
-0)
emoji="-0 :warning:"
;;
H)
# this never gets called (see above) but this is here so others know the color is taken
emoji=""
;;
*)
# usually this should not happen but let's keep the old vote result if it happens
emoji=${vote}
;;
esac
else
emoji="${vote}"
fi
if [[ -n "${logfile}" ]]; then
t1=${logfile/@@BASE@@/}
t2=$(echo "${logfile}" | "${SED}" -e "s,@@BASE@@,${url},g")
t2="[${t1}](${t2})"
else
t2=""
fi
printf '| %s | %s | %s | %s | %s |\n' \
"${emoji}" \
"${subs}" \
"${calctime}" \
"${t2}" \
"${comment}" \
>> "${commentfile}"
((i=i+1))
done
if [[ ${#TP_TEST_TABLE[@]} -gt 0 ]]; then
{
printf '\n\n'
echo "| Reason | Tests |"
echo "|-------:|:------|"
} >> "${commentfile}"
i=0
until [[ ${i} -ge ${#TP_TEST_TABLE[@]} ]]; do
echo "${TP_TEST_TABLE[${i}]}" >> "${commentfile}"
((i=i+1))
done
fi
{
printf '\n\n'
echo "| Subsystem | Report/Notes |"
echo "|----------:|:-------------|"
} >> "${commentfile}"
i=0
until [[ $i -ge ${#TP_FOOTER_TABLE[@]} ]]; do
comment=$(echo "${TP_FOOTER_TABLE[${i}]}" | "${SED}" -e "s,@@BASE@@,${url},g")
printf '%s\n' "${comment}" >> "${commentfile}"
((i=i+1))
done
printf '\n\nThis message was automatically generated.\n\n' >> "${commentfile}"
github_write_comment "${commentfile}"
}
## @description Write a github status
## @audience private
## @stability evolving
## @replaceable no
## @param runresult
function github_status_write
{
declare filename=$1
declare retval=0
declare recoverydir="${PATCH_DIR}/github-status-retry/${GITHUB_REPO}/${GIT_BRANCH_SHA}"
if [[ "${OFFLINE}" == true ]]; then
return 0
fi
if [[ "${#GITHUB_AUTH[@]}" -lt 1 ]]; then
echo "Github Plugin: no credentials provided to write a status."
return 0
fi
if [[ -z "${GITHUB_REPO}" ]]; then
echo "GitHub Repo not defined."
return 0
fi
"${CURL}" --silent --fail -X POST \
-H "Accept: application/vnd.github.v3.full+json" \
-H "Content-Type: application/json" \
"${GITHUB_AUTH[@]}" \
-d @"${filename}" \
--location \
"${GITHUB_API_URL}/repos/${GITHUB_REPO}/statuses/${GIT_BRANCH_SHA}" \
--output "${PATCH_DIR}/gitub-status-response-${GITHUB_STATUS_RECOVERY_COUNTER}.json" \
2>/dev/null
retval=$?
if [[ ${retval} -gt 0 ]]; then
yetus_error "ERROR: Failed to write github status. Token expired or missing repo:status write?"
if [[ "${GITHUB_STATUS_RECOVER_TOOL}" == false ]]; then
mkdir -p "${recoverydir}"
cp -p "${tempfile}" "${recoverydir}/${GITHUB_STATUS_RECOVERY_COUNTER}.json"
((GITHUB_STATUS_RECOVERY_COUNTER=GITHUB_STATUS_RECOVERY_COUNTER+1))
echo "${RESULT}" > "${PATCH_DIR}/github-status-retry/finalresult.txt"