From a3bb6d254c43494b35b1c8d6470f229df6a78d77 Mon Sep 17 00:00:00 2001 From: Sergei Parshev Date: Wed, 23 Oct 2019 20:03:35 -0700 Subject: [PATCH] MPL-8 Prepared realization of OUT MPLModule mechanism --- src/com/griddynamics/devops/mpl/Helper.groovy | 15 +++------------ src/com/griddynamics/devops/mpl/MPLConfig.groovy | 11 +++++++---- src/com/griddynamics/devops/mpl/MPLManager.groovy | 13 +++++++++++++ .../griddynamics/devops/mpl/MPLConfigTest.groovy | 2 +- .../devops/mpl/testing/MPLTestBase.groovy | 3 --- vars/MPLModule.groovy | 9 ++++++++- vars/MPLPipelineConfigMerge.groovy | 7 +++++++ 7 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 vars/MPLPipelineConfigMerge.groovy diff --git a/src/com/griddynamics/devops/mpl/Helper.groovy b/src/com/griddynamics/devops/mpl/Helper.groovy index 6367fee..b8e5c36 100644 --- a/src/com/griddynamics/devops/mpl/Helper.groovy +++ b/src/com/griddynamics/devops/mpl/Helper.groovy @@ -34,6 +34,9 @@ import org.jenkinsci.plugins.workflow.libs.LibrariesAction import hudson.model.Run import hudson.FilePath +import com.griddynamics.devops.mpl.MPLManager +import com.griddynamics.devops.mpl.MPLException + /** * Manages all helpers to interact with low-level groovy * @@ -185,16 +188,4 @@ abstract class Helper { } return null } - - /** - * Special function to return exception if someone tries to use MPLConfig in a wrong way - * Basically used just to be overridden on the unit tests side. - * - * @param config current MPLConfig configuration - * - * @return Set of entries - but only when overridden by unit tests - */ - static Set configEntrySet(Map config) { - throw new MPLException('Forbidden to iterate over MPLConfig') - } } diff --git a/src/com/griddynamics/devops/mpl/MPLConfig.groovy b/src/com/griddynamics/devops/mpl/MPLConfig.groovy index 12fb122..cd5dbf8 100644 --- a/src/com/griddynamics/devops/mpl/MPLConfig.groovy +++ b/src/com/griddynamics/devops/mpl/MPLConfig.groovy @@ -24,6 +24,8 @@ package com.griddynamics.devops.mpl import com.cloudbees.groovy.cps.NonCPS +import com.griddynamics.devops.mpl.Helper +import com.griddynamics.devops.mpl.MPLException /** * Configuration object to provide the config interface @@ -46,7 +48,7 @@ public class MPLConfig implements Map, Serializable { * @return MPLConfig object */ @NonCPS - static public MPLConfig create(cfg) { + static public MPLConfig create(cfg = [:]) { new MPLConfig(cfg) } @@ -70,8 +72,8 @@ public class MPLConfig implements Map, Serializable { } /** - * It's forbidden to use the MPLConfig as a regular Map. - * CFG is just a config place, you can't iterate over it. + * It's not recommended to use the MPLConfig as a regular Map. + * CFG is just a config place. * The module should know the key it's trying to use, not to process everything. */ public Object remove(Object key) {} @@ -84,7 +86,8 @@ public class MPLConfig implements Map, Serializable { public int size() { this.@config.size() } public boolean isEmpty() { this.@config.isEmpty() } - public Set entrySet() { Helper.configEntrySet(this.@config) } + // It's possible to iterate for internal MPL needs + public Set entrySet() { this.@config.entrySet() } /** * Get a value copy of the provided config key path diff --git a/src/com/griddynamics/devops/mpl/MPLManager.groovy b/src/com/griddynamics/devops/mpl/MPLManager.groovy index a1b2f12..8aaa66f 100644 --- a/src/com/griddynamics/devops/mpl/MPLManager.groovy +++ b/src/com/griddynamics/devops/mpl/MPLManager.groovy @@ -23,6 +23,10 @@ package com.griddynamics.devops.mpl +import com.griddynamics.devops.mpl.MPLException +import com.griddynamics.devops.mpl.MPLConfig +import com.griddynamics.devops.mpl.Helper + /** * Object to help with MPL pipelines configuration & poststeps * @@ -96,6 +100,15 @@ class MPLManager implements Serializable { config.modules ? config.modules[name] != null : false } + /** + * Deep merge of the pipeline config with the provided config + * + * @param cfg Map or MPLConfig + */ + public configMerge(cfg) { + config = Helper.mergeMaps(config, cfg) + } + /** * Add post step to the array with specific name * diff --git a/test/groovy/com/griddynamics/devops/mpl/MPLConfigTest.groovy b/test/groovy/com/griddynamics/devops/mpl/MPLConfigTest.groovy index d73220d..81ce101 100644 --- a/test/groovy/com/griddynamics/devops/mpl/MPLConfigTest.groovy +++ b/test/groovy/com/griddynamics/devops/mpl/MPLConfigTest.groovy @@ -35,7 +35,7 @@ class MPLConfigTest { @Before void setUp() throws Exception { - this.CFG = new MPLConfig([ + this.CFG = MPLConfig.create([ first1_level: [ second1_level: [ third1_value: 5, diff --git a/test/groovy/com/griddynamics/devops/mpl/testing/MPLTestBase.groovy b/test/groovy/com/griddynamics/devops/mpl/testing/MPLTestBase.groovy index fe6bcb2..116de16 100644 --- a/test/groovy/com/griddynamics/devops/mpl/testing/MPLTestBase.groovy +++ b/test/groovy/com/griddynamics/devops/mpl/testing/MPLTestBase.groovy @@ -72,8 +72,5 @@ abstract class MPLTestBase extends BasePipelineTest { script.metaClass.methodMissing = helper.getMethodMissingInterceptor() script.run() } - - // Show the dump of the configuration during unit tests execution - Helper.metaClass.static.configEntrySet = { Map config -> config.entrySet() } } } diff --git a/vars/MPLModule.groovy b/vars/MPLModule.groovy index 5cbfe48..8ba54f1 100644 --- a/vars/MPLModule.groovy +++ b/vars/MPLModule.groovy @@ -39,6 +39,8 @@ import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException * * @param name used to determine the module name, by default it's current stage name (ex. "Maven Build") * @param cfg module configuration to override. Will update the common module configuration + * + * @return MPLConfig object was available in the module as `OUT` */ def call(String name = env.STAGE_NAME, cfg = null) { if( cfg == null ) @@ -73,9 +75,12 @@ def call(String name = env.STAGE_NAME, cfg = null) { if( ! module_src ) throw new MPLModuleException("Unable to find not active module to execute: ${(active_modules).join(' --> ')} -X> ${module_path}") + // OUT will be return to caller + def out = MPLConfig.create() + try { MPLManager.instance.pushActiveModule(module_path) - Helper.runModule(module_src, module_path, [CFG: cfg]) + Helper.runModule(module_src, module_path, [CFG: cfg, OUT: out]) } catch( FlowInterruptedException ex ) { // The exception is used by Jenkins to abort a running build and consequently @@ -99,4 +104,6 @@ def call(String name = env.STAGE_NAME, cfg = null) { } MPLManager.instance.popActiveModule() } + + return out } diff --git a/vars/MPLPipelineConfigMerge.groovy b/vars/MPLPipelineConfigMerge.groovy new file mode 100644 index 0000000..efe6c8d --- /dev/null +++ b/vars/MPLPipelineConfigMerge.groovy @@ -0,0 +1,7 @@ +// Merges the provided configuration with the pipeline config + +import com.griddynamics.devops.mpl.MPLManager + +def call(cfg) { + MPLManager.instance.configMerge(cfg) +}