Replace Gerrit vetting job with the pipeline version

This change also move the responsibility to verify
a gerrit code review to the child job. This allow for
the verifier to not wait for the child process to end.

Fixes #12.

Change-Id: I458e0d42498830636f693bee6366245033373a27
diff --git a/jenkins/build_defs/bazel-job-Gerrit.xml.tpl b/jenkins/build_defs/bazel-job-Gerrit.xml.tpl
new file mode 100644
index 0000000..45e2ede
--- /dev/null
+++ b/jenkins/build_defs/bazel-job-Gerrit.xml.tpl
@@ -0,0 +1,67 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<flow-definition plugin="workflow-job">
+  <actions/>
+  <description>Test Gerrit code review for {{ variables.PROJECT_NAME }}. 
+
+This job for testing changes submitted to the Gerrit project: {{ variables.GERRIT_PROJECT }}.
+</description>
+  <keepDependencies>false</keepDependencies>
+  <properties>
+    {% if variables.RUN_SEQUENTIAL == "true" %}
+    <org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty/>
+    {% endif %}
+    {% if variables.github == "True" %}
+    <com.coravy.hudson.plugins.github.GithubProjectProperty>
+      <projectUrl>{{ variables.GITHUB_URL }}</projectUrl>
+    </com.coravy.hudson.plugins.github.GithubProjectProperty>
+    {% endif %}
+    <hudson.model.ParametersDefinitionProperty>
+      <parameterDefinitions>
+        <hudson.model.StringParameterDefinition>
+          <name>REFSPEC</name>
+          <description>Refs to pull</description>
+          <defaultValue>+refs/heads/*:refs/remotes/origin/*</defaultValue>
+        </hudson.model.StringParameterDefinition>
+        <hudson.model.StringParameterDefinition>
+          <name>BRANCH</name>
+          <description>Branch to build</description>
+          <defaultValue>master</defaultValue>
+        </hudson.model.StringParameterDefinition>
+        <hudson.model.StringParameterDefinition>
+          <name>CHANGE_NUMBER</name>
+          <description>Number of the change being tested, for information only.</description>
+          <defaultValue>no-change</defaultValue>
+        </hudson.model.StringParameterDefinition>
+      </parameterDefinitions>
+    </hudson.model.ParametersDefinitionProperty>
+    <org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
+      <triggers/>
+    </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
+  </properties>
+  <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps">
+    <script><![CDATA[
+gerritReview("https://bazel-review.googlesource.com/",
+    "/opt/secrets/gerritcookies",
+    "Bazel CI <ci.bazel@gmail.com>",
+    params.CHANGE_NUMBER,
+    params.BRANCH) {
+    bazelCiConfiguredJob(
+        repository: "https://bazel.googlesource.com/{{ variables.GERRIT_PROJECT }}",
+        branch: params.BRANCH,
+        refspec: params.REFSPEC,
+        bazel_version: "latest",
+        configuration: '''{{ raw_imports['JSON_CONFIGURATION'].replace('\\', '\\\\').replace("'", "\\'") }}''',
+        mail_recipient: "",
+        {% if variables.SAUCE_ENABLED == "true" %}
+        sauce: "61b4846b-279d-4369-ae20-31e9d8b9bc66",
+        {% endif %}
+        run_sequentially: {{ variables.RUN_SEQUENTIAL }},
+        restrict_configuration: {{ variables.RESTRICT_CONFIGURATION }}
+    )
+}
+    ]]></script>
+    <sandbox>true</sandbox>
+  </definition>
+  <triggers/>
+  <quietPeriod>5</quietPeriod>
+</flow-definition>
diff --git a/jenkins/build_defs/github-jobs-Gerrit.xml.tpl b/jenkins/build_defs/github-jobs-Gerrit.xml.tpl
deleted file mode 100644
index fbccde0..0000000
--- a/jenkins/build_defs/github-jobs-Gerrit.xml.tpl
+++ /dev/null
@@ -1,172 +0,0 @@
-<?xml version='1.0' encoding='UTF-8'?>
-<!--
-  Copyright 2015 The Bazel Authors. All rights reserved.
-
-  Licensed under the Apache License, Version 2.0 (the "License");
-  you may not use this file except in compliance with the License.
-  You may obtain a copy of the License at
-
-     http://www.apache.org/licenses/LICENSE-2.0
-
-  Unless required by applicable law or agreed to in writing, software
-  distributed under the License is distributed on an "AS IS" BASIS,
-  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-  See the License for the specific language governing permissions and
-  limitations under the License.
--->
-<matrix-project>
-  <actions/>
-  <description>Test that pull requests on the {{ variables.PROJECT_NAME }} project still build with Bazel at head and latest release.
-
-This job for testing changes submitted to the Gerrit project: {{ variables.GERRIT_PROJECT }}.
-</description>
-  <keepDependencies>false</keepDependencies>
-  <properties>
-  {% if variables.github == "True" %}
-    <com.coravy.hudson.plugins.github.GithubProjectProperty>
-      <projectUrl>{{ variables.GITHUB_URL }}</projectUrl>
-    </com.coravy.hudson.plugins.github.GithubProjectProperty>
-  {% endif %}
-    <hudson.model.ParametersDefinitionProperty>
-      <parameterDefinitions>
-        <hudson.model.StringParameterDefinition>
-          <name>REFSPEC</name>
-          <description>Refs to pull</description>
-          <defaultValue>+refs/heads/*:refs/remotes/origin/*</defaultValue>
-        </hudson.model.StringParameterDefinition>
-        <hudson.model.StringParameterDefinition>
-          <name>BRANCH</name>
-          <description>Branch to build</description>
-          <defaultValue>master</defaultValue>
-        </hudson.model.StringParameterDefinition>
-        <hudson.model.StringParameterDefinition>
-          <name>CHANGE_URL</name>
-          <description>URL of the change being tested, for information only.</description>
-          <defaultValue>no-change</defaultValue>
-        </hudson.model.StringParameterDefinition>
-      </parameterDefinitions>
-    </hudson.model.ParametersDefinitionProperty>
-  </properties>
-  <scm class="hudson.plugins.git.GitSCM">
-    <configVersion>2</configVersion>
-    <userRemoteConfigs>
-      <hudson.plugins.git.UserRemoteConfig>
-        <refspec>${REFSPEC}</refspec>
-        <url>https://bazel.googlesource.com/{{ variables.GERRIT_PROJECT }}</url>
-      </hudson.plugins.git.UserRemoteConfig>
-    </userRemoteConfigs>
-    <branches>
-      <hudson.plugins.git.BranchSpec>
-        <name>${BRANCH}</name>
-      </hudson.plugins.git.BranchSpec>
-    </branches>
-    <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
-    <submoduleCfg class="list"/>
-    <extensions>
-      <hudson.plugins.git.extensions.impl.CleanBeforeCheckout/>
-      <hudson.plugins.git.extensions.impl.AuthorInChangelog/>
-      <hudson.plugins.git.extensions.impl.SubmoduleOption>
-        <disableSubmodules>false</disableSubmodules>
-        <recursiveSubmodules>true</recursiveSubmodules>
-        <trackingSubmodules>false</trackingSubmodules>
-      </hudson.plugins.git.extensions.impl.SubmoduleOption>
-    </extensions>
-  </scm>
-  <quietPeriod>5</quietPeriod>
-  <canRoam>true</canRoam>
-  <disabled>{{ variables.disabled }}</disabled>
-  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
-  <blockBuildWhenUpstreamBuilding>true</blockBuildWhenUpstreamBuilding>
-  <concurrentBuild>false</concurrentBuild>
-  <axes>
-    <hudson.matrix.LabelAxis>
-      <name>PLATFORM_NAME</name>
-      <values>{% for v in variables.PLATFORMS.split("\n") %}<string>{{ v }}</string>{% endfor %}</values>
-    </hudson.matrix.LabelAxis>
-    <hudson.matrix.TextAxis>
-      <name>BAZEL_VERSION</name>
-       <values>{% for v in variables.BAZEL_VERSIONS.split("\n") %}<string>{{ v }}</string>{% endfor %}</values>
-    </hudson.matrix.TextAxis>
-  </axes>
-  <builders>
-    <org.jenkinsci.plugins.conditionalbuildstep.ConditionalBuilder plugin="conditional-buildstep@1.3.3">
-    <runner class="org.jenkins_ci.plugins.run_condition.BuildStepRunner$Fail" plugin="run-condition@1.0"/>
-    <runCondition class="org.jenkins_ci.plugins.run_condition.core.ExpressionCondition" plugin="run-condition@1.0">
-      <expression>^((?!windows).)*$</expression>
-      <label>${PLATFORM_NAME}</label>
-    </runCondition>
-    <conditionalbuilders>
-    <hudson.tasks.Shell>
-      <command>{{ imports['//jenkins/build_defs:github-jobs.sh.tpl'] }}</command>
-    </hudson.tasks.Shell>
-    <hudson.tasks.Shell>
-      <command>{{ imports['//jenkins/build_defs:github-jobs.test-logs.sh.tpl'] }}</command>
-    </hudson.tasks.Shell>
-    </conditionalbuilders>
-    </org.jenkinsci.plugins.conditionalbuildstep.ConditionalBuilder>
-
-    <org.jenkinsci.plugins.conditionalbuildstep.ConditionalBuilder plugin="conditional-buildstep@1.3.3">
-      <runner class="org.jenkins_ci.plugins.run_condition.BuildStepRunner$Fail" plugin="run-condition@1.0"/>
-      <runCondition class="org.jenkins_ci.plugins.run_condition.logic.And" plugin="run-condition@1.0">
-        <conditions>
-          <org.jenkins__ci.plugins.run__condition.logic.ConditionContainer>
-            <condition class="org.jenkins_ci.plugins.run_condition.core.ExpressionCondition">
-              <expression>windows.*</expression>
-              <label>${PLATFORM_NAME}</label>
-            </condition>
-          </org.jenkins__ci.plugins.run__condition.logic.ConditionContainer>
-        </conditions>
-      </runCondition>
-      <conditionalbuilders>
-        <hudson.tasks.BatchFile>
-          <command>{{ imports['//jenkins/build_defs:github-jobs.bat.tpl'] }}</command>
-        </hudson.tasks.BatchFile>
-        <hudson.tasks.BatchFile>
-          <command>{{ imports['//jenkins/build_defs:github-jobs.test-logs.bat.tpl'] }}</command>
-        </hudson.tasks.BatchFile>
-      </conditionalbuilders>
-    </org.jenkinsci.plugins.conditionalbuildstep.ConditionalBuilder>
-
-    <org.jenkinsci.plugins.conditionalbuildstep.singlestep.SingleConditionalBuilder>
-      <condition class="org.jenkins_ci.plugins.run_condition.core.FileExistsCondition">
-        <file>.unstable</file>
-        <baseDir class="org.jenkins_ci.plugins.run_condition.common.BaseDirectory$Workspace"/>
-      </condition>
-      <buildStep class="org.jenkins_ci.plugins.fail_the_build.FixResultBuilder">
-        <defaultResultName>UNSTABLE</defaultResultName>
-        <success></success>
-        <unstable></unstable>
-        <failure></failure>
-        <aborted></aborted>
-      </buildStep>
-      <runner class="org.jenkins_ci.plugins.run_condition.BuildStepRunner$Unstable"/>
-    </org.jenkinsci.plugins.conditionalbuildstep.singlestep.SingleConditionalBuilder>
-  </builders>
-  <publishers>
-    <hudson.tasks.junit.JUnitResultArchiver>
-      <testResults>bazel-testlogs/**/test.xml, bazel-testlogs/**/dummy.xml</testResults>
-      <keepLongStdio>false</keepLongStdio>
-      <healthScaleFactor>1.0</healthScaleFactor>
-      <allowEmptyResults>true</allowEmptyResults>
-    </hudson.tasks.junit.JUnitResultArchiver>
-    <com.cloudbees.jenkins.GitHubCommitNotifier>
-      <resultOnFailure>FAILURE</resultOnFailure>
-    </com.cloudbees.jenkins.GitHubCommitNotifier>
-  </publishers>
-  <buildWrappers>
-    <hudson.plugins.build__timeout.BuildTimeoutWrapper>
-      <strategy class="hudson.plugins.build_timeout.impl.AbsoluteTimeOutStrategy">
-        <timeoutMinutes>240</timeoutMinutes>
-      </strategy>
-      <operationList>
-        <hudson.plugins.build__timeout.operations.FailOperation/>
-        <hudson.plugins.build__timeout.operations.WriteDescriptionOperation>
-          <description>Timed out</description>
-        </hudson.plugins.build__timeout.operations.WriteDescriptionOperation>
-      </operationList>
-    </hudson.plugins.build__timeout.BuildTimeoutWrapper>
-  </buildWrappers>
-  <executionStrategy class="hudson.matrix.DefaultMatrixExecutionStrategyImpl">
-    <runSequentially>false</runSequentially>
-  </executionStrategy>
-</matrix-project>
diff --git a/jenkins/build_defs/jenkins_job.bzl b/jenkins/build_defs/jenkins_job.bzl
index bdf3914..65985a4 100644
--- a/jenkins/build_defs/jenkins_job.bzl
+++ b/jenkins/build_defs/jenkins_job.bzl
@@ -226,13 +226,10 @@
   if gerrit_project != None:
     jenkins_job(
         name = "CR/" + name,
-        config = "//jenkins/build_defs:github-jobs-Gerrit.xml.tpl",
-        deps = [
-            "//jenkins/build_defs:github-jobs.sh.tpl",
-            "//jenkins/build_defs:github-jobs.bat.tpl",
-            "//jenkins/build_defs:github-jobs.test-logs.sh.tpl",
-            "//jenkins/build_defs:github-jobs.test-logs.bat.tpl",
-        ],
+        config = "//jenkins/build_defs:bazel-job-Gerrit.xml.tpl",
+        deps_aliases = {
+          "JSON_CONFIGURATION": config,
+        },
         substitutions=substitutions,
         project=project,
         org=org,
diff --git a/jenkins/gerrit-verifier.groovy b/jenkins/gerrit-verifier.groovy
index e438c1e..3288dc0 100644
--- a/jenkins/gerrit-verifier.groovy
+++ b/jenkins/gerrit-verifier.groovy
@@ -24,23 +24,19 @@
 def buildChange(gerrit, change) {
   def refspec = "+" + change.ref + ":" + change.ref.replaceAll('ref/', 'ref/remotes/origin/')
   def jobs = JenkinsUtils.jobsWithDescription("CR", "Gerrit project: " + change.project + ".")
-  def changeUrl = gerrit.url(change.number, change.patchNumber)
+
   if (jobs != null && !jobs.empty) {
     gerrit.addReviewer(change.number)
     for(job in jobs) {
-      gerritReview(gerrit.server, "/opt/secrets/gerritcookies", gerrit.reviewer, change.number, change.sha1) {
-        // gerritBuild is a trick to propagate the result, the gerritReview step should live in the side of
-        // the child job.
-        delegate.gerritBuild = build job: job, propagate: false, parameters: [
-          [$class: 'StringParameterValue', name: 'REFSPEC', value: refspec],
-          [$class: 'StringParameterValue', name: 'BRANCH', value: change.sha1],
-          [$class: 'StringParameterValue', name: 'CHANGE_URL', value: changeUrl]]
-      }
+      build job: job, propagate: false, wait: false, parameters: [
+        [$class: 'StringParameterValue', name: 'REFSPEC', value: refspec],
+        [$class: 'StringParameterValue', name: 'BRANCH', value: change.sha1],
+        [$class: 'StringParameterValue', name: 'CHANGE_NUMBER', value: change.number.toString()]]
     }
   }
 }
 
-timeout(240) {
+timeout(2) {
   def changes = [:]
   // Get open gerrit changes that were verified but not yet processed
   stage("Get changes") {
diff --git a/jenkins/lib/vars/gerritReview.groovy b/jenkins/lib/vars/gerritReview.groovy
index 7324a9c..7e92f76 100644
--- a/jenkins/lib/vars/gerritReview.groovy
+++ b/jenkins/lib/vars/gerritReview.groovy
@@ -33,9 +33,10 @@
     body.delegate = config
     body()
   } finally {
-    def verified = config.gerritBuild.result == "SUCCESS" ? "+" : "-"
-    echo "Setting ${verified}Verified to change ${url} after build returned ${config.gerritBuild.result}"
-    gerrit.review(changeNum, branch, config.gerritBuild.result == "SUCCESS" ? 1 : -1,
-                  "Build ${config.gerritBuild.getAbsoluteUrl()} finished with status ${config.gerritBuild.result}")
+    def result = config.gerritBuild.result == null ? "SUCCESS" : config.gerritBuild.result
+    def verified = result == "SUCCESS" ? "+" : "-"
+    echo "Setting ${verified}Verified to change ${url} after build returned ${result}"
+    gerrit.review(changeNum, branch, result == "SUCCESS" ? 1 : -1,
+                  "Build ${config.gerritBuild.getAbsoluteUrl()} finished with status ${result}")
   }
 }