feat(jobspec): Unify soak jobspecs
[csit.git] / resources / libraries / bash / function / branch.sh
1 # Copyright (c) 2020 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 set -exuo pipefail
15
16 # This library defines functions related to handling VPP and CSIT git branches.
17 # Keep functions ordered alphabetically, please.
18
19 # TODO: Add a link to bash style guide.
20
21
22 function checkout_csit_for_vpp () {
23
24     # This should be useful mainly for vpp-csit jobs (and timed csit-vpp jobs),
25     # which want to use csit oper branches (especially for vpp stable branches).
26     # This allows the Jenkins job to checkout CSIT master branch,
27     # and use this function to compute and checkout the final CSIT branch.
28     # When the refspec is overriden, the computation is still performed,
29     # in order to show (on Sandbox) the computation is correct.
30     #
31     # Git status and git log -1 are executed, so the current state
32     # of the checked-out branch is visible.
33     # This is mainly useful for identifying trending anomalies
34     # caused by changes in CSIT code.
35     #
36     # On failure, working directory could remain changed to ${CSIT_DIR}.
37     # TODO: It could be possible to use ERR trap to force popd,
38     #   but with "set -x" the noise is not worth it,
39     #   especially if several levels of pushd are to be supported.
40     #
41     # Arguments:
42     # - ${1} - Git branch of VPP code, e.g. GERRIT_BRANCH set by Jenkins.
43     #   This is not read from GERRIT_BRANCH directly,
44     #   because in csit-vpp jobs that refers to CSIT branch instead.
45     #   Required.
46     # Variables read:
47     # - CSIT_REF - If set and non-empty, override the computed refspec.
48     # - CSIT_DIR - Path to existing root of local CSIT git repository.
49     #   The repository could be cloned with "--depth 1",
50     #   but it is required to be cloned with "--no-single-branch",
51     #   as otherwise the "git checkout" this function performs probably fails.
52     # Directoried updated:
53     # - ${CSIT_DIR} - Probably "git checkout"ed into new refspec.
54     # Functions called:
55     # - die - Print to stderr and exit, defined in "common" library.
56
57     set -exuo pipefail
58
59     case "${1}" in
60         "stable/"*)
61             branch_id="origin/${1/stable\//oper-rls}"
62             ;;
63         "rls"*)
64             branch_id="origin/oper-${1}"
65             ;;
66         *)  # This includes "master".
67             branch_id="origin/oper"
68     esac
69     # Get the latest verified version of the required branch.
70     pushd "${CSIT_DIR}" || die
71     csit_branches="$(git branch -r | grep -E "${branch_id}-[0-9]+")" || {
72         # We might be in time when VPP has cut their new branch,
73         # but CSIT not, yet. Use master oper branch in this case.
74         csit_branches="$(git branch -r | grep -E "origin/oper-[0-9]+")" || die
75     }
76     # The xargs is there just to remove leading (or trailing) spaces.
77     csit_branch="$(echo "${csit_branches}" | tail -n 1 | xargs)" || die
78     if [[ -z "${csit_branch}" ]]; then
79         die "No verified CSIT branch found - exiting."
80     fi
81     # Remove 'origin/' from the branch name.
82     csit_branch="${csit_branch#origin/}" || die
83     override_ref="${CSIT_REF-}"
84     if [[ -n "${override_ref}" ]]; then
85         git fetch --depth=1 https://gerrit.fd.io/r/csit "${override_ref}" || die
86         git checkout FETCH_HEAD || die
87     else
88         git checkout "${csit_branch}" || die
89     fi
90     git status || die
91     git log -1 || die
92     popd || die
93 }