Skip to content

Commit

Permalink
MPL-8 Prepared realization of OUT MPLModule mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergei Parshev committed Oct 24, 2019
1 parent 3d4ff63 commit a3bb6d2
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 21 deletions.
15 changes: 3 additions & 12 deletions src/com/griddynamics/devops/mpl/Helper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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')
}
}
11 changes: 7 additions & 4 deletions src/com/griddynamics/devops/mpl/MPLConfig.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand All @@ -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) {}
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/com/griddynamics/devops/mpl/MPLManager.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
}
}
9 changes: 8 additions & 1 deletion vars/MPLModule.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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
Expand All @@ -99,4 +104,6 @@ def call(String name = env.STAGE_NAME, cfg = null) {
}
MPLManager.instance.popActiveModule()
}

return out
}
7 changes: 7 additions & 0 deletions vars/MPLPipelineConfigMerge.groovy
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit a3bb6d2

Please sign in to comment.