blob: f285230e7292cf012aed57cfa6c9408cf27b41cf [file] [log] [blame]
robert.tomczyka9b3ba62019-05-22 18:19:17 +01001#!/bin/bash
2
3# ============LICENSE_START=======================================================
4# Copyright (C) 2019 The Nordix Foundation. All rights reserved.
5# ================================================================================
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18# SPDX-License-Identifier: Apache-2.0
19# ============LICENSE_END=========================================================
20
21#
robert.tomczyk4926d1c2019-07-05 09:52:58 +010022# This script will take the single commit form the given Nordix Gerrit review
23# and push it, and all its ancestor commit till $GERRIT_BRANCH, to OpenDaylight
24# Gerrit.
robert.tomczyka9b3ba62019-05-22 18:19:17 +010025# Pre-requisites for script to run successfully:
afenner96103982019-08-09 09:18:42 +010026# - Author name in Nordix Gerrit equals LFID i.e. need to run "git config --global user.name <LFID>"
robert.tomczyk4926d1c2019-07-05 09:52:58 +010027# - infra public key on build server needs to be added to your users SSH
28# Public Keys in OpenDaylight Gerrit
robert.tomczyka9b3ba62019-05-22 18:19:17 +010029#
robert.tomczyk4926d1c2019-07-05 09:52:58 +010030set -euxo pipefail
robert.tomczyka9b3ba62019-05-22 18:19:17 +010031
robert.tomczyk4926d1c2019-07-05 09:52:58 +010032cd "$WORKSPACE"
robert.tomczyka9b3ba62019-05-22 18:19:17 +010033
robert.tomczyk4926d1c2019-07-05 09:52:58 +010034opendaylight_gerrit_base='git.opendaylight.org:29418'
35nordix_gerrit_rest='https://gerrit.nordix.org'
robert.tomczyka9b3ba62019-05-22 18:19:17 +010036
robert.tomczyk4926d1c2019-07-05 09:52:58 +010037echo >&2 "Collecting information about what and how to push towards OpenDaylight"
robert.tomczyka9b3ba62019-05-22 18:19:17 +010038
robert.tomczyk4926d1c2019-07-05 09:52:58 +010039# The git hash of the commit in the review this job runs on
40commit_hash="$GERRIT_PATCHSET_REVISION"
robert.tomczyka9b3ba62019-05-22 18:19:17 +010041
robert.tomczyk4926d1c2019-07-05 09:52:58 +010042# GERRIT_PROJECT is in the form of opendaylight/<opendaylight-project>
43opendaylight_project=$(echo "$GERRIT_PROJECT" | cut -d/ -f2- )
robert.tomczyka9b3ba62019-05-22 18:19:17 +010044
afenner96103982019-08-09 09:18:42 +010045# original way to get user name based on nordix id replaced by method below
robert.tomczyk4926d1c2019-07-05 09:52:58 +010046# Nordix Gerrit can map the email address of the user pushed the change to
47# the review to the Gerrit username of the account in Nordix Gerrit. This
48# should match with the Gerrit username in the OpenDaylight Gerrit.
afenner96103982019-08-09 09:18:42 +010049#username=$(
50# curl -s -H 'Accept: application/json' \
51# "$nordix_gerrit_rest/accounts/?q=email:$GERRIT_EVENT_ACCOUNT_EMAIL&o=DETAILS" \
52# | tee /dev/stderr | tail -1 | jq -r '.[0].username')
53
54# read user name from the commit details which allows nordix user name to be different to lfid
55username=$(git show -s --pretty=%an)
56
57pattern=" "
58if [[ "$username" =~ $pattern ]]
59then
60 echo "Incorrect username, use Linux Foundation ID as git user.name when pushing to Nordix"
61 echo "Use command git config --global user.name <LFID>"
62 exit 1
63fi
robert.tomczyk4926d1c2019-07-05 09:52:58 +010064
65opendaylight_remote_url="ssh://$username@$opendaylight_gerrit_base/$opendaylight_project"
66
67# GERRIT_BRANCH is the _intended_ branch of the commit under review
68# E.g. git push HEAD:refs/for/master => GERRIT_BRANCH='master'
69# GERRIT_TOPIC is the rest of the refspec after GERRIT_BRANCH when the topic
70# is set for the change.
71if [ -z "${GERRIT_TOPIC:-}" ] ; then
robert.tomczyk40f60152020-10-13 11:03:13 +010072 refspec="${commit_hash}:refs/for/${GERRIT_BRANCH}"
robert.tomczyka9b3ba62019-05-22 18:19:17 +010073else
robert.tomczyk40f60152020-10-13 11:03:13 +010074 refspec="${commit_hash}:refs/for/${GERRIT_BRANCH}%topic=${GERRIT_TOPIC}"
robert.tomczyka9b3ba62019-05-22 18:19:17 +010075fi
76
robert.tomczyk4926d1c2019-07-05 09:52:58 +010077echo >&2 "Pushing to OpenDaylight"
robert.tomczyka9b3ba62019-05-22 18:19:17 +010078
robert.tomczyk4926d1c2019-07-05 09:52:58 +010079# do not fail if git push fails as in case of no new changes we want to succeed. See below.
80set +o pipefail
81
82git push "$opendaylight_remote_url" "$refspec" 2>&1 | tee push_result.txt
83push_result=${PIPESTATUS[0]}
84
85# make the job a success if the above git push fails due to no new changes are needed
86# to be created upstream. This will be useful when we switch to automatic triggering
87# of the push-upstream for each patch set and jobs on different commits in a same chain
88# will race with each other to push parts of the chain
89if [ $push_result -ne 0 ]; then
90 grep '(no new changes)' push_result.txt
robert.tomczyka9b3ba62019-05-22 18:19:17 +010091fi