Add voting facility on checkers to CI pipeline

Add voting facility on checkers:

* codestyle
* notedb
* reviewdb
* polygerrit

to CI pipeline. Right now the voting on checkers happens in addition to
the Verify and Code-Style labels. In follow up changes, the voting on
the labels will be removed (and Verified label will be removed as well).

This change is also adding checker definitions that will replace
Verified label on gerrit-review site. For now those checkers are not
blocking, but this will be changed in the future.

Change-Id: Iebe4248675585da1b248edc8d78b1af2b136271e
diff --git a/jenkins/checker_definitions.txt b/jenkins/checker_definitions.txt
new file mode 100644
index 0000000..0340800
--- /dev/null
+++ b/jenkins/checker_definitions.txt
@@ -0,0 +1,43 @@
+POST /plugins/checks/checkers/ HTTP/1.0
+  Content-Type: application/json; charset=UTF-8
+  {
+    "uuid": "gerritforge:notedb-a6a0e4682515f3521897c5f950d1394f4619d928",
+    "name": "Build/Tests",
+    "description": "Builds the code base and executes unit/integration tests",
+    "repository": "gerrit",
+    "query": "branch:stable-2.15 OR branch:stable-2.16 OR branch:stable-3.0 OR branch:master",
+    "blocking": []
+  }
+
+POST /plugins/checks/checkers/ HTTP/1.0
+  Content-Type: application/json; charset=UTF-8
+  {
+    "uuid": "gerritforge:reviewdb-a6a0e4682515f3521897c5f950d1394f4619d928",
+    "name": "ReviewDb Build/Tests",
+    "description": "Using ReviewDb, builds the code base and executes unit/integration tests",
+    "repository": "gerrit",
+    "query": "branch:stable-2.13 OR branch:stable-2.14 OR branch:stable-2.15 OR branch:stable-2.16",
+    "blocking": []
+  }
+
+POST /plugins/checks/checkers/ HTTP/1.0
+  Content-Type: application/json; charset=UTF-8
+  {
+    "uuid": "gerritforge:polygerrit-a6a0e4682515f3521897c5f950d1394f4619d928",
+    "name": "PolyGerrit UI Tests",
+    "description": "Executes unit/integration tests for PolyGerrit UI",
+    "repository": "gerrit",
+    "query": "(directory:polygerit-ui OR file:WORKSPACE) AND (branch:stable-2.14 OR branch:stable-2.15 OR branch:stable-2.16 OR branch:stable-3.0 OR branch:master)",
+    "blocking": []
+  }
+
+POST /plugins/checks/checkers/ HTTP/1.0
+  Content-Type: application/json; charset=UTF-8
+  {
+    "uuid": "gerritforge:codestyle-a6a0e4682515f3521897c5f950d1394f4619d928",
+    "name": "Code Style",
+    "description": "Executes Code Style tests",
+    "repository": "gerrit",
+    "query": "branch:stable-2.14 OR branch:stable-2.15 OR branch:stable-2.16 OR branch:stable-3.0 OR branch:master",
+    "blocking": []
+  }
diff --git a/jenkins/gerrit-verifier-change.groovy b/jenkins/gerrit-verifier-change.groovy
index 6fa43b8..baa1223 100644
--- a/jenkins/gerrit-verifier-change.groovy
+++ b/jenkins/gerrit-verifier-change.groovy
@@ -32,6 +32,7 @@
   static String addVerifiedTag = ciTag("addVerified")
   static Set<String> codeStyleBranches = ["master", "stable-3.0", "stable-2.16", "stable-2.15", "stable-2.14"]
   static resTicks = [ 'ABORTED':'\u26aa', 'SUCCESS':'\u2705', 'FAILURE':'\u274c' ]
+  static String gerritRepositoryNameSha1Suffix = "-a6a0e4682515f3521897c5f950d1394f4619d928"
 }
 
 abstract class AbstractLabel {
@@ -132,6 +133,50 @@
   }
 }
 
+class GerritCheck {
+  String uuid
+  String changeNum
+  String sha1
+  Object build
+
+  GerritCheck(name, changeNum, sha1, build) {
+    this.uuid = "gerritforge:" + name.replaceAll("(buck/|bazel/)", "") + Globals.gerritRepositoryNameSha1Suffix
+    this.changeNum = changeNum
+    this.sha1 = sha1
+    this.build = build
+  }
+
+  def printCheckSummary() {
+    println "----------------------------------------------------------------------------"
+    println "Gerrit Check: ${uuid}=" + build.getResult() + " to change " + changeNum + "/" + sha1
+    println "----------------------------------------------------------------------------"
+  }
+
+  def getCheckResultFromBuild() {
+    switch(build.getResult()) {
+      case Result.SUCCESS:
+        return "SUCCESSFUL"
+      case Result.ABORTED:
+        return "NOT_STARTED"
+      case Result.FAILURE:
+      case Result.NOT_BUILT:
+      case Result.UNSTABLE:
+      default:
+        return "FAILED"
+    }
+  }
+
+  def createCheckPayload() {
+    def json = new JsonBuilder()
+    json {
+      checker_uuid(uuid)
+      state(getCheckResultFromBuild())
+      url(build.getBuildUrl() + "consoleText")
+    }
+    return json.toString()
+  }
+}
+
 class Gerrit {
   String url
   Script script
@@ -146,6 +191,14 @@
     }
   }
 
+  def postCheck(check) {
+    def exitCode = httpPost("a/changes/${check.changeNum}/revisions/${check.sha1}/checks",
+        check.createCheckPayload())
+    if (exitCode == 0) {
+      check.printCheckSummary()
+    }
+  }
+
   private def httpPost(path, jsonPayload) {
     def error = ""
     def gerritPostUrl = url + path
@@ -371,6 +424,9 @@
     Globals.buildsList.findAll { key,build -> key != "codestyle" })
   gerrit.addLabel(verifyLabel, change, sha1)
 
+  // Per build result create vote on gerrit checker
+  Globals.buildsList.each { type,build -> gerrit.postCheck(new GerritCheck(type, changeNum, sha1, build)) }
+
   switch(resAll) {
     case 0: build.state.result = ABORTED
             break