From cfc34325599e8540be6de457b5f07ac9e08196b4 Mon Sep 17 00:00:00 2001 From: Anil Belur Date: Mon, 9 Apr 2018 19:22:15 +1000 Subject: [PATCH] Add generic push changes to gerrit builder This is a generic version of the script to push a change through a job to Gerrit in an automated fashion. Projects can reuse this macros when they have jobs which requires to push a change to gerrit. RELENG-769 Change-Id: I4d269a4dad6ddbd509a134306b61b2214194b479 Signed-off-by: Anil Belur --- jjb/lf-macros.yaml | 13 ++++++++ shell/gerrit-push-patch.sh | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 shell/gerrit-push-patch.sh diff --git a/jjb/lf-macros.yaml b/jjb/lf-macros.yaml index fb24a621..c7b7fb2f 100644 --- a/jjb/lf-macros.yaml +++ b/jjb/lf-macros.yaml @@ -86,6 +86,19 @@ - shell: !include-raw: - ../shell/packer-clear-credentials.sh +- builder: + name: lf-infra-push-gerrit-patch + builders: + - inject: + properties-content: | + PROJECT='{project}' + GERRIT_COMMIT_MESSAGE='{gerrit-commit-message}' + GERRIT_HOST='{gerrit-host}' + GERRIT_TOPIC='{gerrit-topic}' + GERRIT_USER='{gerrit-user}' + REVIEWERS_EMAIL='{reviewers-email}' + - shell: !include-raw-escape: ../shell/gerrit-push-patch.sh + - builder: name: lf-infra-sysstat builders: diff --git a/shell/gerrit-push-patch.sh b/shell/gerrit-push-patch.sh new file mode 100644 index 00000000..43762775 --- /dev/null +++ b/shell/gerrit-push-patch.sh @@ -0,0 +1,75 @@ +#!/bin/bash +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2018 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +# Push a change to Gerrit if files modified in repository. +# +# The script requires to install the minimum version 1.25 of git-review using +# virtualenv and pip install which supports `--reviewers` option. +# +# The script allows a job to push a patch to Gerrit in an automated fashion. +# This is meant for tasks that creates the same patch regularly and needs the +# ability to detect if an unreviewed patch already exists. In which case it +# will update the existing patch. +# +# Note: This patch assumes the $WORKSPACE contains the project repo with +# the files changed already "git add" and waiting for a "git commit" call. +# +# This script requires the following JJB variables to be passed in: +# +# $PROJECT : Gerrit project-name +# $GERRIT_COMMIT_MESSAGE: Commit message to assign to commit +# $GERRIT_HOST : Gerrit hostname +# $GERRIT_TOPIC : Gerrit topic, please make a unique topic +# $GERRIT_USER : Gerrit user +# $REVIEWERS_EMAIL : Reviewers email + +# TODO: remove the workaround when v1.26 is available on all images +# Workaround for git-review bug in v1.24 +# https://storyboard.openstack.org/#!/story/2001081 +set +u # Allow unbound variables for virtualenv +virtualenv --quiet "/tmp/v/git-review" +# shellcheck source=/tmp/v/git-review/bin/activate disable=SC1091 +source "/tmp/v/git-review/bin/activate" +pip install --quiet --upgrade "pip==9.0.3" setuptools +pip install --quiet --upgrade git-review +set -u +# End git-review workaround + +# Remove any leading or trailing quotes surrounding the strings +# which can cause parse errors when passed as CLI options to commands +PROJECT="$(echo $PROJECT | sed "s/^\([\"']\)\(.*\)\1\$/\2/g")" +GERRIT_COMMIT_MESSAGE="$(echo $GERRIT_COMMIT_MESSAGE | sed "s/^\([\"']\)\(.*\)\1\$/\2/g")" +GERRIT_HOST="$(echo $GERRIT_HOST | sed "s/^\([\"']\)\(.*\)\1\$/\2/g")" +GERRIT_TOPIC="$(echo $GERRIT_TOPIC | sed "s/^\([\"']\)\(.*\)\1\$/\2/g")" +GERRIT_USER="$(echo $GERRIT_USER | sed "s/^\([\"']\)\(.*\)\1\$/\2/g")" +REVIEWERS_EMAIL="$(echo $REVIEWERS_EMAIL | sed "s/^\([\"']\)\(.*\)\1\$/\2/g")" + +CHANGE_ID=$(ssh -p 29418 "$GERRIT_USER@$GERRIT_HOST" gerrit query \ + limit:1 owner:self is:open project:"$PROJECT" \ + message: "$GERRIT_COMMIT_MESSAGE" \ + topic: "$GERRIT_TOPIC" | \ + grep 'Change-Id:' | \ + awk '{ print $2 }') + +if [ -z "$CHANGE_ID" ]; then + git commit -sm "$GERRIT_COMMIT_MESSAGE" +else + git commit -sm "$GERRIT_COMMIT_MESSAGE" -m "Change-Id: $CHANGE_ID" +fi + +git status +git remote add gerrit "ssh://$GERRIT_USER@$GERRIT_HOST:29418/$PROJECT.git" + +# if the reviewers email is empty then use a default +REVIEWERS_EMAIL=${REVIEWERS_EMAIL:-"$GERRIT_USER@$GERRIT_HOST"} + +# Don't fail the build if this command fails because it's possible that there +# is no changes since last update. +git review --yes -t "$GERRIT_TOPIC" --reviewers "$REVIEWERS_EMAIL" || true -- 2.16.6