diff --git a/documentation/docs/steps/transportRequestCreate.md b/documentation/docs/steps/transportRequestCreate.md index a5f44a865..a4b7fb2e3 100644 --- a/documentation/docs/steps/transportRequestCreate.md +++ b/documentation/docs/steps/transportRequestCreate.md @@ -7,6 +7,8 @@ Creates * a Transport Request for a Change Document on the Solution Manager (type `SOLMAN`) or * a Transport Request inside an ABAP system (type`CTS`) +The id of the transport request is availabe via [commonPipelineEnvironment.getTransportRequestId()](commonPipelineEnvironment.md) + ## Prerequisites * **[Change Management Client 2.0.0 or compatible version](http://central.maven.org/maven2/com/sap/devops/cmclient/dist.cli/)** - available for download on Maven Central. @@ -97,7 +99,7 @@ The parameters can also be provided when the step is invoked. For examples see b ## Return value -The id of the Transport Request that has been created. +none ## Exceptions diff --git a/src/com/sap/piper/cm/StepHelpers.groovy b/src/com/sap/piper/cm/StepHelpers.groovy index cc0bfc27d..7b0c21eef 100644 --- a/src/com/sap/piper/cm/StepHelpers.groovy +++ b/src/com/sap/piper/cm/StepHelpers.groovy @@ -4,57 +4,64 @@ import com.cloudbees.groovy.cps.NonCPS public class StepHelpers { - @NonCPS - public static def getTransportRequestId(ChangeManagement cm, def step, Map configuration) { + public static def getTransportRequestId(ChangeManagement cm, def script, Map configuration) { def transportRequestId = configuration.transportRequestId if(transportRequestId?.trim()) { - step.echo "[INFO] Transport request id '${transportRequestId}' retrieved from parameters." + script.echo "[INFO] Transport request id '${transportRequestId}' retrieved from parameters." + return transportRequestId - } else { - - step.echo "[INFO] Retrieving transport request id from commit history [from: ${configuration.changeManagement.git.from}, to: ${configuration.changeManagement.git.to}]." + - " Searching for pattern '${configuration.changeManagement.transportRequestLabel}'. Searching with format '${configuration.changeManagement.git.format}'." - - try { - transportRequestId = cm.getTransportRequestId( - configuration.changeManagement.git.from, - configuration.changeManagement.git.to, - configuration.changeManagement.transportRequestLabel, - configuration.changeManagement.git.format - ) - - step.echo "[INFO] Transport request id '${transportRequestId}' retrieved from commit history" - - } catch(ChangeManagementException ex) { - step.echo "[WARN] Cannot retrieve transportRequestId from commit history: ${ex.getMessage()}." - } } - return transportRequestId + + transportRequestId = script.commonPipelineEnvironment.getTransportRequestId() + + if(transportRequestId?.trim()) { + script.echo "[INFO] Transport request id '${transportRequestId}' retrieved from common pipeline environment." + return transportRequestId + } + + script.echo "[INFO] Retrieving transport request id from commit history [from: ${configuration.changeManagement.git.from}, to: ${configuration.changeManagement.git.to}]." + + " Searching for pattern '${configuration.changeManagement.transportRequestLabel}'. Searching with format '${configuration.changeManagement.git.format}'." + + try { + transportRequestId = cm.getTransportRequestId( + configuration.changeManagement.git.from, + configuration.changeManagement.git.to, + configuration.changeManagement.transportRequestLabel, + configuration.changeManagement.git.format + ) + + script.commonPipelineEnvironment.setTransportRequestId(transportRequestId) + script.echo "[INFO] Transport request id '${transportRequestId}' retrieved from commit history" + + } catch(ChangeManagementException ex) { + script.echo "[WARN] Cannot retrieve transportRequestId from commit history: ${ex.getMessage()}." + } + + transportRequestId } - @NonCPS - public static getChangeDocumentId(ChangeManagement cm, def step, Map configuration) { + public static getChangeDocumentId(ChangeManagement cm, def script, Map configuration) { def changeDocumentId = configuration.changeDocumentId if(changeDocumentId?.trim()) { - step.echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from parameters." + script.echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from parameters." return changeDocumentId } - changeDocumentId = step.commonPipelineEnvironment.getChangeDocumentId() + changeDocumentId = script.commonPipelineEnvironment.getChangeDocumentId() if(changeDocumentId?.trim()) { - step.echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from common pipeline environment." + script.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}]." + + script.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 { @@ -65,31 +72,31 @@ public class StepHelpers { configuration.changeManagement.git.format ) - step.echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from commit history" - step.commonPipelineEnvironment.setChangeDocumentId(changeDocumentId) + script.echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from commit history" + script.commonPipelineEnvironment.setChangeDocumentId(changeDocumentId) } catch(ChangeManagementException ex) { - step.echo "[WARN] Cannot retrieve changeDocumentId from commit history: ${ex.getMessage()}." + script.echo "[WARN] Cannot retrieve changeDocumentId from commit history: ${ex.getMessage()}." } return changeDocumentId } @NonCPS - static BackendType getBackendTypeAndLogInfoIfCMIntegrationDisabled(def step, Map configuration) { + static BackendType getBackendTypeAndLogInfoIfCMIntegrationDisabled(def script, Map configuration) { BackendType backendType try { backendType = configuration.changeManagement.type as BackendType } catch(IllegalArgumentException e) { - step.error "Invalid backend type: '${configuration.changeManagement.type}'. " + + script.error "Invalid backend type: '${configuration.changeManagement.type}'. " + "Valid values: [${BackendType.values().join(', ')}]. " + "Configuration: 'changeManagement/type'." } if (backendType == BackendType.NONE) { - step.echo "[INFO] Change management integration intentionally switched off. " + + script.echo "[INFO] Change management integration intentionally switched off. " + "In order to enable it provide 'changeManagement/type with one of " + "[${BackendType.values().minus(BackendType.NONE).join(', ')}] and maintain " + "other required properties like 'endpoint', 'credentialsId'." diff --git a/test/groovy/CommonStepsTest.groovy b/test/groovy/CommonStepsTest.groovy index da24a3c8f..77ac6137d 100644 --- a/test/groovy/CommonStepsTest.groovy +++ b/test/groovy/CommonStepsTest.groovy @@ -216,7 +216,6 @@ public class CommonStepsTest extends BasePiperTest{ def stepsWithCallMethodsOtherThanVoid = [] def whitelist = [ - 'transportRequestCreate', 'durationMeasure', ] diff --git a/test/groovy/TransportRequestCreateTest.groovy b/test/groovy/TransportRequestCreateTest.groovy index 8a9c0dc52..186c29994 100644 --- a/test/groovy/TransportRequestCreateTest.groovy +++ b/test/groovy/TransportRequestCreateTest.groovy @@ -127,9 +127,9 @@ public class TransportRequestCreateTest extends BasePiperTest { } } - def transportId = jsr.step.transportRequestCreate(script: nullScript, changeDocumentId: '001', developmentSystemId: '001', cmUtils: cm) + jsr.step.transportRequestCreate(script: nullScript, changeDocumentId: '001', developmentSystemId: '001', cmUtils: cm) - assert transportId == '001' + assert nullScript.commonPipelineEnvironment.getTransportRequestId() == '001' assert result == [changeId: '001', developmentSystemId: '001', cmEndpoint: 'https://example.org/cm', @@ -166,14 +166,14 @@ public class TransportRequestCreateTest extends BasePiperTest { } } - def transportId = jsr.step.transportRequestCreate(script: nullScript, - transportType: 'W', - targetSystem: 'XYZ', - description: 'desc', - changeManagement: [type: 'CTS'], - cmUtils: cm) + jsr.step.call(script: nullScript, + transportType: 'W', + targetSystem: 'XYZ', + description: 'desc', + changeManagement: [type: 'CTS'], + cmUtils: cm) - assert transportId == '001' + assert nullScript.commonPipelineEnvironment.getTransportRequestId() == '001' assert result == [transportType: 'W', targetSystemId: 'XYZ', description: 'desc', diff --git a/test/groovy/TransportRequestReleaseTest.groovy b/test/groovy/TransportRequestReleaseTest.groovy index 2f740d180..7a41d8f91 100644 --- a/test/groovy/TransportRequestReleaseTest.groovy +++ b/test/groovy/TransportRequestReleaseTest.groovy @@ -108,6 +108,10 @@ public class TransportRequestReleaseTest extends BasePiperTest { @Test public void releaseTransportRequestSuccessTest() { + // Here we test only the case where the transportRequestId is + // provided via parameters. The other cases are tested by + // corresponding tests for StepHelpers#getTransportRequestId(./.) + jlr.expect("[INFO] Closing transport request '002' for change document '001'.") jlr.expect("[INFO] Transport Request '002' has been successfully closed.") diff --git a/test/groovy/TransportRequestUploadFileTest.groovy b/test/groovy/TransportRequestUploadFileTest.groovy index 772ba71fe..ed42e61de 100644 --- a/test/groovy/TransportRequestUploadFileTest.groovy +++ b/test/groovy/TransportRequestUploadFileTest.groovy @@ -194,6 +194,10 @@ public class TransportRequestUploadFileTest extends BasePiperTest { @Test public void uploadFileToTransportRequestSOLMANSuccessTest() { + // Here we test only the case where the transportRequestId is + // provided via parameters. The other cases are tested by + // corresponding tests for StepHelpers#getTransportRequestId(./.) + jlr.expect("[INFO] Uploading file '/path' to transport request '002' of change document '001'.") jlr.expect("[INFO] File '/path' has been successfully uploaded to transport request '002' of change document '001'.") diff --git a/test/groovy/com/sap/piper/cm/StepHelpersTest.groovy b/test/groovy/com/sap/piper/cm/StepHelpersTest.groovy index 7cb6cf904..2df34c4d3 100644 --- a/test/groovy/com/sap/piper/cm/StepHelpersTest.groovy +++ b/test/groovy/com/sap/piper/cm/StepHelpersTest.groovy @@ -20,15 +20,18 @@ class StepHelpersTest extends BasePiperTest { to: 'HEAD', format: '%b' ], - changeDocumentLabel: "ChangeDocument:" + changeDocumentLabel: "ChangeDocument:", + transportRequestLabel: "TransportRequest:", ] ] private Map getChangeDocumentIdReceivedParameters = [:] + private Map getTransportRequestIdReceivedParameters = [:] @Before public void setup() { getChangeDocumentIdReceivedParameters.clear() + getTransportRequestIdReceivedParameters.clear() } JenkinsLoggingRule jlr = new JenkinsLoggingRule(this) @@ -38,6 +41,20 @@ class StepHelpersTest extends BasePiperTest { .around(jlr) private ChangeManagement cm = new ChangeManagement(nullScript) { + + String getTransportRequestId( + String from, + String to, + String label, + String format + ) { + getTransportRequestIdReceivedParameters['from'] = from + getTransportRequestIdReceivedParameters['to'] = to + getTransportRequestIdReceivedParameters['label'] = label + getTransportRequestIdReceivedParameters['format'] = format + return '097' + } + String getChangeDocumentId( String from, String to, @@ -53,6 +70,24 @@ class StepHelpersTest extends BasePiperTest { } @Test + public void transportRequestIdViaCommitHistoryTest() { + + def transportRequestId = StepHelpers.getTransportRequestId(cm, nullScript, params) + + assert transportRequestId == '097' + assert getTransportRequestIdReceivedParameters == + [ + from: 'HEAD~1', + to: 'HEAD', + label: 'TransportRequest:', + format: '%b' + ] + + // We cache the value. Otherwise we have to retrieve it each time from the + // commit history. + assert nullScript.commonPipelineEnvironment.getTransportRequestId() == '097' + } + public void changeDocumentIdViaCommitHistoryTest() { def changeDocumentId = StepHelpers.getChangeDocumentId(cm, nullScript, params) @@ -69,10 +104,60 @@ class StepHelpersTest extends BasePiperTest { // We cache the value. Otherwise we have to retrieve it each time from the // commit history. assert nullScript.commonPipelineEnvironment.getChangeDocumentId() == '001' - } @Test + public void transportRequestIdViaCommonPipelineEnvironmentTest() { + + nullScript.commonPipelineEnvironment.setTransportRequestId('098') + def transportRequestId = StepHelpers.getTransportRequestId(cm, nullScript, params) + + assert transportRequestId == '098' + + // getTransportRequestId gets not called on ChangeManagement util class + // in this case. + assert getTransportRequestIdReceivedParameters == [:] + } + + @Test + public void transportRequestIdViaParametersTest() { + + params << [transportRequestId: '099'] + + def transportRequestId = StepHelpers.getTransportRequestId(cm, nullScript, params) + + assert transportRequestId == '099' + + // In case we get the transport request id via parameters we do not cache it + // Caller knows the transport request id anyway. So the caller can provide it with + // each call. + assert nullScript.commonPipelineEnvironment.getTransportRequestId() == null + + // getTransportRequestId gets not called on ChangeManagement util class + // in this case. + assert getTransportRequestIdReceivedParameters == [:] + } + + @Test + public void transportRequestIdNotProvidedTest() { + + jlr.expect('Cannot retrieve transportRequestId from commit history') + + def cm = new ChangeManagement(nullScript) { + String getTransportRequestId( + String from, + String to, + String label, + String format + ) { + throw new ChangeManagementException('Cannot retrieve transport request id') + } + } + + def transportRequestId = StepHelpers.getTransportRequestId(cm, nullScript, params) + assert transportRequestId == null + } + public void changeDocumentIdViaCommonPipelineEnvironmentTest() { nullScript.commonPipelineEnvironment.setChangeDocumentId('002') @@ -120,8 +205,8 @@ class StepHelpersTest extends BasePiperTest { } } - def transportRequestId = StepHelpers.getChangeDocumentId(cm, nullScript, params) + def changeDocumentId = StepHelpers.getChangeDocumentId(cm, nullScript, params) - assert transportRequestId == null + assert changeDocumentId == null } } diff --git a/vars/checkChangeInDevelopment.groovy b/vars/checkChangeInDevelopment.groovy index e63b489be..51413f232 100644 --- a/vars/checkChangeInDevelopment.groovy +++ b/vars/checkChangeInDevelopment.groovy @@ -97,7 +97,7 @@ void call(parameters = [:]) { new Utils().pushToSWA([step: STEP_NAME, stepParam1: parameters?.script == null], configuration) - def changeId = getChangeDocumentId(cm, this, configuration) + def changeId = getChangeDocumentId(cm, script, configuration) configuration = configHelper.mixin([changeDocumentId: changeId?.trim() ?: null], ['changeDocumentId'] as Set) diff --git a/vars/commonPipelineEnvironment.groovy b/vars/commonPipelineEnvironment.groovy index 7d43536ee..89e34fd74 100644 --- a/vars/commonPipelineEnvironment.groovy +++ b/vars/commonPipelineEnvironment.groovy @@ -30,6 +30,7 @@ class commonPipelineEnvironment implements Serializable { String mtarFilePath + String transportRequestId String changeDocumentId def reset() { @@ -52,6 +53,7 @@ class commonPipelineEnvironment implements Serializable { mtarFilePath = null + transportRequestId = null changeDocumentId = null } diff --git a/vars/neoDeploy.groovy b/vars/neoDeploy.groovy index 75cabb565..bddb754be 100644 --- a/vars/neoDeploy.groovy +++ b/vars/neoDeploy.groovy @@ -190,9 +190,12 @@ void call(parameters = [:]) { """--user '${username}' \ --password '${password}' \ """ - dockerExecute(dockerImage: configuration.get('dockerImage'), - dockerEnvVars: configuration.get('dockerEnvVars'), - dockerOptions: configuration.get('dockerOptions')) { + dockerExecute( + script: script, + dockerImage: configuration.get('dockerImage'), + dockerEnvVars: configuration.get('dockerEnvVars'), + dockerOptions: configuration.get('dockerOptions') + ) { neo.verify(this, configuration) diff --git a/vars/transportRequestCreate.groovy b/vars/transportRequestCreate.groovy index 1fec7d580..cfa9ba023 100644 --- a/vars/transportRequestCreate.groovy +++ b/vars/transportRequestCreate.groovy @@ -27,7 +27,7 @@ import hudson.AbortException @Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS.plus(['changeDocumentId']) -def call(parameters = [:]) { +void call(parameters = [:]) { def transportRequestId @@ -68,7 +68,7 @@ def call(parameters = [:]) { if(backendType == BackendType.SOLMAN) { - changeDocumentId = getChangeDocumentId(cm, this, configuration) + changeDocumentId = getChangeDocumentId(cm, script, configuration) configHelper.mixin([changeDocumentId: changeDocumentId?.trim() ?: null], ['changeDocumentId'] as Set) .withMandatoryProperty('developmentSystemId') @@ -110,7 +110,6 @@ def call(parameters = [:]) { echo "[INFO] Transport Request '$transportRequestId' has been successfully created." + script.commonPipelineEnvironment.setTransportRequestId(transportRequestId) } - - return transportRequestId } diff --git a/vars/transportRequestRelease.groovy b/vars/transportRequestRelease.groovy index 9c28d7dcd..d11903f3e 100644 --- a/vars/transportRequestRelease.groovy +++ b/vars/transportRequestRelease.groovy @@ -62,11 +62,11 @@ void call(parameters = [:]) { stepParam1: parameters?.script == null], configuration) def changeDocumentId = null - def transportRequestId = getTransportRequestId(cm, this, configuration) + def transportRequestId = getTransportRequestId(cm, script, configuration) if(backendType == BackendType.SOLMAN) { - changeDocumentId = getChangeDocumentId(cm, this, configuration) + changeDocumentId = getChangeDocumentId(cm, script, configuration) configHelper.mixin([changeDocumentId: changeDocumentId?.trim() ?: null], ['changeDocumentId'] as Set) .withMandatoryProperty('changeDocumentId', diff --git a/vars/transportRequestUploadFile.groovy b/vars/transportRequestUploadFile.groovy index 420b59592..34eef2ff7 100644 --- a/vars/transportRequestUploadFile.groovy +++ b/vars/transportRequestUploadFile.groovy @@ -68,10 +68,10 @@ void call(parameters = [:]) { def changeDocumentId = null if(backendType == BackendType.SOLMAN) { - changeDocumentId = getChangeDocumentId(cm, this, configuration) + changeDocumentId = getChangeDocumentId(cm, script, configuration) } - def transportRequestId = getTransportRequestId(cm, this, configuration) + def transportRequestId = getTransportRequestId(cm, script, configuration) configHelper .mixin([changeDocumentId: changeDocumentId?.trim() ?: null,