OPENDEV: Parse json using jq tool
[infra/cicd.git] / jjb / opendev / push-upstream-jobs.sh
index 02337646d468628ac2ae8243f96d8725030b06ea..0e1326f93126b8cb2345b53883319948bf4dda1a 100755 (executable)
@@ -46,20 +46,22 @@ opendev_project=$(echo "$GERRIT_PROJECT" | cut -d/ -f2- )
 # 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')
+    | tee /dev/stderr | tail -1 | jq -r '.[0].username')
 
 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"
+# 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
+    refspec="$commit_hash:refs/for/$GERRIT_BRANCH/$GERRIT_TOPIC"
+fi
 
 echo >&2 "Pushing to OpenDev"
 
@@ -76,4 +78,3 @@ push_result=${PIPESTATUS[0]}
 if [ $push_result -ne 0 ]; then
     grep '(no new changes)' push_result.txt
 fi
-