Refactor collecting YAML with params for child jobs

Jenkins doesn't have shipped groovy/java modules to handle YAML, but JSON
is a subset of YAML.

So let's use standard JSON handling modules to serialize gerrit environment
variables to JSON, and parse it as YAML.

Change-Id: I79a9ba5dd8ec9559581cec3e294b1336ccee85dd
See: https://mirantis.jira.com/browse/PROD-24467
See: https://en.wikipedia.org/wiki/YAML#Comparison_with_JSON
diff --git a/gating-pipeline.groovy b/gating-pipeline.groovy
index aeaee9a..15518d4 100644
--- a/gating-pipeline.groovy
+++ b/gating-pipeline.groovy
@@ -4,6 +4,7 @@
  * JOBS_NAMESPACE - Gerrit gating jobs namespace (mk, contrail, ...)
  *
  **/
+import groovy.json.JsonOutput
 
 def common = new com.mirantis.mk.Common()
 def gerrit = new com.mirantis.mk.Gerrit()
@@ -18,12 +19,7 @@
 }
 
 def callJobWithExtraVars(String jobName) {
-    def gerritVars = '\n---'
-    for (envVar in env.getEnvironment()) {
-        if (envVar.key.startsWith("GERRIT_")) {
-            gerritVars += "\n${envVar.key}: '${envVar.value}'"
-        }
-    }
+    def gerritVars = JsonOutput.toJson(env.getEnvironment().findAll{ it.key.startsWith('GERRIT_') })
     testJob = build job: jobName, parameters: [
         [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: gerritVars]
     ]
diff --git a/test-cookiecutter-reclass.groovy b/test-cookiecutter-reclass.groovy
index 33ab3f5..cee90d1 100644
--- a/test-cookiecutter-reclass.groovy
+++ b/test-cookiecutter-reclass.groovy
@@ -12,6 +12,8 @@
    Always test GERRIT_REFSPEC VS GERRIT_BRANCH-master version of opposite project
  */
 
+import groovy.json.JsonOutput
+
 common = new com.mirantis.mk.Common()
 gerrit = new com.mirantis.mk.Gerrit()
 git = new com.mirantis.mk.Git()
@@ -20,6 +22,9 @@
 extraVarsYAML = env.EXTRA_VARIABLES_YAML.trim() ?: ''
 if (extraVarsYAML) {
     common.mergeEnv(env, extraVarsYAML)
+    extraVars = readYaml text: extraVarsYAML
+} else {
+    extraVars = [:]
 }
 
 slaveNode = env.SLAVE_NODE ?: 'docker'
@@ -74,16 +79,15 @@
     // modelFile - `modelfiname` from model/modelfiname/modelfiname.yaml
     //* Grub all models and send it to check in paralell - by one in thread.
     def _uuid = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}_${modelFile.toLowerCase()}_" + UUID.randomUUID().toString().take(8)
-    def _values_string = """
----
-MODELS_TARGZ: "${env.BUILD_URL}/artifact/${reclassArtifactName}"
-DockerCName: "${_uuid}"
-testReclassEnv: "model/${modelFile}/"
-modelFile: "contexts/${modelFile}.yml"
-DISTRIB_REVISION: "${testDistribRevision}"
-useExtraRepos: ${useExtraRepos}
-${extraVarsYAML.replaceAll('---', '')}
-"""
+    def _values = [
+        MODELS_TARGZ: "${env.BUILD_URL}/artifact/${reclassArtifactName}",
+        DockerCName: _uuid,
+        testReclassEnv: "model/${modelFile}/",
+        modelFile: "contexts/${modelFile}.yml",
+        DISTRIB_REVISION: testDistribRevision,
+        useExtraRepos: useExtraRepos,
+    ]
+    def _values_string = JsonOutput.toJson(_values << extraVars)
     def chunkJob = build job: chunkJobName, parameters: [
         [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML',
          value : _values_string.stripIndent()],
diff --git a/test-system-reclass-pipeline.groovy b/test-system-reclass-pipeline.groovy
index 04eafeb..a8e10b7 100644
--- a/test-system-reclass-pipeline.groovy
+++ b/test-system-reclass-pipeline.groovy
@@ -1,3 +1,5 @@
+import groovy.json.JsonOutput
+
 def gerrit = new com.mirantis.mk.Gerrit()
 def common = new com.mirantis.mk.Common()
 
@@ -7,12 +9,7 @@
 if (extraVarsYaml != '') {
     common.mergeEnv(env, extraVarsYaml)
 } else {
-    extraVarsYaml = '\n---'
-    for (envVar in env.getEnvironment()) {
-        if (envVar.key.startsWith("GERRIT_")) {
-            extraVarsYaml += "\n${envVar.key}: '${envVar.value}'"
-        }
-    }
+    extraVarsYaml = JsonOutput.toJson(env.getEnvironment().findAll{ it.key.startsWith('GERRIT_') })
 }
 
 def slaveNode = env.SLAVE_NODE ?: 'python&&docker'