Make sure that jjb-cleanup.sh allows unbound vars
[releng/global-jjb.git] / shell / gerrit-fetch-dependencies.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 # Fetches patches all projects provided by comment trigger
12 #
13 # Takes a list of Gerrit patches and fetches all projects and cherry-pick
14 # patches for projects. The trigger is
15 #     'recheck: SPACE_SEPERATED_LIST_OF_PATCHES'
16 #     or
17 #     'reverify: SPACE_SEPERATED_LIST_OF_PATCHES'
18 #
19 # NOTE: This script assumes the user will provide the correct dependency order
20 #       via the PATCHES list.
21
22 # Ensure we fail the job if any steps fail.
23 set -eu -o pipefail
24
25 REPOS_DIR="$WORKSPACE/.repos"
26
27 IFS=" " read -r -a PATCHES <<< "$(echo "$GERRIT_EVENT_COMMENT_TEXT" | grep -E '(recheck:|reverify:)' | awk -F: '{print $2}')"
28
29 # Workaround for git-review bug in v1.24
30 # https://storyboard.openstack.org/#!/story/2001081
31 set +u  # Allow unbound variables for virtualenv
32 virtualenv --quiet "/tmp/v/git-review"
33 # shellcheck source=/tmp/v/git-review/bin/activate disable=SC1091
34 source "/tmp/v/git-review/bin/activate"
35 pip install --quiet --upgrade "pip==9.0.3" setuptools
36 pip install --quiet --upgrade git-review
37 set -u
38 # End git-review workaround
39
40 projects=()
41 for patch in $(echo "${PATCHES[@]}"); do
42     json=$(curl -s "$GERRIT_URL/changes/$patch" | sed -e "s/)]}'//")
43     project=$(echo "$json" | jq -r '.project')
44     branch=$(echo "$json" | jq -r '.branch')
45
46     if [ "$GERRIT_CHANGE_NUMBER" == "$patch" ]; then
47         echo "WARN: GERRIT_CHANGE and $patch are one and the same. Ignoring patch..."
48         continue
49     fi
50
51     if [ ! -d "$REPOS_DIR/$project" ]; then
52         git clone -q --depth 1 -b "$branch" "$GIT_URL/$project" "$REPOS_DIR/$project"
53
54         # This array will be used later to determine project build order.
55         projects+=("$project")
56     fi
57
58     pushd "$REPOS_DIR/$project"
59     # If remote gerrit already exists just make sure path is expected
60     if ! git remote add gerrit "$GERRIT_URL/$project" > /dev/null 2>&1; then
61         git remote set-url gerrit "$GERRIT_URL/$project"
62     fi
63     git review --cherrypick="$patch"
64     popd
65 done
66
67 # This script should be a macro which re-inject's the projects variable back
68 # into the build so a script later on can use it.
69 echo "DEPENDENCY_BUILD_ORDER=${projects[*]}" > "$WORKSPACE/.dependency.properties"