diff --git a/src/com/sap/piper/cm/StepHelpers.groovy b/src/com/sap/piper/cm/StepHelpers.groovy index f0604d98f..cc0bfc27d 100644 --- a/src/com/sap/piper/cm/StepHelpers.groovy +++ b/src/com/sap/piper/cm/StepHelpers.groovy @@ -43,26 +43,35 @@ public class StepHelpers { if(changeDocumentId?.trim()) { step.echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from parameters." - - } else { - - step.echo "[INFO] Retrieving ChangeDocumentId from commit history [from: ${configuration.changeManagement.git.from}, to: ${configuration.changeManagement.git.to}]." + - "Searching for pattern '${configuration.changeManagement.changeDocumentLabel}'. Searching with format '${configuration.changeManagement.git.format}'." - - try { - changeDocumentId = cm.getChangeDocumentId( - configuration.changeManagement.git.from, - configuration.changeManagement.git.to, - configuration.changeManagement.changeDocumentLabel, - configuration.changeManagement.git.format - ) - - step.echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from commit history" - - } catch(ChangeManagementException ex) { - step.echo "[WARN] Cannot retrieve changeDocumentId from commit history: ${ex.getMessage()}." - } + return changeDocumentId } + + changeDocumentId = step.commonPipelineEnvironment.getChangeDocumentId() + + if(changeDocumentId?.trim()) { + + step.echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from common pipeline environment." + return changeDocumentId + } + + step.echo "[INFO] Retrieving ChangeDocumentId from commit history [from: ${configuration.changeManagement.git.from}, to: ${configuration.changeManagement.git.to}]." + + "Searching for pattern '${configuration.changeManagement.changeDocumentLabel}'. Searching with format '${configuration.changeManagement.git.format}'." + + try { + changeDocumentId = cm.getChangeDocumentId( + configuration.changeManagement.git.from, + configuration.changeManagement.git.to, + configuration.changeManagement.changeDocumentLabel, + configuration.changeManagement.git.format + ) + + step.echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from commit history" + step.commonPipelineEnvironment.setChangeDocumentId(changeDocumentId) + + } catch(ChangeManagementException ex) { + step.echo "[WARN] Cannot retrieve changeDocumentId from commit history: ${ex.getMessage()}." + } + return changeDocumentId } diff --git a/test/groovy/com/sap/piper/cm/StepHelpersTest.groovy b/test/groovy/com/sap/piper/cm/StepHelpersTest.groovy new file mode 100644 index 000000000..7cb6cf904 --- /dev/null +++ b/test/groovy/com/sap/piper/cm/StepHelpersTest.groovy @@ -0,0 +1,127 @@ +package com.sap.piper.cm + +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.RuleChain + +import util.BasePiperTest +import util.JenkinsLoggingRule +import util.Rules + +class StepHelpersTest extends BasePiperTest { + + // Configuration is not checked by the tests here. + // We simply assume it fits. It is the duty of the + // step related tests to ensure the configuration is valid. + def params = [changeManagement: + [git: [ + from: 'HEAD~1', + to: 'HEAD', + format: '%b' + ], + changeDocumentLabel: "ChangeDocument:" + ] + ] + + private Map getChangeDocumentIdReceivedParameters = [:] + + @Before + public void setup() { + getChangeDocumentIdReceivedParameters.clear() + } + + JenkinsLoggingRule jlr = new JenkinsLoggingRule(this) + + @Rule + public RuleChain rules = Rules.getCommonRules(this) + .around(jlr) + + private ChangeManagement cm = new ChangeManagement(nullScript) { + String getChangeDocumentId( + String from, + String to, + String label, + String format + ) { + getChangeDocumentIdReceivedParameters['from'] = from + getChangeDocumentIdReceivedParameters['to'] = to + getChangeDocumentIdReceivedParameters['label'] = label + getChangeDocumentIdReceivedParameters['format'] = format + return '001' + } + } + + @Test + public void changeDocumentIdViaCommitHistoryTest() { + + def changeDocumentId = StepHelpers.getChangeDocumentId(cm, nullScript, params) + + assert changeDocumentId == '001' + assert getChangeDocumentIdReceivedParameters == + [ + from: 'HEAD~1', + to: 'HEAD', + label: 'ChangeDocument:', + format: '%b' + ] + + // We cache the value. Otherwise we have to retrieve it each time from the + // commit history. + assert nullScript.commonPipelineEnvironment.getChangeDocumentId() == '001' + + } + + @Test + public void changeDocumentIdViaCommonPipelineEnvironmentTest() { + + nullScript.commonPipelineEnvironment.setChangeDocumentId('002') + def transportRequestId = StepHelpers.getChangeDocumentId(cm, nullScript, params) + + assert transportRequestId == '002' + + // getChangeDocumentId gets not called on ChangeManagement util class + // in this case. + assert getChangeDocumentIdReceivedParameters == [:] + } + + @Test + public void changeDocumentIdViaParametersTest() { + + params << [changeDocumentId: '003'] + + def transportRequestId = StepHelpers.getChangeDocumentId(cm, nullScript, params) + + assert transportRequestId == '003' + + // In case we get the change document id via parameters we do not cache it + // Caller knows the change document id anyway. So the caller can provide it with + // each call. + assert nullScript.commonPipelineEnvironment.getChangeDocumentId() == null + + // getChangeDocumentId gets not called on ChangeManagement util class + // in this case. + assert getChangeDocumentIdReceivedParameters == [:] + } + + @Test + public void changeDocumentIdNotProvidedTest() { + + jlr.expect('[WARN] Cannot retrieve changeDocumentId from commit history') + + def cm = new ChangeManagement(nullScript) { + String getChangeDocumentId( + String from, + String to, + String label, + String format + ) { + throw new ChangeManagementException('Cannot retrieve change document ids') + } + } + + def transportRequestId = StepHelpers.getChangeDocumentId(cm, nullScript, params) + + assert transportRequestId == null + } +} diff --git a/vars/commonPipelineEnvironment.groovy b/vars/commonPipelineEnvironment.groovy index 5685c2302..7d43536ee 100644 --- a/vars/commonPipelineEnvironment.groovy +++ b/vars/commonPipelineEnvironment.groovy @@ -30,6 +30,8 @@ class commonPipelineEnvironment implements Serializable { String mtarFilePath + String changeDocumentId + def reset() { appContainerProperties = [:] artifactVersion = null @@ -49,6 +51,8 @@ class commonPipelineEnvironment implements Serializable { influxCustomDataMap = [pipeline_data: [:], step_data: [:]] mtarFilePath = null + + changeDocumentId = null } def setAppContainerProperty(property, value) {