blob: 02337646d468628ac2ae8243f96d8725030b06ea [file] [log] [blame]
robert.tomczyk29c6ced2019-05-28 11:43:02 +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#
Balazs Gibizerc90ddaf2019-05-30 10:58:51 +020022# 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 OpenDev
24# Gerrit.
robert.tomczyk29c6ced2019-05-28 11:43:02 +010025# Pre-requisites for script to run successfully:
Balazs Gibizerc90ddaf2019-05-30 10:58:51 +020026# - The Gerrit username in Nordix needs to match with the Gerrit username in
27# OpenDev
28# - infra public key on build server needs to be added to your users SSH
29# Public Keys in OpenDev Gerrit
robert.tomczyk29c6ced2019-05-28 11:43:02 +010030#
Balazs Gibizerc90ddaf2019-05-30 10:58:51 +020031set -euxo pipefail
robert.tomczyk29c6ced2019-05-28 11:43:02 +010032
Balazs Gibizerc90ddaf2019-05-30 10:58:51 +020033cd "$WORKSPACE"
robert.tomczyk29c6ced2019-05-28 11:43:02 +010034
Balazs Gibizerc90ddaf2019-05-30 10:58:51 +020035opendev_gerrit_base='review.opendev.org:29418'
36nordix_gerrit_rest='https://gerrit.nordix.org'
37
38echo >&2 "Collecting information about what and how to push towards OpenDev"
39
40# The git hash of the commit in the review this job runs on
41commit_hash="$GERRIT_PATCHSET_REVISION"
42
43# GERRIT_PROJECT is in the form of opendev/<opendev-project>
44opendev_project=$(echo "$GERRIT_PROJECT" | cut -d/ -f2- )
45
46# Nordix Gerrit can map the email address of the user pushed the change to
47# the review to the Gerrit username of the accound in Nordix Gerrit. This
48# should match with the Gerrit username in the OpenDev Gerrit.
49# TODO(gibi): parse the json propely if there are tools (e.g. jq) available on
50# the jenkins slave
51username=$(
52 curl -s -H 'Accept: application/json' \
53 "$nordix_gerrit_rest/accounts/?q=email:$GERRIT_EVENT_ACCOUNT_EMAIL&o=DETAILS" \
54 | tee /dev/stderr \
55 | sed -n 's/^.*"username":"\(\S*\)".*$/\1/p')
56
57opendev_remote_url="ssh://$username@$opendev_gerrit_base/$opendev_project"
58
59# GERRIT_BRANCH is the _intended_ branch of the commit under review
60# E.g. git push HEAD:refs/for/master => GERRIT_BRANCH='master'
61# GERRIT_TOPIC is the rest of the refspec after GERRIT_BRANCH
62refspec="$commit_hash:refs/for/$GERRIT_BRANCH/$GERRIT_TOPIC"
63
64echo >&2 "Pushing to OpenDev"
65
66# do not fail if git push fails as in case of no new changes we want to succeed. See below.
67set +o pipefail
68
69git push "$opendev_remote_url" "$refspec" 2>&1 | tee push_result.txt
70push_result=${PIPESTATUS[0]}
71
72# make the job a success if the above git push fails due to no new changes are needed
73# to be created upstream. This will be useful when we switch to automatic triggering
74# of the push-upstream for each patch set and jobs on different commits in a same chain
75# will race with each other to push parts of the chain
76if [ $push_result -ne 0 ]; then
77 grep '(no new changes)' push_result.txt
robert.tomczyk29c6ced2019-05-28 11:43:02 +010078fi
79