Tag Gerrit-CI comments on review

By tagging all the comments to changes given by Gerrit-CI we
allow a cleaner view of the change feedback, because all the tagged
comments can be filtered out with one click.

Change-Id: Ia72f44518dfa8ff162f7a0af7a650b46bf54b38e
diff --git a/jenkins/gerrit-verifier-change.groovy b/jenkins/gerrit-verifier-change.groovy
index b769aea..772d412 100644
--- a/jenkins/gerrit-verifier-change.groovy
+++ b/jenkins/gerrit-verifier-change.groovy
@@ -32,29 +32,50 @@
   static int myAccountId = 1022687
   static int waitForResultTimeout = 10000
   static Map buildsList = [:]
+
+  static def ciTag(String operation) {
+    " \"tag\" : \"autogenerated:gerrit-ci:$operation\" "
+  }
+
+  static String addReviewerTag = ciTag("addReviewer")
+  static String addVerifiedTag = ciTag("addVerified")
+  static String addCommentTag = ciTag("comment")
 }
 
 
 def gerritPost(url, jsonPayload) {
+  def error = ""
   def gerritPostUrl = Globals.gerrit + url
-  def curl = ['curl', '-n', '-s', '-S',
-    "-X", "POST", "-H", "Content-Type: application/json",
-    "--data-binary", jsonPayload,
+  def curl = ['curl', '--silent',
+  '--output', '/dev/stderr',
+  '--write-out', '%{http_code}',
+  '-n', '-s', '-S',
+  '-X', 'POST', '-H', 'Content-Type: application/json',
+  '--data-binary', jsonPayload,
     gerritPostUrl ]
+  println "CURL/EXEC> $curl"
   def proc = curl.execute()
   def sout = new StringBuffer(), serr = new StringBuffer()
   proc.consumeProcessOutput(sout, serr)
   proc.waitForOrKill(Globals.curlTimeout)
   def curlExit = proc.exitValue()
   if(curlExit != 0) {
-    println "$curl ** FAILED ** with exit code $curlExit"
+    error = "$curl **FAILED** with exit code = $curlExit"
+    println error
+    throw new IOException(error)
   }
-  if(!serr.toString().trim().isEmpty()) {
-    println "--- ERROR ---"
-    println serr    
+  def httpStatus = sout.toString().trim()
+  if(httpStatus != "200") {
+    error = "$curl **FAILED** with HTTP STATUS = $httpStatus"
+    println error
+    throw new IOException(error)
   }
 
-  return curlExit
+  if(!serr.toString().trim().isEmpty()) {
+    println "CURL/OUTPUT> $serr"
+  }
+
+  return 0
 }
 
 def gerritReview(buildUrl,changeNum, sha1, verified, msgPrefix) {
@@ -63,7 +84,7 @@
   }
 
   def addReviewerExit = gerritPost("a/changes/" + changeNum + "/reviewers", '{ "reviewer" : "' +
-                                   Globals.gerritReviewer + '" }')
+                                   Globals.gerritReviewer + "\" , ${Globals.addReviewerTag} }")
   if(addReviewerExit != 0) {
     println "**** ERROR: cannot add myself as reviewer of change " + changeNum + " *****"
     return addReviewerExit
@@ -71,7 +92,7 @@
 
   def jsonPayload = '{"labels":{"Code-Review":0,"Verified":' + verified + '},' +
                     ' "message": "' + msgPrefix + 'Gerrit-CI Build: ' + buildUrl + '", ' +
-                    ' "notify" : "' + (verified < 0 ? "OWNER": "OWNER_REVIEWERS") + '" }'
+                    ' "notify" : "' + (verified < 0 ? "OWNER": "OWNER_REVIEWERS") + "\" , ${Globals.addVerifiedTag} }"
   def addVerifiedExit = gerritPost("a/changes/" + changeNum + "/revisions/" + sha1 + "/review",
                                    jsonPayload)
 
@@ -85,7 +106,7 @@
 
 def gerritComment(buildUrl,changeNum, sha1, msgPrefix) {
   return gerritPost("a/changes/$changeNum/revisions/$sha1/review",
-                    "{\"message\": \"$msgPrefix Gerrit-CI Flow: $buildUrl\", \"notify\" : \"NONE\" }")
+                    "{ \"message\": \"$msgPrefix Gerrit-CI Flow: $buildUrl\", \"notify\" : \"NONE\", ${Globals.addCommentTag} }")
 }
 
 def waitForResult(b) {