2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2018 The Linux Foundation and others.
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.
14 # Need to use git-review >= 1.28 to be compatible with Gerrit 3
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.
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.
26 # This script expects the following environment variables to be set in the
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
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"
45 echo -e "INFO: Staged for commit:\n$staged_commits\n"
47 # shellcheck disable=SC1090
50 lf-activate-venv "git-review>=1.28"
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")
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\nChange-Id: $change_id"
65 echo "NOTE: No gerrit review found"
68 git commit -sm "$GERRIT_COMMIT_MESSAGE" -m "$message"
71 git remote add gerrit "ssh://$GERRIT_USER@$GERRIT_HOST:29418/$PROJECT.git"
73 # If the reviewers email is unset/empty then use a default
74 reviewers_email=${REVIEWERS_EMAIL:-"$GERRIT_USER@$GERRIT_HOST"}
76 git review --yes -t "$GERRIT_TOPIC" --reviewers "$reviewers_email"