ACUMOS: Improve jenkins jobs for Acumos projects

- Mirror branches from Acumos upstream project to Nordix.
- Do not force the git user.name to be the gerrit id.
  Git authorship can be independent from the
  gerrit review ownership now.
- Cache the repository on the Jenkins slave nodes
  when rebasing repositories.
- Make sure the commit hash in the local repo is the same
  as in nordix Gerrit and the same as the commit hash
  in the upstream gerrit.
- Decrease number of jobs required for project in Jenkins

Change-Id: I29ea8b9b2573b5d894246f47b537aeb61027f6f8
diff --git a/jjb/acumos/push-upstream-jobs.sh b/jjb/acumos/push-upstream-jobs.sh
index 1eded15..9354bfb 100755
--- a/jjb/acumos/push-upstream-jobs.sh
+++ b/jjb/acumos/push-upstream-jobs.sh
@@ -19,58 +19,62 @@
 # ============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 Acumos
+# 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 ONAP Gerrit
+# - The Gerrit username in Nordix needs to match with the Gerrit username in
+#   Acumos
+# - infra public key on build server needs to be added to your users SSH
+#   Public Keys in Acumos Gerrit
 #
-set -o nounset
-set -o pipefail
+set -euxo pipefail
 
-cd $WORKSPACE
-echo "Retrieving information from commit to push towards Acumos"
-project=$(git config --local remote.origin.url | awk -F "29418/acumos/" '{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
+acumos_gerrit_base='gerrit.acumos.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 Acumos"
 
-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 acumos/<acumos-project>
+acumos_project=$(echo "$GERRIT_PROJECT" | cut -d/ -f2- )
 
-if [[ $retVal -eq 0 ]]
-then
-  git remote rm upstream
-  git remote add upstream "ssh://$username@gerrit.acumos.org:29418/$project.git"
+# Nordix Gerrit can map the email address of the user pushed the change to
+# the review to the Gerrit username of the account in Nordix Gerrit. This
+# should match with the Gerrit username in the Acumos Gerrit.
+username=$(
+    curl -s -H 'Accept: application/json' \
+        "$nordix_gerrit_rest/accounts/?q=email:$GERRIT_EVENT_ACCOUNT_EMAIL&o=DETAILS" \
+    | tee /dev/stderr | tail -1 | jq -r '.[0].username')
+
+acumos_remote_url="ssh://$username@$acumos_gerrit_base/$acumos_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
-  git remote add upstream "ssh://$username@gerrit.acumos.org:29418/$project.git"
+    refspec="$commit_hash:refs/for/$GERRIT_BRANCH/$GERRIT_TOPIC"
 fi
 
-echo "Committing changes and pushing upstream"
-git commit -as -m "$message"
-git push upstream HEAD:refs/for/$BRANCH
+echo >&2 "Pushing to Acumos"
 
-retVal1=$?
-if [[ $retVal1 -eq 0 ]]
-then
-  echo "Push upstream to Acumos succeeded"
-else
-  echo "Push upstream to Acumos failed"
-  exit 2
+# 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 "$acumos_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