Chore: Upgrade Jenkins-job-builder to 6.3.0
[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 echo "---> gerrit-fetch-dependencies.sh"
12 # Fetches patches all projects provided by comment trigger
13 #
14 # Takes a list of Gerrit patches and fetches all projects and cherry-pick
15 # patches for projects. The trigger is
16 #     'recheck: SPACE_SEPERATED_LIST_OF_PATCHES'
17 #     or
18 #     'reverify: SPACE_SEPERATED_LIST_OF_PATCHES'
19 #
20 # NOTE: This script assumes the user will provide the correct dependency order
21 #       via the PATCHES list.
22
23 # Ensure we fail the job if any steps fail.
24 set -eu -o pipefail
25
26 REPOS_DIR="$WORKSPACE/.repos"
27
28 IFS=" " read -r -a PATCHES <<< \
29         "$(echo "$GERRIT_EVENT_COMMENT_TEXT" | \
30             grep -E '(recheck:|reverify:)' | awk -F: '{print $2}')"
31
32 # shellcheck disable=SC1090
33 source ~/lf-env.sh
34
35 lf-activate-venv "git-review==1.28"
36
37
38 projects=()
39 for patch in "${PATCHES[@]}"; do
40     json=$(curl -s "$GERRIT_URL/changes/$patch" | sed -e "s/)]}'//")
41     project=$(echo "$json" | jq -r '.project')
42     branch=$(echo "$json" | jq -r '.branch')
43
44     if [ "$GERRIT_CHANGE_NUMBER" == "$patch" ]; then
45         echo "WARN: GERRIT_CHANGE and $patch are one and the same. Ignoring patch..."
46         continue
47     fi
48
49     if [ ! -d "$REPOS_DIR/$project" ]; then
50         git clone -q --depth 1 -b "$branch" "$GIT_URL/$project" "$REPOS_DIR/$project"
51
52         # This array will be used later to determine project build order.
53         projects+=("$project")
54     fi
55
56     pushd "$REPOS_DIR/$project"
57     # If remote gerrit already exists just make sure path is expected
58     if ! git remote add gerrit "$GERRIT_URL/$project" > /dev/null 2>&1; then
59         git remote set-url gerrit "$GERRIT_URL/$project"
60     fi
61     git review --cherrypick="$patch"
62     popd
63 done
64
65 # This script should be a macro which re-inject's the projects variable back
66 # into the build so a script later on can use it.
67 echo "DEPENDENCY_BUILD_ORDER=${projects[*]}" > "$WORKSPACE/.dependency.properties"