Merge "Add Workflow option 'inherit_parent_params'"
diff --git a/src/com/mirantis/mk/Workflow.groovy b/src/com/mirantis/mk/Workflow.groovy
index 0716a76..d13301c 100644
--- a/src/com/mirantis/mk/Workflow.groovy
+++ b/src/com/mirantis/mk/Workflow.groovy
@@ -18,6 +18,32 @@
 
 
 /**
+ * Get Jenkins parameter names, values and types from jobName
+ * @param jobName job name
+ * @return Map with parameter names as keys and the following map as values:
+ *  [
+ *    <str name1>: [type: <str cls1>, use_variable: <str name1>, defaultValue: <cls value1>],
+ *    <str name2>: [type: <str cls2>, use_variable: <str name2>, defaultValue: <cls value2>],
+ *  ]
+ */
+def getJobDefaultParameters(jobName) {
+    def jenkinsUtils = new com.mirantis.mk.JenkinsUtils()
+    def item = jenkinsUtils.getJobByName(env.JOB_NAME)
+    def parameters = [:]
+    def prop = item.getProperty(ParametersDefinitionProperty.class)
+    if(prop != null) {
+        for(param in prop.getParameterDefinitions()) {
+            def defaultParam = param.getDefaultParameterValue()
+            def cls = defaultParam.getClass().getName()
+            def value = defaultParam.getValue()
+            def name = defaultParam.getName()
+            parameters[name] = [type: cls, use_variable: name, defaultValue: value]
+        }
+    }
+    return parameters
+}
+
+/**
  * Run a Jenkins job using the collected parameters
  *
  * @param job_name          Name of the running job
@@ -141,7 +167,14 @@
         stage("Running job ${step['job']}") {
 
             def job_name = step['job']
-            def job_parameters = step['parameters']
+            def job_parameters = [:]
+            if (step['inherit_parent_params'] ?: false) {
+                // add parameters from the current job for the child job
+                job_parameters << getJobDefaultParameters(env.JOB_NAME)
+            }
+            // add parameters from the workflow for the child job
+            job_parameters << step['parameters']
+
             // Collect job parameters and run the job
             def job_info = runJob(job_name, job_parameters, global_variables, propagate)
             def job_result = job_info.getResult()
@@ -206,6 +239,17 @@
  *         KUBECONFIG_ARTIFACT: artifacts/management_kubeconfig
  *         DEPLOYED_KAAS_VERSION: artifacts/management_version
  *
+ *     - job: create-child
+ *       inherit_parent_params: true
+ *       ignore_failed: false
+ *       parameters:
+ *         KUBECONFIG_ARTIFACT_URL:
+ *           type: StringParameterValue
+ *           use_variable: KUBECONFIG_ARTIFACT
+ *         KAAS_VERSION:
+ *           type: StringParameterValue
+ *           get_variable_from_url: DEPLOYED_KAAS_VERSION
+ *
  *     - job: test-kaas-ui
  *       ignore_not_built: false
  *       parameters:
@@ -233,6 +277,13 @@
  *
  *     runScenario(scenario)
  *
+ * Scenario workflow keys:
+ *
+ *   job: string. Jenkins job name
+ *   ignore_failed: bool. if true, keep running the workflow jobs if the job is failed, but fail the workflow at finish
+ *   ignore_not_built: bool. if true, keep running the workflow jobs if the job set own status to NOT_BUILT, do not fail the workflow at finish for such jobs
+ *   inherit_parent_params: bool. if true, provide all parameters from the parent job to the child job as defaults
+ *   parameters: dict. parameters name and type to inherit from parent to child job, or from artifact to child job
  */
 
 def runScenario(scenario, slackReportChannel = '') {