4040readonly IS_PROW
4141[[ ! -v REPO_ROOT_DIR ]] && REPO_ROOT_DIR=" $( git rev-parse --show-toplevel) "
4242readonly 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
4456
4557# Useful flags about the current OS
4658IS_LINUX=0
@@ -65,10 +77,6 @@ if [[ -z "${ARTIFACTS:-}" ]]; then
6577fi
6678mkdir -p " $ARTIFACTS "
6779
68-
69- # On a Prow job, redirect stderr to stdout so it's synchronously added to log
70- (( IS_PROW )) && exec 2>&1
71-
7280# Return the major version of a release.
7381# For example, "v0.2.1" returns "0"
7482# Parameters: $1 - release version label.
@@ -94,23 +102,63 @@ function patch_version() {
94102 echo " ${tokens[2]} "
95103}
96104
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.
98142# Parameters: $1..$n - error message to be displayed
143+ # Globals: abort_retcode will change the default retcode to be returned
99144function abort() {
100- echo " error: $* " >&2
101- exit 1
145+ make_banner ' *' " ERROR: $* " >&2
146+ readonly abort_retcode=" ${abort_retcode:- $(calcRetcode " $* " )} "
147+ exit " $abort_retcode "
102148}
103149
104150# Display a box banner.
105151# Parameters: $1 - character to use for the box.
106152# $2 - banner message.
107153function make_banner() {
108154 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 } "
110156 echo -e " ${border} \n${msg} \n${border} "
111157 # TODO(adrcunha): Remove once logs have timestamps on Prow
112158 # 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
114162}
115163
116164# Simple header for logging purposes.
@@ -126,7 +174,7 @@ function subheader() {
126174
127175# Simple warning banner for logging purposes.
128176function warning() {
129- make_banner ' !' " $* " >&2
177+ make_banner ' !' " WARN: $* " >&2
130178}
131179
132180# Checks whether the given function exists.
@@ -448,14 +496,14 @@ function report_go_test() {
448496 logfile=" ${xml/ junit_/ go_test_} "
449497 logfile=" ${logfile/ .xml/ .jsonl} "
450498 echo " Running go test with args: ${go_test_args[*]} "
499+ local gotest_retcode=0
451500 go_run gotest.tools/
[email protected] \
452501 --format " ${GO_TEST_VERBOSITY:- testname} " \
453502 --junitfile " ${xml} " \
454503 --junitfile-testsuite-name relative \
455504 --junitfile-testcase-classname relative \
456505 --jsonfile " ${logfile} " \
457- -- " ${go_test_args[@]} "
458- local gotest_retcode=$?
506+ -- " ${go_test_args[@]} " || gotest_retcode=$?
459507 echo " Finished run, return code is ${gotest_retcode} "
460508
461509 echo " XML report written to ${xml} "
@@ -558,6 +606,9 @@ function go_run() {
558606 if [[ " $package " != * @* ]]; then
559607 abort ' Package for "go_run" needs to have @version'
560608 fi
609+ if [[ " $package " == * @latest ]] && [[ " $package " != knative.dev* ]]; then
610+ warning ' Using @latest version for external dependencies is unsafe. Use numbered version!'
611+ fi
561612 shift 1
562613 GORUN_PATH=" ${GORUN_PATH:- $(go env GOPATH)} "
563614 # Some CI environments may have non-writable GOPATH
0 commit comments