1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-30 05:59:39 +02:00

ConfigurationLoader, ConfigurationHelper working without script reference

This commit is contained in:
Marcus Holl 2019-08-08 12:47:28 +02:00
parent 063a1dc3fc
commit 9962060254
5 changed files with 122 additions and 19 deletions

View File

@ -31,23 +31,55 @@ class ConfigurationHelper implements Serializable {
if(!this.name) throw new IllegalArgumentException('Step has no public name property!')
}
/*
* By default this methods does nothing. With this method we are able to ensure that we do not call the
* deprecated methods. Might be usefull during local development.
*/
private static handleDeprecation(script, String methodName) {
if(script != null) {
def msg = "ConfigurationHelper.${methodName} was called with a script reference." +
'This method is deprecated. Use the same method without the script reference'
if(Boolean.getBoolean('com.sap.piper.failOnScriptReferenceInConfigurationHelper'))
throw new RuntimeException(msg)
if(Boolean.getBoolean('com.sap.piper.emitWarningOnScriptReferenceInConfigurationHelper') &&
script instanceof Script) script.echo("[WARNING] ${msg}")
}
}
ConfigurationHelper collectValidationFailures() {
validationResults = validationResults ?: [:]
return this
}
ConfigurationHelper mixinGeneralConfig(Set filter = null, Map compatibleParameters = [:]){
mixinGeneralConfig(null, filter, compatibleParameters)
}
@Deprecated
/** Use mixinGeneralConfig without commonPipelineEnvironment*/
ConfigurationHelper mixinGeneralConfig(commonPipelineEnvironment, Set filter = null, Map compatibleParameters = [:]){
Map generalConfiguration = ConfigurationLoader.generalConfiguration([commonPipelineEnvironment: commonPipelineEnvironment])
handleDeprecation(commonPipelineEnvironment, 'mixinGeneralConfig')
Map generalConfiguration = ConfigurationLoader.generalConfiguration()
return mixin(generalConfiguration, filter, compatibleParameters)
}
ConfigurationHelper mixinStageConfig(stageName, Set filter = null, Map compatibleParameters = [:]){
mixinStageConfig(null, stageName, filter, compatibleParameters)
}
@Deprecated
ConfigurationHelper mixinStageConfig(commonPipelineEnvironment, stageName, Set filter = null, Map compatibleParameters = [:]){
Map stageConfiguration = ConfigurationLoader.stageConfiguration([commonPipelineEnvironment: commonPipelineEnvironment], stageName)
handleDeprecation(commonPipelineEnvironment, 'mixinStageConfig')
Map stageConfiguration = ConfigurationLoader.stageConfiguration(stageName)
return mixin(stageConfiguration, filter, compatibleParameters)
}
ConfigurationHelper mixinStepConfig(Set filter = null, Map compatibleParameters = [:]){
mixinStepConfig(null, filter, compatibleParameters)
}
@Deprecated
ConfigurationHelper mixinStepConfig(commonPipelineEnvironment, Set filter = null, Map compatibleParameters = [:]){
Map stepConfiguration = ConfigurationLoader.stepConfiguration([commonPipelineEnvironment: commonPipelineEnvironment], name)
handleDeprecation(commonPipelineEnvironment, 'mixinStepConfig')
Map stepConfiguration = ConfigurationLoader.stepConfiguration(name)
return mixin(stepConfiguration, filter, compatibleParameters)
}

View File

@ -1,44 +1,104 @@
package com.sap.piper
// script is present in the signatures in order to keep api compatibility.
// The script referenced is not used inside the method bodies.
@API(deprecated = true)
class ConfigurationLoader implements Serializable {
static Map stepConfiguration(String stepName) {
return stepConfiguration(null, stepName)
}
@Deprecated
/** Use stepConfiguration(stepName) instead */
static Map stepConfiguration(script, String stepName) {
return loadConfiguration(script, 'steps', stepName, ConfigurationType.CUSTOM_CONFIGURATION)
return loadConfiguration('steps', stepName, ConfigurationType.CUSTOM_CONFIGURATION)
}
/*
* By default this methods does nothing. With this method we are able to ensure that we do not call the
* deprecated methods. Might be usefull during local development.
*/
private static handleDeprecation(script, String methodName) {
if(script != null) {
def msg = "ConfigurationLoader.${methodName} was called with a script reference." +
'This method is deprecated. Use the same method without the script reference'
if(Boolean.getBoolean('com.sap.piper.failOnScriptReferenceInConfigurationLoader'))
throw new RuntimeException(msg)
if(Boolean.getBoolean('com.sap.piper.emitWarningOnScriptReferenceInConfigurationLoader') &&
script instanceof Script) script.echo("[WARNING] ${msg}")
}
}
static Map stageConfiguration(String stageName) {
stageConfiguration(null, stageName)
}
@Deprecated
/** Use stageConfiguration(stageName) instead */
static Map stageConfiguration(script, String stageName) {
return loadConfiguration(script, 'stages', stageName, ConfigurationType.CUSTOM_CONFIGURATION)
handleDeprecation(script, 'stageConfiguration')
return loadConfiguration('stages', stageName, ConfigurationType.CUSTOM_CONFIGURATION)
}
static Map defaultStepConfiguration(String stepName) {
defaultStepConfiguration(null, stepName)
}
@Deprecated
/** Use defaultStepConfiguration(stepName) instead */
static Map defaultStepConfiguration(script, String stepName) {
return loadConfiguration(script, 'steps', stepName, ConfigurationType.DEFAULT_CONFIGURATION)
handleDeprecation(script, 'defaultStepConfiguration')
return loadConfiguration('steps', stepName, ConfigurationType.DEFAULT_CONFIGURATION)
}
static Map defaultStageConfiguration(String stageName) {
defaultStageConfiguration(null, stageName)
}
@Deprecated
/** Use defaultStageConfiguration(stepName) instead */
static Map defaultStageConfiguration(script, String stageName) {
return loadConfiguration(script, 'stages', stageName, ConfigurationType.DEFAULT_CONFIGURATION)
handleDeprecation(script, 'defaultStageConfiguration')
return loadConfiguration('stages', stageName, ConfigurationType.DEFAULT_CONFIGURATION)
}
static Map generalConfiguration(){
generalConfiguration(null)
}
@Deprecated
/** Use generalConfiguration() instead */
static Map generalConfiguration(script){
handleDeprecation(script, 'generalConfiguration')
try {
return script?.commonPipelineEnvironment?.configuration?.general ?: [:]
return CommonPipelineEnvironment.getInstance()?.configuration?.general ?: [:]
} catch (groovy.lang.MissingPropertyException mpe) {
return [:]
}
}
static Map defaultGeneralConfiguration(){
defaultGeneralConfiguration(null)
}
@Deprecated
/** Use defaultGeneralConfiguration() instead */
static Map defaultGeneralConfiguration(script){
handleDeprecation(script, 'defaultGeneralConfiguration')
return DefaultValueCache.getInstance()?.getDefaultValues()?.general ?: [:]
}
static Map postActionConfiguration(String actionName){
postActionConfiguration(null, actionName)
}
@Deprecated
/** Use postActionConfiguration() instead */
static Map postActionConfiguration(script, String actionName){
return loadConfiguration(script, 'postActions', actionName, ConfigurationType.CUSTOM_CONFIGURATION)
handleDeprecation(script, 'postActionConfiguration')
return loadConfiguration('postActions', actionName, ConfigurationType.CUSTOM_CONFIGURATION)
}
private static Map loadConfiguration(script, String type, String entryName, ConfigurationType configType){
private static Map loadConfiguration(String type, String entryName, ConfigurationType configType){
switch (configType) {
case ConfigurationType.CUSTOM_CONFIGURATION:
try {
return script?.commonPipelineEnvironment?.configuration?.get(type)?.get(entryName) ?: [:]
return CommonPipelineEnvironment.getInstance()?.configuration?.get(type)?.get(entryName) ?: [:]
} catch (groovy.lang.MissingPropertyException mpe) {
return [:]
}

View File

@ -3,6 +3,10 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import com.sap.piper.DefaultValueCache
import com.sap.piper.CommonPipelineEnvironment
import org.junit.Ignore
import util.BasePiperTest
@ -154,9 +158,10 @@ class ChecksPublishResultsTest extends BasePiperTest {
@Test
void testPublishWithChangedStepDefaultSettings() throws Exception {
// pmd has been set to active: true in step configuration
stepRule.step.checksPublishResults(script: [commonPipelineEnvironment: [
configuration: [steps: [checksPublishResults: [pmd: [active: true]]]]
]])
CommonPipelineEnvironment.getInstance().configuration =
[steps: [checksPublishResults: [pmd: [active: true]]]]
stepRule.step.checksPublishResults([script: nullScript])
assertTrue("AnalysisPublisher options not set", publisherStepOptions['AnalysisPublisher'] != null)
assertTrue("PmdPublisher options not set", publisherStepOptions['PmdPublisher'] != null)

View File

@ -101,11 +101,17 @@ class ConfigurationHelperTest {
@Test
void testConfigurationHelperLoadingStepDefaults() {
Set filter = ['property2']
CommonPipelineEnvironment.getInstance().configuration = [
general: ['general': 'test', 'oldGeneral': 'test2'],
stages: [testStage:['stage': 'test', 'oldStage': 'test2']],
steps: [mock: [step: 'test', 'oldStep': 'test2']]
]
Map config = ConfigurationHelper.newInstance(mockScript, [property1: '27'])
.loadStepDefaults()
.mixinGeneralConfig([configuration:[general: ['general': 'test', 'oldGeneral': 'test2']]], null, [general2: 'oldGeneral'])
.mixinStageConfig([configuration:[stages:[testStage:['stage': 'test', 'oldStage': 'test2']]]], 'testStage', null, [stage2: 'oldStage'])
.mixinStepConfig([configuration:[steps:[mock: [step: 'test', 'oldStep': 'test2']]]], null, [step2: 'oldStep'])
.mixinGeneralConfig(null, null, [general2: 'oldGeneral'])
.mixinStageConfig(null, 'testStage', null, [stage2: 'oldStage'])
.mixinStepConfig(null, null, [step2: 'oldStep'])
.mixin([property1: '41', property2: '28', property3: '29'], filter)
.use()
// asserts

View File

@ -17,9 +17,9 @@ class ConfigurationLoaderTest {
defaultConfiguration.steps = [executeGradle: [dockerImage: 'gradle:4.0.1-jdk8']]
defaultConfiguration.stages = [staticCodeChecks: [pmdExcludes: '*.java']]
def pipelineEnvironment = [configuration: configuration]
DefaultValueCache.createInstance(defaultConfiguration)
return [commonPipelineEnvironment: pipelineEnvironment]
CommonPipelineEnvironment.getInstance().configuration = configuration
return [commonPipelineEnvironment: [configuration: configuration]]
}
@Test