Fix github-maven-merge scm config
[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 # Push a change to Gerrit if files modified in repository.
12 #
13 # The script requires to install the minimum version 1.25 of git-review using
14 # virtualenv and pip install which supports `--reviewers` option.
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 patch assumes the $WORKSPACE contains the project repo with
22 #       the files changed already "git add" and waiting for a "git commit" call.
23 #
24 # This script requires the following JJB variables to be passed in:
25 #
26 #   $PROJECT              : Gerrit project-name
27 #   $GERRIT_COMMIT_MESSAGE: Commit message to assign to commit
28 #   $GERRIT_HOST          : Gerrit hostname
29 #   $GERRIT_TOPIC         : Gerrit topic, please make a unique topic
30 #   $GERRIT_USER          : Gerrit user
31 #   $REVIEWERS_EMAIL      : Reviewers email
32
33 # TODO: remove the workaround when v1.26 is available on all images
34 # Workaround for git-review bug in v1.24
35 # https://storyboard.openstack.org/#!/story/2001081
36 set +u  # Allow unbound variables for virtualenv
37 virtualenv --quiet "/tmp/v/git-review"
38 # shellcheck source=/tmp/v/git-review/bin/activate disable=SC1091
39 source "/tmp/v/git-review/bin/activate"
40 pip install --quiet --upgrade "pip==9.0.3" setuptools
41 pip install --quiet --upgrade git-review
42 set -u
43 # End git-review workaround
44
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")"
53
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" | \
58                grep 'Change-Id:' | \
59                awk '{ print $2 }')
60
61 if [ -z "$CHANGE_ID" ]; then
62    git commit -sm "$GERRIT_COMMIT_MESSAGE"
63 else
64    git commit -sm "$GERRIT_COMMIT_MESSAGE" -m "Change-Id: $CHANGE_ID"
65 fi
66
67 git status
68 git remote add gerrit "ssh://$GERRIT_USER@$GERRIT_HOST:29418/$PROJECT.git"
69
70 # if the reviewers email is empty then use a default
71 REVIEWERS_EMAIL=${REVIEWERS_EMAIL:-"$GERRIT_USER@$GERRIT_HOST"}
72
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