X-Git-Url: https://gerrit.nordix.org/gitweb?a=blobdiff_plain;f=jjb%2Fonap%2Fpush-upstream-jobs.sh;h=ce106611e228e934d52a8c21333d541bfecd6a41;hb=ab49178bfd1199c59d929ebf396e5d87f9a315f6;hp=7f00fa310e87a573e4cbff056c2189063c5d38ab;hpb=e53f009e429bb4c792a1920f2c5ff71742f52250;p=infra%2Fcicd.git diff --git a/jjb/onap/push-upstream-jobs.sh b/jjb/onap/push-upstream-jobs.sh index 7f00fa31..ce106611 100755 --- a/jjb/onap/push-upstream-jobs.sh +++ b/jjb/onap/push-upstream-jobs.sh @@ -1,8 +1,8 @@ #!/bin/bash -# ============LICENSE_START======================================================= +# ============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 @@ -16,61 +16,78 @@ # limitations under the License. # # SPDX-License-Identifier: Apache-2.0 -# ============LICENSE_END========================================================= - +# ============LICENSE_END====================================================== # -# This script will take the changes that are pushed to Nordix Gerrit and push upstream to ONAP +# 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 ONAP 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 " -# - infra public key on build server needs to be added to your users SSH Public Keys in ONAP Gerrit +# - Author name in Nordix Gerrit equals LFID +# i.e. need to run "git config --global user.name " +# - infra public key on build server needs to be added to your users SSH +# Public Keys in ONAP Gerrit # -set -o nounset -set -o pipefail +set -euxo pipefail -cd $WORKSPACE -echo "Retrieving information from commit to push towards ONAP" -project=$(git config --local remote.origin.url | awk -F "29418/onap/" '{print $2}') -message=$(git show -s --pretty=%B | grep -vi 'Signed-off-by') -username=$(git show -s --pretty=%an) +cd "$WORKSPACE" -pattern=" " -if [[ "$username" =~ $pattern ]] -then - echo "Incorrect username, use Linux Foundation ID as git user.name when pushing to Nordix" - exit 1 -fi +onap_gerrit_base='gerrit.onap.org:29418' +nordix_gerrit_rest='https://gerrit.nordix.org' +echo >&2 "Collecting information about what and how to push towards ONAP" -echo "Setting user name and email" -git config user.email $(git show -s --pretty=%ae) -git config user.name $username +# 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 onap/ +onap_project=$(echo "$GERRIT_PROJECT" | cut -d/ -f2- ) + +# Unfortunately for the some ONAP users, the Nordix Gerrit username don't match +# the ONAP Gerrit username as the old script use the git author name as +# the user which will push the change to ONAP Gerrit. +username=$(git show -s --pretty=%an) -echo "Checking out branch on master with new changes" -git reset HEAD~1 --soft -git checkout -b delivery_branch origin/$BRANCH +onap_remote_url="ssh://$username@$onap_gerrit_base/$onap_project" -git config --get remote.upstream.url -retVal=$? +# GERRIT_BRANCH is the _intended_ branch of the commit under review +# E.g. git push HEAD:refs/for/master => GERRIT_BRANCH='master' +refspec="${commit_hash}:refs/for/${GERRIT_BRANCH}" -if [[ $retVal -eq 0 ]] -then - git remote rm upstream - git remote add upstream "ssh://$username@gerrit.onap.org:29418/$project.git" -else - git remote add upstream "ssh://$username@gerrit.onap.org:29418/$project.git" +# When 'push-upstream wip' is specified the Work-In-Progress flag is set to change on push to upstream +if [[ "${GERRIT_EVENT_TYPE:-}" == "comment-added" && "${GERRIT_EVENT_COMMENT_TEXT:-}" =~ "push-upstream wip" ]] ; then + wip_option="--push-option wip" +# When 'push-upstream ready' is specified the Work-In-Progress flag from a change is removed on push to upstream +elif [[ "${GERRIT_EVENT_TYPE:-}" == "comment-added" && "${GERRIT_EVENT_COMMENT_TEXT:-}" =~ "push-upstream ready" ]] ; then + wip_option="--push-option ready" fi -echo "Committing changes and pushing upstream" -git commit -as -m "$message" -git push upstream HEAD:refs/for/$BRANCH +# When the topic is set for a change the GERRIT_TOPIC is push upstream using git option +if [ -n "${GERRIT_TOPIC:-}" ] ; then + topic_option="--push-option topic=${GERRIT_TOPIC}" +fi + +echo >&2 "Pushing to ONAP" + +# do not fail if git push fails as in case of no new changes we want to succeed. See below. +set +o pipefail -retVal1=$? -if [[ $retVal1 -eq 0 ]] -then - echo "Push upstream to ONAP succeeded" -else - echo "Push upstream to ONAP failed" - exit 2 +git push ${topic_option:-} ${wip_option:-} "$onap_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 -git checkout $BRANCH -git branch -D delivery_branch +# When a push-upstream job completes successfully. +# The link to the corresponding upstream gerrit review is posted +# on the Nordix gerrit using Gerrit API and infra user HTTP API Key. +if grep -q http push_result.txt; then + URL=$(sed -ne 's/.*\(http\S*\).*/\1/p' < push_result.txt) + echo "URL: $URL" + curl --request 'POST' -H 'Content-Type:application/json' \ + "${nordix_gerrit_rest}/a/changes/${GERRIT_CHANGE_NUMBER}/revisions/${GERRIT_PATCHSET_NUMBER}/review" \ + --user "${GERRIT_USER}:${GERRIT_API_KEY}" \ + --data "{\"message\": \"UPSTREAM URL: ${URL}\"}" +fi