40
40
readonly IS_PROW
41
41
[[ ! -v REPO_ROOT_DIR ]] && REPO_ROOT_DIR=" $( git rev-parse --show-toplevel) "
42
42
readonly REPO_ROOT_DIR
43
- readonly REPO_NAME=" ${REPO_NAME:- $(basename " ${REPO_ROOT_DIR} " )} "
43
+
44
+ # Resolves the repository name given a root directory.
45
+ # Parameters: $1 - repository root directory.
46
+ function __resolveRepoName() {
47
+ local repoName
48
+ repoName=" $( basename " ${1:- $(git rev-parse --show-toplevel)} " ) "
49
+ repoName=" ${repoName# knative-sandbox-} " # Remove knative-sandbox- prefix if any
50
+ repoName=" ${repoName# knative-} " # Remove knative- prefix if any
51
+ echo " ${repoName} "
52
+ }
53
+ default_repo_name=" $( __resolveRepoName " ${REPO_ROOT_DIR} " ) "
54
+ readonly REPO_NAME=" ${REPO_NAME:- $default_repo_name } "
55
+ unset default_repo_name
44
56
45
57
# Useful flags about the current OS
46
58
IS_LINUX=0
@@ -65,10 +77,6 @@ if [[ -z "${ARTIFACTS:-}" ]]; then
65
77
fi
66
78
mkdir -p " $ARTIFACTS "
67
79
68
-
69
- # On a Prow job, redirect stderr to stdout so it's synchronously added to log
70
- (( IS_PROW )) && exec 2>&1
71
-
72
80
# Return the major version of a release.
73
81
# For example, "v0.2.1" returns "0"
74
82
# Parameters: $1 - release version label.
@@ -94,23 +102,63 @@ function patch_version() {
94
102
echo " ${tokens[2]} "
95
103
}
96
104
97
- # Print error message and exit 1
105
+ # Calculates the hashcode for a given string.
106
+ # Parameters: $* - string to be hashed.
107
+ # See: https://stackoverflow.com/a/48863502/844449
108
+ function hashCode() {
109
+ local input=" $1 "
110
+ local -i h=0
111
+ for (( i = 0 ; i < ${# input} ; i++ )) ; do
112
+ # val is ASCII val
113
+ printf -v val " %d" " '${input: $i : 1} "
114
+ hval=$(( 31 * h + val))
115
+ # hash scheme
116
+ if (( hval > 2147483647 )) ; then
117
+ h=$(( (hval - 2147483648 ) % 2147483648 ))
118
+ elif (( hval < - 2147483648 )) ; then
119
+ h=$(( (hval + 2147483648 ) % 2147483648 ))
120
+ else
121
+ h=$(( hval ))
122
+ fi
123
+ done
124
+ # final hashCode in decimal
125
+ printf " %d" $h
126
+ }
127
+
128
+ # Calculates the retcode for a given string. Makes sure the return code is
129
+ # non-zero.
130
+ # Parameters: $* - string to be hashed.
131
+ function calcRetcode() {
132
+ local rc=1
133
+ local rcc
134
+ rcc=" $( hashCode " $* " ) "
135
+ if [[ $rcc != 0 ]]; then
136
+ rc=$(( rcc % 255 ))
137
+ fi
138
+ echo " $rc "
139
+ }
140
+
141
+ # Print error message and call exit(n) where n calculated from the error message.
98
142
# Parameters: $1..$n - error message to be displayed
143
+ # Globals: abort_retcode will change the default retcode to be returned
99
144
function abort() {
100
- echo " error: $* " >&2
101
- exit 1
145
+ make_banner ' *' " ERROR: $* " >&2
146
+ readonly abort_retcode=" ${abort_retcode:- $(calcRetcode " $* " )} "
147
+ exit " $abort_retcode "
102
148
}
103
149
104
150
# Display a box banner.
105
151
# Parameters: $1 - character to use for the box.
106
152
# $2 - banner message.
107
153
function make_banner() {
108
154
local msg=" $1$1$1$1 $2 $1$1$1$1 "
109
- local border=" ${msg// [-0-9A-Za-z _.,\/()\' ]/ $1 } "
155
+ local border=" ${msg// [^$1 ]/ $1 } "
110
156
echo -e " ${border} \n${msg} \n${border} "
111
157
# TODO(adrcunha): Remove once logs have timestamps on Prow
112
158
# For details, see https://github.com/kubernetes/test-infra/issues/10100
113
- echo -e " $1$1$1$1 $( TZ=' UTC' date) \n${border} "
159
+ if (( IS_PROW )) ; then
160
+ echo -e " $1$1$1$1 $( TZ=' UTC' date --rfc-3339=ns) \n${border} "
161
+ fi
114
162
}
115
163
116
164
# Simple header for logging purposes.
@@ -126,7 +174,7 @@ function subheader() {
126
174
127
175
# Simple warning banner for logging purposes.
128
176
function warning() {
129
- make_banner ' !' " $* " >&2
177
+ make_banner ' !' " WARN: $* " >&2
130
178
}
131
179
132
180
# Checks whether the given function exists.
@@ -448,14 +496,14 @@ function report_go_test() {
448
496
logfile=" ${xml/ junit_/ go_test_} "
449
497
logfile=" ${logfile/ .xml/ .jsonl} "
450
498
echo " Running go test with args: ${go_test_args[*]} "
499
+ local gotest_retcode=0
451
500
go_run gotest.tools/
[email protected] \
452
501
--format " ${GO_TEST_VERBOSITY:- testname} " \
453
502
--junitfile " ${xml} " \
454
503
--junitfile-testsuite-name relative \
455
504
--junitfile-testcase-classname relative \
456
505
--jsonfile " ${logfile} " \
457
- -- " ${go_test_args[@]} "
458
- local gotest_retcode=$?
506
+ -- " ${go_test_args[@]} " || gotest_retcode=$?
459
507
echo " Finished run, return code is ${gotest_retcode} "
460
508
461
509
echo " XML report written to ${xml} "
@@ -558,6 +606,9 @@ function go_run() {
558
606
if [[ " $package " != * @* ]]; then
559
607
abort ' Package for "go_run" needs to have @version'
560
608
fi
609
+ if [[ " $package " == * @latest ]] && [[ " $package " != knative.dev* ]]; then
610
+ warning ' Using @latest version for external dependencies is unsafe. Use numbered version!'
611
+ fi
561
612
shift 1
562
613
GORUN_PATH=" ${GORUN_PATH:- $(go env GOPATH)} "
563
614
# Some CI environments may have non-writable GOPATH
0 commit comments