From 3b3754f1bc1a8c8573870e07ba61decb43160b3c Mon Sep 17 00:00:00 2001 From: Willem Borgesius Date: Wed, 30 Sep 2020 20:13:36 +0200 Subject: [PATCH] Pass param value maps back so that they can be logged in the callstack for a build step. Add secret param method --- .../DeclarativePipelineTest.groovy | 28 +++++++++---------- .../TestDeclarativePipeline.groovy | 9 ++++++ .../jenkinsfiles/DownstreamBuild_Jenkinsfile | 18 ++++++++++++ 3 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 src/test/jenkins/jenkinsfiles/DownstreamBuild_Jenkinsfile diff --git a/src/main/groovy/com/lesfurets/jenkins/unit/declarative/DeclarativePipelineTest.groovy b/src/main/groovy/com/lesfurets/jenkins/unit/declarative/DeclarativePipelineTest.groovy index e4095ccc..fd4d2521 100644 --- a/src/main/groovy/com/lesfurets/jenkins/unit/declarative/DeclarativePipelineTest.groovy +++ b/src/main/groovy/com/lesfurets/jenkins/unit/declarative/DeclarativePipelineTest.groovy @@ -12,27 +12,20 @@ abstract class DeclarativePipelineTest extends BasePipelineTest { GenericPipelineDeclaration.createComponent(DeclarativePipeline, closure).execute(delegate) } - def paramInterceptor = { Map desc -> - addParam(desc.name, desc.defaultValue, false) - } - - def stringInterceptor = { Map desc-> - if (desc) { - // we are in context of parameters { string(...)} - if (desc.name) { - addParam(desc.name, desc.defaultValue, false) - } - // we are in context of withCredentials([string()..]) { } - if(desc.variable) { - return desc.variable - } + def stringInterceptor = { Map param -> + // we are in context of withCredentials([string()..]) { } + if(param?.variable) { + addParam(param.name, param.defaultValue, false) + return param.variable + } else { + return param } } @Override void setUp() throws Exception { super.setUp() - helper.registerAllowedMethod('booleanParam', [Map], paramInterceptor) + helper.registerAllowedMethod('booleanParam', [Map], { booleanParam -> booleanParam }) helper.registerAllowedMethod('checkout', [Closure]) helper.registerAllowedMethod('credentials', [String], { String credName -> return binding.getVariable('credentials')[credName] @@ -47,6 +40,11 @@ abstract class DeclarativePipelineTest extends BasePipelineTest { helper.registerAllowedMethod('string', [Map], stringInterceptor) helper.registerAllowedMethod('timeout', [Integer, Closure]) helper.registerAllowedMethod('timestamps') + helper.registerAllowedMethod('password', [Map], { Map param -> + String obscuredValue = param.containsKey("value") ? param.get("value").replaceAll(".","*") : null + param.put("value", obscuredValue) + return param + }) binding.setVariable('credentials', [:]) binding.setVariable('params', [:]) } diff --git a/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclarativePipeline.groovy b/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclarativePipeline.groovy index 47f9a246..3874ad21 100644 --- a/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclarativePipeline.groovy +++ b/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclarativePipeline.groovy @@ -524,4 +524,13 @@ class TestDeclarativePipeline extends DeclarativePipelineTest { assertJobStatusSuccess() } + @Test + void should_log_downstream_var() throws Exception { + runScript('DownstreamBuild_Jenkinsfile') + printCallStack() + assertCallStack().contains('build({job=stop, parameters=[{name=stringVarName, value=stringVarValue}, {name=secretVarName, value=***********}, {name=booleanVarName, value=true}], wait=true}') + assertJobStatusSuccess() + + } + } diff --git a/src/test/jenkins/jenkinsfiles/DownstreamBuild_Jenkinsfile b/src/test/jenkins/jenkinsfiles/DownstreamBuild_Jenkinsfile new file mode 100644 index 00000000..250503ad --- /dev/null +++ b/src/test/jenkins/jenkinsfiles/DownstreamBuild_Jenkinsfile @@ -0,0 +1,18 @@ +pipeline { + agent none + stages { + stage('Run subbuild') { + parallel { + stage('Build') { + steps { + build(job: "stop", parameters: [ + string(name: 'stringVarName', value: 'stringVarValue'), + password(name: 'secretVarName', value: 'superSecret'), + booleanParam(name: 'booleanVarName', value: 'true') + ], wait: true) + } + } + } + } + } +}