blob: 6557a18273971a325a4203e61f72e7498a87a208 [file] [log] [blame]
robert.tomczyke53f0092019-06-05 12:03:09 +01001#!/bin/bash
2
robert.tomczyka49a3a42019-09-04 15:43:19 +01003# ============LICENSE_START====================================================
robert.tomczyke53f0092019-06-05 12:03:09 +01004# Copyright (C) 2019 The Nordix Foundation. All rights reserved.
robert.tomczyka49a3a42019-09-04 15:43:19 +01005# =============================================================================
robert.tomczyke53f0092019-06-05 12:03:09 +01006# 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
robert.tomczyka49a3a42019-09-04 15:43:19 +010019# ============LICENSE_END======================================================
robert.tomczyke53f0092019-06-05 12:03:09 +010020#
robert.tomczyka49a3a42019-09-04 15:43:19 +010021# This script will take the single commit form the given Nordix Gerrit review
22# and push it, and all its ancestor commit till $GERRIT_BRANCH, to ONAP Gerrit.
robert.tomczyke53f0092019-06-05 12:03:09 +010023# Pre-requisites for script to run successfully:
robert.tomczyka49a3a42019-09-04 15:43:19 +010024# - Author name in Nordix Gerrit equals LFID
25# i.e. need to run "git config --global user.name <LFID>"
26# - infra public key on build server needs to be added to your users SSH
27# Public Keys in ONAP Gerrit
robert.tomczyke53f0092019-06-05 12:03:09 +010028#
robert.tomczyka49a3a42019-09-04 15:43:19 +010029set -euxo pipefail
robert.tomczyke53f0092019-06-05 12:03:09 +010030
robert.tomczyka49a3a42019-09-04 15:43:19 +010031cd "$WORKSPACE"
32
33onap_gerrit_base='gerrit.onap.org:29418'
34echo >&2 "Collecting information about what and how to push towards ONAP"
35
36# The git hash of the commit in the review this job runs on
37commit_hash="$GERRIT_PATCHSET_REVISION"
38
39# GERRIT_PROJECT is in the form of onap/<onap-project>
40onap_project=$(echo "$GERRIT_PROJECT" | cut -d/ -f2- )
41
42# Unfortunately for the some ONAP users, the Nordix Gerrit username don't match
43# the ONAP Gerrit username as the old script use the git author name as
44# the user which will push the change to ONAP Gerrit.
robert.tomczyke53f0092019-06-05 12:03:09 +010045username=$(git show -s --pretty=%an)
46
robert.tomczyka49a3a42019-09-04 15:43:19 +010047onap_remote_url="ssh://$username@$onap_gerrit_base/$onap_project"
robert.tomczyke53f0092019-06-05 12:03:09 +010048
robert.tomczyka49a3a42019-09-04 15:43:19 +010049# GERRIT_BRANCH is the _intended_ branch of the commit under review
50# E.g. git push HEAD:refs/for/master => GERRIT_BRANCH='master'
51# GERRIT_TOPIC is the rest of the refspec after GERRIT_BRANCH when the topic
52# is set for the change.
53if [ -z "${GERRIT_TOPIC:-}" ] ; then
54 refspec="$commit_hash:refs/for/$GERRIT_BRANCH"
robert.tomczyke53f0092019-06-05 12:03:09 +010055else
robert.tomczyka49a3a42019-09-04 15:43:19 +010056 refspec="$commit_hash:refs/for/$GERRIT_BRANCH/$GERRIT_TOPIC"
robert.tomczyke53f0092019-06-05 12:03:09 +010057fi
58
robert.tomczyka49a3a42019-09-04 15:43:19 +010059echo >&2 "Pushing to ONAP"
robert.tomczyke53f0092019-06-05 12:03:09 +010060
robert.tomczyka49a3a42019-09-04 15:43:19 +010061# do not fail if git push fails as in case of no new changes we want to succeed. See below.
62set +o pipefail
63
64git push "$onap_remote_url" "$refspec" 2>&1 | tee push_result.txt
65push_result=${PIPESTATUS[0]}
66
67# make the job a success if the above git push fails due to no new changes are needed
68# to be created upstream. This will be useful when we switch to automatic triggering
69# of the push-upstream for each patch set and jobs on different commits in a same chain
70# will race with each other to push parts of the chain
71if [ $push_result -ne 0 ]; then
72 grep '(no new changes)' push_result.txt
robert.tomczyke53f0092019-06-05 12:03:09 +010073fi