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 # The script requires to install the minimum version 1.25 of git-review using
15 # virtualenv and pip install which supports `--reviewers` option.
17 # The script allows a job to push a patch to Gerrit in an automated fashion.
18 # This is meant for tasks that creates the same patch regularly and needs the
19 # ability to detect if an unreviewed patch already exists. In which case it
20 # will update the existing patch.
22 # Note: This patch assumes the $WORKSPACE contains the project repo with
23 # the files changed already "git add" and waiting for a "git commit" call.
25 # This script requires the following JJB variables to be passed in:
27 # $PROJECT : Gerrit project-name
28 # $GERRIT_COMMIT_MESSAGE: Commit message to assign to commit
29 # $GERRIT_HOST : Gerrit hostname
30 # $GERRIT_TOPIC : Gerrit topic, please make a unique topic
31 # $GERRIT_USER : Gerrit user
32 # $REVIEWERS_EMAIL : Reviewers email
34 # TODO: remove the workaround when v1.26 is available on all images
35 # Workaround for git-review bug in v1.24
36 # https://storyboard.openstack.org/#!/story/2001081
37 set +u # Allow unbound variables for virtualenv
38 virtualenv --quiet "/tmp/v/git-review"
39 # shellcheck source=/tmp/v/git-review/bin/activate disable=SC1091
40 source "/tmp/v/git-review/bin/activate"
41 pip install --quiet --upgrade "pip==9.0.3" setuptools
42 pip install --quiet --upgrade git-review
44 # End git-review workaround
45 # Remove any leading or trailing quotes surrounding the strings
46 # which can cause parse errors when passed as CLI options to commands
47 PROJECT="$(echo "$PROJECT" | sed "s/^\([\"']\)\(.*\)\1\$/\2/g")"
48 GERRIT_COMMIT_MESSAGE="$(echo "$GERRIT_COMMIT_MESSAGE" | sed "s/^\([\"']\)\(.*\)\1\$/\2/g")"
49 GERRIT_HOST="$(echo "$GERRIT_HOST" | sed "s/^\([\"']\)\(.*\)\1\$/\2/g")"
50 GERRIT_TOPIC="$(echo "$GERRIT_TOPIC" | sed "s/^\([\"']\)\(.*\)\1\$/\2/g")"
51 GERRIT_USER="$(echo "$GERRIT_USER" | sed "s/^\([\"']\)\(.*\)\1\$/\2/g")"
52 REVIEWERS_EMAIL="$(echo "$REVIEWERS_EMAIL" | sed "s/^\([\"']\)\(.*\)\1\$/\2/g")"
54 CHANGE_ID=$(ssh -p 29418 "$GERRIT_USER@$GERRIT_HOST" gerrit query \
55 limit:1 owner:self is:open project:"$PROJECT" \
56 message: "$GERRIT_COMMIT_MESSAGE" \
57 topic: "$GERRIT_TOPIC" | \
61 if [ -z "$CHANGE_ID" ]; then
62 git commit -sm "$GERRIT_COMMIT_MESSAGE"
64 git commit -sm "$GERRIT_COMMIT_MESSAGE" -m "Change-Id: $CHANGE_ID"
68 git remote add gerrit "ssh://$GERRIT_USER@$GERRIT_HOST:29418/$PROJECT.git"
70 # if the reviewers email is empty then use a default
71 REVIEWERS_EMAIL=${REVIEWERS_EMAIL:-"$GERRIT_USER@$GERRIT_HOST"}
73 # Don't fail the build if this command fails because it's possible that there
74 # is no changes since last update.
75 git review --yes -t "$GERRIT_TOPIC" --reviewers "$REVIEWERS_EMAIL" || true