From: Balazs Gibizer Date: Thu, 30 May 2019 08:58:51 +0000 (+0200) Subject: Simplify OpenDev push-upstream job X-Git-Url: https://gerrit.nordix.org/gitweb?a=commitdiff_plain;h=c90ddaf974baf3f42849b74e869cfa22c775df33;p=infra%2Fcicd.git Simplify OpenDev push-upstream job This patch adjust the OpenDev push-upstream jenkins job to follow the following rules: * do not change the commit that is being upstreamed * do not force the git user.name to be the gerrit id. Git authorship can be independent from gerrit review ownership. Query Nordix Gerrit about the user name instead. * consider 'no new changes' error from OpenDev Gerrit as success as it means the current patch is up-to-date upstream Change-Id: Ib7a0d9d6052db357148d07f57b141730b837055f --- diff --git a/jjb/opendev/push-upstream-jobs.sh b/jjb/opendev/push-upstream-jobs.sh index 148b2b093..02337646d 100755 --- a/jjb/opendev/push-upstream-jobs.sh +++ b/jjb/opendev/push-upstream-jobs.sh @@ -19,59 +19,61 @@ # ============LICENSE_END========================================================= # -# This script will take the changes that are pushed to Nordix Gerrit and push upstream to OpenDev +# 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 OpenDev +# Gerrit. # Pre-requisites for script to run successfully: -# - Author name in Nordix Gerrit have to be the same as OpenDev username -# 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 OpenDev Gerrit +# - The Gerrit username in Nordix needs to match with the Gerrit username in +# OpenDev +# - infra public key on build server needs to be added to your users SSH +# Public Keys in OpenDev Gerrit # -set -o nounset -set -o pipefail +set -euxo pipefail -cd $WORKSPACE -echo "Retrieving information from commit to push towards OpenDev" -project=$(git config --local remote.origin.url | awk -F "29418/opendev/" '{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 OpenDev username as git user.name when pushing to Nordix" - exit 1 -fi +opendev_gerrit_base='review.opendev.org:29418' +nordix_gerrit_rest='https://gerrit.nordix.org' -echo "Setting user name and email" -git config user.email $(git show -s --pretty=%ae) -git config user.name $username +echo >&2 "Collecting information about what and how to push towards OpenDev" -echo "Checking out branch on master with new changes" -git reset HEAD~1 --soft -git checkout -b delivery_branch origin/$BRANCH +# The git hash of the commit in the review this job runs on +commit_hash="$GERRIT_PATCHSET_REVISION" -git config --get remote.upstream.url -retVal=$? +# GERRIT_PROJECT is in the form of opendev/ +opendev_project=$(echo "$GERRIT_PROJECT" | cut -d/ -f2- ) -if [[ $retVal -eq 0 ]] -then - git remote rm upstream - git remote add upstream "ssh://$username@review.opendev.org:29418/$project.git" -else - git remote add upstream "ssh://$username@review.opendev.org:29418/$project.git" -fi +# Nordix Gerrit can map the email address of the user pushed the change to +# the review to the Gerrit username of the accound in Nordix Gerrit. This +# should match with the Gerrit username in the OpenDev Gerrit. +# TODO(gibi): parse the json propely if there are tools (e.g. jq) available on +# the jenkins slave +username=$( + curl -s -H 'Accept: application/json' \ + "$nordix_gerrit_rest/accounts/?q=email:$GERRIT_EVENT_ACCOUNT_EMAIL&o=DETAILS" \ + | tee /dev/stderr \ + | sed -n 's/^.*"username":"\(\S*\)".*$/\1/p') + +opendev_remote_url="ssh://$username@$opendev_gerrit_base/$opendev_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 +refspec="$commit_hash:refs/for/$GERRIT_BRANCH/$GERRIT_TOPIC" + +echo >&2 "Pushing to OpenDev" + +# do not fail if git push fails as in case of no new changes we want to succeed. See below. +set +o pipefail -echo "Committing changes and pushing upstream" -git commit -as -m "$message" -git push upstream HEAD:refs/for/$BRANCH +git push "$opendev_remote_url" "$refspec" 2>&1 | tee push_result.txt +push_result=${PIPESTATUS[0]} -retVal1=$? -if [[ $retVal1 -eq 0 ]] -then - echo "Push upstream to OpenDev succeeded" -else - echo "Push upstream to OpenDev failed" - exit 2 +# 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