Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / gerrit-push-patch.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2018 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-push-patch.sh"
12 # Push a change to Gerrit if files modified in repository.
13 #
14 # Need to use git-review >= 1.28 to be compatible with Gerrit 3
15 #
16 # The script allows a job to push a patch to Gerrit in an automated fashion.
17 # This is meant for tasks that creates the same patch regularly and needs the
18 # ability to detect if an unreviewed patch already exists. In which case it
19 # will update the existing patch.
20 #
21 # Note: This script expects $WORKSPACE to point to a project git repo that
22 # may contain staged commits. This script will exit with OK status if no
23 # staged commits are present, otherwise the staged commits will be commited and
24 # a gerrit review will be created.
25 #
26 # This script expects the following environment variables to be set in the
27 # JJB configuration
28 #
29 #   $PROJECT              : Gerrit project-name
30 #   $GERRIT_COMMIT_MESSAGE: Commit message to assign to commit
31 #   $GERRIT_HOST          : Gerrit hostname
32 #   $GERRIT_TOPIC         : Gerrit topic, please make a unique topic
33 #   $GERRIT_USER          : Gerrit user
34 #   $REVIEWERS_EMAIL      : Reviewers email
35
36 set -eufo pipefail
37
38 # No reason to continue if there are no staged commits
39 staged_commits=$(git diff --cached --name-only)
40 if [[ -z $staged_commits ]]; then
41     echo "INFO: Nothing to commit"
42     exit 0
43 fi
44
45 echo -e "INFO: Staged for commit:\n$staged_commits\n"
46
47 # shellcheck disable=SC1090
48 source ~/lf-env.sh
49
50 lf-activate-venv --python python3 "git-review==2.3.1"
51
52 # Query for a pre-existing gerrit review
53 query_result=$(ssh -p 29418 "$GERRIT_USER@$GERRIT_HOST" gerrit query \
54                 limit:1 owner:self is:open project:"$PROJECT"        \
55                 message: "$GERRIT_COMMIT_MESSAGE"                    \
56                 topic: "$GERRIT_TOPIC")
57
58 # Extract the change_id from the query_result
59 job=$JOB_NAME/$BUILD_NUMBER
60 # If available, add change_id to commit message
61 if change_id=$(echo "$query_result" | grep 'Change-Id:' | awk '{print $2}'); then
62     echo "NOTE: Found gerrit review: $change_id"
63     message="Job: $job"$'\n'"Change-Id: $change_id"
64 else
65     echo "NOTE: No gerrit review found"
66     message="Job: $job"
67 fi
68 git commit -sm "$GERRIT_COMMIT_MESSAGE
69
70 $message"
71
72 git status
73 git remote add gerrit "ssh://$GERRIT_USER@$GERRIT_HOST:29418/$PROJECT.git"
74
75 # If the reviewers email is unset/empty then use a default
76 reviewers_email=${REVIEWERS_EMAIL:-"$GERRIT_USER@$GERRIT_HOST"}
77
78 # Workaround for git-review failing to copy the commit-msg hook to submodules
79 git config core.hooksPath "$(git rev-parse --show-toplevel)/.git/hooks"
80
81 git review --yes -t "$GERRIT_TOPIC" --reviewers "$reviewers_email"