Update remote gerrit to use GERRIT_URL
[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 #
17 # NOTE: This script assumes the user will provide the correct dependency order
18 #       via the PATCHES list.
19
20 # Ensure we fail the job if any steps fail.
21 set -eu -o pipefail
22
23 REPOS_DIR="$WORKSPACE/.repos"
24
25 IFS=" " read -r -a PATCHES <<< "$(echo "$GERRIT_EVENT_COMMENT_TEXT" | grep 'recheck:' | awk -F: '{print $2}')"
26
27 projects=()
28 for patch in $(echo "${PATCHES[@]}"); do
29     json=$(curl -s "$GERRIT_URL/changes/$patch" | sed -e "s/)]}'//")
30     project=$(echo "$json" | jq -r '.project')
31     branch=$(echo "$json" | jq -r '.branch')
32
33     if [ ! -d "$REPOS_DIR/$project" ]; then
34         git clone -q --depth 1 -b "$branch" "$GIT_URL/$project" "$REPOS_DIR/$project"
35
36         # This array will be used later to determine project build order.
37         projects+=("$project")
38     fi
39
40     # Workaround for git-review bug in v1.24
41     # https://storyboard.openstack.org/#!/story/2001081
42     set +u  # Allow unbound variables for virtualenv
43     virtualenv --quiet "/tmp/v/git-review"
44     # shellcheck source=/tmp/v/git-review/bin/activate disable=SC1091
45     source "/tmp/v/git-review/bin/activate"
46     pip install --quiet --upgrade pip
47     pip install --quiet --upgrade git-review
48     set -u
49     # End git-review workaround
50
51     pushd "$REPOS_DIR/$project"
52     # If remote gerrit already exists just make sure path is expected
53     if ! git remote add gerrit "$GERRIT_URL/$project" > /dev/null 2>&1; then
54         git remote set-url gerrit "$GERRIT_URL/$project"
55     fi
56     git review --cherrypick="$patch"
57     popd
58 done
59
60 # This script should be a macro which re-inject's the projects variable back
61 # into the build so a script later on can use it.
62 echo "DEPENDENCY_BUILD_ORDER=${projects[*]}" > "$WORKSPACE/.dependency.properties"