robert.tomczyk | e53f009 | 2019-06-05 12:03:09 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
robert.tomczyk | a49a3a4 | 2019-09-04 15:43:19 +0100 | [diff] [blame] | 3 | # ============LICENSE_START==================================================== |
robert.tomczyk | e53f009 | 2019-06-05 12:03:09 +0100 | [diff] [blame] | 4 | # Copyright (C) 2019 The Nordix Foundation. All rights reserved. |
robert.tomczyk | a49a3a4 | 2019-09-04 15:43:19 +0100 | [diff] [blame] | 5 | # ============================================================================= |
robert.tomczyk | e53f009 | 2019-06-05 12:03:09 +0100 | [diff] [blame] | 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 |
robert.tomczyk | a49a3a4 | 2019-09-04 15:43:19 +0100 | [diff] [blame] | 19 | # ============LICENSE_END====================================================== |
robert.tomczyk | e53f009 | 2019-06-05 12:03:09 +0100 | [diff] [blame] | 20 | # |
robert.tomczyk | a49a3a4 | 2019-09-04 15:43:19 +0100 | [diff] [blame] | 21 | # 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.tomczyk | e53f009 | 2019-06-05 12:03:09 +0100 | [diff] [blame] | 23 | # Pre-requisites for script to run successfully: |
robert.tomczyk | a49a3a4 | 2019-09-04 15:43:19 +0100 | [diff] [blame] | 24 | # - 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.tomczyk | e53f009 | 2019-06-05 12:03:09 +0100 | [diff] [blame] | 28 | # |
robert.tomczyk | a49a3a4 | 2019-09-04 15:43:19 +0100 | [diff] [blame] | 29 | set -euxo pipefail |
robert.tomczyk | e53f009 | 2019-06-05 12:03:09 +0100 | [diff] [blame] | 30 | |
robert.tomczyk | a49a3a4 | 2019-09-04 15:43:19 +0100 | [diff] [blame] | 31 | cd "$WORKSPACE" |
| 32 | |
| 33 | onap_gerrit_base='gerrit.onap.org:29418' |
robert.tomczyk | a22a930 | 2021-03-31 16:46:13 +0100 | [diff] [blame] | 34 | nordix_gerrit_rest='https://gerrit.nordix.org' |
robert.tomczyk | a49a3a4 | 2019-09-04 15:43:19 +0100 | [diff] [blame] | 35 | echo >&2 "Collecting information about what and how to push towards ONAP" |
| 36 | |
| 37 | # The git hash of the commit in the review this job runs on |
| 38 | commit_hash="$GERRIT_PATCHSET_REVISION" |
| 39 | |
| 40 | # GERRIT_PROJECT is in the form of onap/<onap-project> |
| 41 | onap_project=$(echo "$GERRIT_PROJECT" | cut -d/ -f2- ) |
| 42 | |
| 43 | # Unfortunately for the some ONAP users, the Nordix Gerrit username don't match |
| 44 | # the ONAP Gerrit username as the old script use the git author name as |
| 45 | # the user which will push the change to ONAP Gerrit. |
robert.tomczyk | e53f009 | 2019-06-05 12:03:09 +0100 | [diff] [blame] | 46 | username=$(git show -s --pretty=%an) |
| 47 | |
robert.tomczyk | a49a3a4 | 2019-09-04 15:43:19 +0100 | [diff] [blame] | 48 | onap_remote_url="ssh://$username@$onap_gerrit_base/$onap_project" |
robert.tomczyk | e53f009 | 2019-06-05 12:03:09 +0100 | [diff] [blame] | 49 | |
robert.tomczyk | a49a3a4 | 2019-09-04 15:43:19 +0100 | [diff] [blame] | 50 | # GERRIT_BRANCH is the _intended_ branch of the commit under review |
| 51 | # E.g. git push HEAD:refs/for/master => GERRIT_BRANCH='master' |
robert.tomczyk | 90de09b | 2021-04-13 18:35:57 +0100 | [diff] [blame] | 52 | refspec="${commit_hash}:refs/for/${GERRIT_BRANCH}" |
| 53 | |
| 54 | # When 'push-upstream wip' is specified the Work-In-Progress flag is set to change on push to upstream |
| 55 | if [[ "${GERRIT_EVENT_TYPE:-}" == "comment-added" && "${GERRIT_EVENT_COMMENT_TEXT:-}" =~ "push-upstream wip" ]] ; then |
| 56 | wip_option="--push-option wip" |
| 57 | # When 'push-upstream ready' is specified the Work-In-Progress flag from a change is removed on push to upstream |
| 58 | elif [[ "${GERRIT_EVENT_TYPE:-}" == "comment-added" && "${GERRIT_EVENT_COMMENT_TEXT:-}" =~ "push-upstream ready" ]] ; then |
| 59 | wip_option="--push-option ready" |
| 60 | fi |
| 61 | |
| 62 | # When the topic is set for a change the GERRIT_TOPIC is push upstream using git option |
| 63 | if [ -n "${GERRIT_TOPIC:-}" ] ; then |
| 64 | topic_option="--push-option topic=${GERRIT_TOPIC}" |
robert.tomczyk | e53f009 | 2019-06-05 12:03:09 +0100 | [diff] [blame] | 65 | fi |
| 66 | |
robert.tomczyk | a49a3a4 | 2019-09-04 15:43:19 +0100 | [diff] [blame] | 67 | echo >&2 "Pushing to ONAP" |
robert.tomczyk | e53f009 | 2019-06-05 12:03:09 +0100 | [diff] [blame] | 68 | |
robert.tomczyk | a49a3a4 | 2019-09-04 15:43:19 +0100 | [diff] [blame] | 69 | # do not fail if git push fails as in case of no new changes we want to succeed. See below. |
| 70 | set +o pipefail |
| 71 | |
robert.tomczyk | 90de09b | 2021-04-13 18:35:57 +0100 | [diff] [blame] | 72 | git push ${topic_option:-} ${wip_option:-} "$onap_remote_url" "$refspec" 2>&1 | tee push_result.txt |
robert.tomczyk | a49a3a4 | 2019-09-04 15:43:19 +0100 | [diff] [blame] | 73 | push_result=${PIPESTATUS[0]} |
| 74 | |
| 75 | # make the job a success if the above git push fails due to no new changes are needed |
| 76 | # to be created upstream. This will be useful when we switch to automatic triggering |
| 77 | # of the push-upstream for each patch set and jobs on different commits in a same chain |
| 78 | # will race with each other to push parts of the chain |
| 79 | if [ $push_result -ne 0 ]; then |
| 80 | grep '(no new changes)' push_result.txt |
robert.tomczyk | e53f009 | 2019-06-05 12:03:09 +0100 | [diff] [blame] | 81 | fi |
robert.tomczyk | a22a930 | 2021-03-31 16:46:13 +0100 | [diff] [blame] | 82 | |
| 83 | # When a push-upstream job completes successfully. |
| 84 | # The link to the corresponding upstream gerrit review is posted |
| 85 | # on the Nordix gerrit using Gerrit API and infra user HTTP API Key. |
| 86 | if grep -q http push_result.txt; then |
| 87 | URL=$(sed -ne 's/.*\(http\S*\).*/\1/p' < push_result.txt) |
| 88 | echo "URL: $URL" |
| 89 | curl --request 'POST' -H 'Content-Type:application/json' \ |
| 90 | "${nordix_gerrit_rest}/a/changes/${GERRIT_CHANGE_NUMBER}/revisions/${GERRIT_PATCHSET_NUMBER}/review" \ |
| 91 | --user "${GERRIT_USER}:${GERRIT_API_KEY}" \ |
robert.tomczyk | 9bb9a13 | 2021-04-12 13:19:32 +0100 | [diff] [blame] | 92 | --data "{\"message\": \"UPSTREAM URL: ${URL}\"}" |
robert.tomczyk | a22a930 | 2021-03-31 16:46:13 +0100 | [diff] [blame] | 93 | fi |