ORANSC: Introduction ORANSC upstream to Nordix
[infra/cicd.git] / jjb / oransc / push-upstream-jobs.sh
diff --git a/jjb/oransc/push-upstream-jobs.sh b/jjb/oransc/push-upstream-jobs.sh
new file mode 100755 (executable)
index 0000000..03f412f
--- /dev/null
@@ -0,0 +1,73 @@
+#!/bin/bash
+
+# ============LICENSE_START====================================================
+#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
+# =============================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END======================================================
+#
+# This script will take the single commit form the given Nordix Gerrit review
+# and push it, and all its ancestor commit till $GERRIT_BRANCH, to ORANSC Gerrit.
+# Pre-requisites for script to run successfully:
+# - Author name in Nordix Gerrit equals LFID
+#   i.e. need to run "git config --global user.name <LFID>"
+# - infra public key on build server needs to be added to your users SSH
+#   Public Keys in ORANSC Gerrit
+#
+set -euxo pipefail
+
+cd "$WORKSPACE"
+
+oransc_gerrit_base='gerrit.o-ran-sc.org:29418'
+echo >&2 "Collecting information about what and how to push towards ORANSC Gerrit"
+
+# The git hash of the commit in the review this job runs on
+commit_hash="$GERRIT_PATCHSET_REVISION"
+
+# GERRIT_PROJECT is in the form of oransc/<oransc-project>
+oransc_project=$(echo "$GERRIT_PROJECT" | cut -d/ -f2- )
+
+# Unfortunately for the some of the Nordix users, the Nordix Gerrit username don't match
+# the ORANSC Gerrit username for their LFID. The git author will be used
+# to push the change to ORANSC Gerrit.
+username=$(git show -s --pretty=%an)
+
+oransc_remote_url="ssh://$username@$oransc_gerrit_base/$oransc_project"
+
+# GERRIT_BRANCH is the _intended_ branch of the commit under review
+# E.g. git push HEAD:refs/for/master => GERRIT_BRANCH='master'
+# GERRIT_TOPIC is the rest of the refspec after GERRIT_BRANCH when the topic
+# is set for the change.
+if [ -z "${GERRIT_TOPIC:-}" ] ; then
+    refspec="$commit_hash:refs/for/$GERRIT_BRANCH"
+else
+    refspec="$commit_hash:refs/for/$GERRIT_BRANCH/$GERRIT_TOPIC"
+fi
+
+echo >&2 "Pushing to ORANSC Gerrit"
+
+# do not fail if git push fails as in case of no new changes we want to succeed. See below.
+set +o pipefail
+
+git push "$oransc_remote_url" "$refspec" 2>&1 | tee push_result.txt
+push_result=${PIPESTATUS[0]}
+
+# make the job a success if the above git push fails due to no new changes are needed
+# to be created upstream. This will be useful when we switch to automatic triggering
+# of the push-upstream for each patch set and jobs on different commits in a same chain
+# will race with each other to push parts of the chain
+if [ $push_result -ne 0 ]; then
+    grep '(no new changes)' push_result.txt
+fi