30853ccb70fa7d8fb475fcd1aa67454f07db95a7
[infra/cicd.git] / jjb / onap / push-upstream-jobs.sh
1 #!/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 # 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.
23 # Pre-requisites for script to run successfully:
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
28 #
29 set -euxo pipefail
30
31 cd "$WORKSPACE"
32
33 onap_gerrit_base='gerrit.onap.org:29418'
34 nordix_gerrit_rest='https://gerrit.nordix.org'
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.
46 username=$(git show -s --pretty=%an)
47
48 onap_remote_url="ssh://$username@$onap_gerrit_base/$onap_project"
49
50 # GERRIT_BRANCH is the _intended_ branch of the commit under review
51 # E.g. git push HEAD:refs/for/master => GERRIT_BRANCH='master'
52 # GERRIT_TOPIC is the rest of the refspec after GERRIT_BRANCH when the topic
53 # is set for the change.
54 if [ -z "${GERRIT_TOPIC:-}" ] ; then
55     refspec="${commit_hash}:refs/for/${GERRIT_BRANCH}"
56 else
57     refspec="${commit_hash}:refs/for/${GERRIT_BRANCH}%topic=${GERRIT_TOPIC}"
58 fi
59
60 echo >&2 "Pushing to ONAP"
61
62 # do not fail if git push fails as in case of no new changes we want to succeed. See below.
63 set +o pipefail
64
65 git push "$onap_remote_url" "$refspec" 2>&1 | tee push_result.txt
66 push_result=${PIPESTATUS[0]}
67
68 # make the job a success if the above git push fails due to no new changes are needed
69 # to be created upstream. This will be useful when we switch to automatic triggering
70 # of the push-upstream for each patch set and jobs on different commits in a same chain
71 # will race with each other to push parts of the chain
72 if [ $push_result -ne 0 ]; then
73     grep '(no new changes)' push_result.txt
74 fi
75
76 # When a push-upstream job completes successfully.
77 # The link to the corresponding upstream gerrit review is posted
78 # on the Nordix gerrit using Gerrit API and infra user HTTP API Key.
79 if grep -q http push_result.txt; then
80     URL=$(sed -ne 's/.*\(http\S*\).*/\1/p' < push_result.txt)
81     echo "URL: $URL"
82     curl --request 'POST' -H 'Content-Type:application/json' \
83      "${nordix_gerrit_rest}/a/changes/${GERRIT_CHANGE_NUMBER}/revisions/${GERRIT_PATCHSET_NUMBER}/review" \
84      --user "${GERRIT_USER}:${GERRIT_API_KEY}" \
85      --data "{\"message\": \"UPSTREAM URL: ${URL}\"}"
86 fi