From f88751feb122cc1b702d6397f03a1ab96f36b049 Mon Sep 17 00:00:00 2001 From: Roland Stengel Date: Mon, 27 Sep 2021 11:57:20 +0200 Subject: [PATCH] TransportRequestUploadFile migration to Go - Git commit scan (#3094) UploadFile requestid/changedocid retrieval - migrate Git commit history traversal to GO code --- src/com/sap/piper/cm/StepHelpers.groovy | 89 ++++++++- .../TransportRequestUploadFileTest.groovy | 64 ++++--- .../com/sap/piper/cm/StepHelpersTest.groovy | 175 ++++++++++++++++++ vars/transportRequestUploadFile.groovy | 4 +- 4 files changed, 304 insertions(+), 28 deletions(-) diff --git a/src/com/sap/piper/cm/StepHelpers.groovy b/src/com/sap/piper/cm/StepHelpers.groovy index 3f2e73681..ce5da9286 100644 --- a/src/com/sap/piper/cm/StepHelpers.groovy +++ b/src/com/sap/piper/cm/StepHelpers.groovy @@ -4,6 +4,7 @@ import com.cloudbees.groovy.cps.NonCPS public class StepHelpers { + @Deprecated // use go implementation instead public static def getTransportRequestId(ChangeManagement cm, def script, Map configuration) { def transportRequestId = configuration.transportRequestId @@ -43,8 +44,8 @@ public class StepHelpers { transportRequestId } + @Deprecated // use go implementation instead public static getChangeDocumentId(ChangeManagement cm, def script, Map configuration) { - def changeDocumentId = configuration.changeDocumentId if(changeDocumentId?.trim()) { @@ -82,6 +83,92 @@ public class StepHelpers { return changeDocumentId } + public static def getTransportRequestId(def script, Map configuration) { + + def transportRequestId = configuration.transportRequestId + + if(transportRequestId?.trim()) { + + script.echo "[INFO] transportRequestId '${transportRequestId}' retrieved from parameters." + return transportRequestId + + } + + transportRequestId = script.commonPipelineEnvironment.getValue('transportRequestId') + + if(transportRequestId?.trim()) { + script.echo "[INFO] transportRequestId '${transportRequestId}' retrieved from common pipeline environment." + return transportRequestId + } + + script.echo "[INFO] Retrieving transportRequestId from commit history [" + + "from: ${configuration.changeManagement.git.from}, " + + "to: ${configuration.changeManagement.git.to}]." + + "transportRequestLabel: '${configuration.changeManagement.transportRequestLabel}']." + + script.transportRequestReqIDFromGit(script: this, + gitFrom: configuration.changeManagement.git.from, + gitTo: configuration.changeManagement.git.to, + transportRequestLabel: configuration.changeManagement.transportRequestLabel + ) + + transportRequestId = script.commonPipelineEnvironment.getValue('transportRequestId') + if(transportRequestId != null) { + script.echo "[INFO] transportRequestId '${transportRequestId}' retrieved from commit history" + } + else{ + script.echo "[WARN] Cannot retrieve transportRequestId from commit history [" + + "from: ${configuration.changeManagement.git.from}, " + + "to: ${configuration.changeManagement.git.to}, " + + "transportRequestLabel: '${configuration.changeManagement.transportRequestLabel}']." + } + + return transportRequestId + } + + public static getChangeDocumentId(def script, Map configuration) { + def changeDocumentId = configuration.changeDocumentId + + if(changeDocumentId?.trim()) { + + script.echo "[INFO] changeDocumentId '${changeDocumentId}' retrieved from parameters." + return changeDocumentId + } + + changeDocumentId = script.commonPipelineEnvironment.getValue('changeDocumentId') + + if(changeDocumentId?.trim()) { + + script.echo "[INFO] changeDocumentId '${changeDocumentId}' retrieved from common pipeline environment." + return changeDocumentId + } + + script.echo "[INFO] Retrieving changeDocumentId from commit history [" + + "from: ${configuration.changeManagement.git.from}, " + + "to: ${configuration.changeManagement.git.to}, " + + "changeDocumentLabel: '${configuration.changeManagement.changeDocumentLabel}']." + + script.transportRequestDocIDFromGit(script: this, + gitFrom: configuration.changeManagement.git.from, + gitTo: configuration.changeManagement.git.to, + changeDocumentLabel: configuration.changeManagement.changeDocumentLabel + ) + + changeDocumentId = script.commonPipelineEnvironment.getValue('changeDocumentId') + + if(changeDocumentId == null) { + script.echo "[WARN] Cannot retrieve changeDocumentId from commit history [" + + "from: ${configuration.changeManagement.git.from}, " + + "to: ${configuration.changeManagement.git.to}, " + + "changeDocumentLabel: '${configuration.changeManagement.changeDocumentLabel}']." + } + else { + script.echo "[INFO] changeDocumentId '${changeDocumentId}' retrieved from commit history" + } + + return changeDocumentId + } + @NonCPS static BackendType getBackendTypeAndLogInfoIfCMIntegrationDisabled(def script, Map configuration) { diff --git a/test/groovy/TransportRequestUploadFileTest.groovy b/test/groovy/TransportRequestUploadFileTest.groovy index 12f36d5df..cbbd4991f 100644 --- a/test/groovy/TransportRequestUploadFileTest.groovy +++ b/test/groovy/TransportRequestUploadFileTest.groovy @@ -62,45 +62,59 @@ public class TransportRequestUploadFileTest extends BasePiperTest { @Test public void changeDocumentIdNotProvidedSOLMANTest() { - // we expect the failure only for SOLMAN (which is the default). - // Use case for CTS without change document id is checked by the - // straight forward test case for CTS + def calledWithParameters = null + + helper.registerAllowedMethod( 'piperExecuteBin', [Map, String, String, List], + { + params, stepName, metaData, creds -> + if(stepName.equals("transportRequestDocIDFromGit")) { + calledWithParameters = params + } + } + ) thrown.expect(IllegalArgumentException) thrown.expectMessage("Change document id not provided (parameter: 'changeDocumentId' provided to the step call or via commit history).") - ChangeManagement cm = new ChangeManagement(nullScript) { - String getChangeDocumentId( - String from, - String to, - String pattern, - String format - ) { - throw new ChangeManagementException('Cannot retrieve changeId from git commits.') - } - } + stepRule.step.transportRequestUploadFile(script: nullScript, transportRequestId: '001', applicationId: 'app', filePath: '/path', + changeManagement: [ + type: 'SOLMAN', + endpoint: 'https://example.org/cm', + clientOpts: '--client opts' + ], + credentialsId: 'CM' + ) - stepRule.step.transportRequestUploadFile(script: nullScript, transportRequestId: '001', applicationId: 'app', filePath: '/path', cmUtils: cm) + assert calledWithParameters != null } @Test public void transportRequestIdNotProvidedTest() { - ChangeManagement cm = new ChangeManagement(nullScript) { - String getTransportRequestId( - String from, - String to, - String pattern, - String format - ) { - throw new ChangeManagementException('Cannot retrieve transport request id from git commits.') - } - } + def calledWithParameters = null + + helper.registerAllowedMethod( 'piperExecuteBin', [Map, String, String, List], + { + params, stepName, metaData, creds -> + if(stepName.equals("transportRequestReqIDFromGit")) { + calledWithParameters = params + } + } + ) thrown.expect(IllegalArgumentException) thrown.expectMessage("Transport request id not provided (parameter: 'transportRequestId' provided to the step call or via commit history).") - stepRule.step.transportRequestUploadFile(script: nullScript, changeDocumentId: '001', applicationId: 'app', filePath: '/path', cmUtils: cm) + stepRule.step.transportRequestUploadFile(script: nullScript, changeDocumentId: '001', applicationId: 'app', filePath: '/path', + changeManagement: [ + type: 'SOLMAN', + endpoint: 'https://example.org/cm', + clientOpts: '--client opts' + ], + credentialsId: 'CM' + ) + + assert calledWithParameters != null } @Test diff --git a/test/groovy/com/sap/piper/cm/StepHelpersTest.groovy b/test/groovy/com/sap/piper/cm/StepHelpersTest.groovy index 63f07fc72..262a9de7a 100644 --- a/test/groovy/com/sap/piper/cm/StepHelpersTest.groovy +++ b/test/groovy/com/sap/piper/cm/StepHelpersTest.groovy @@ -158,6 +158,7 @@ class StepHelpersTest extends BasePiperTest { assert transportRequestId == null } + @Test public void changeDocumentIdViaCommonPipelineEnvironmentTest() { nullScript.commonPipelineEnvironment.setChangeDocumentId('002') @@ -209,4 +210,178 @@ class StepHelpersTest extends BasePiperTest { assert changeDocumentId == null } + + @Test + public void reqidViaConfigTest() { + + nullScript.commonPipelineEnvironment.setValue('transportRequestId', 'unused') + + def calledWithParameters = null + + helper.registerAllowedMethod( 'piperExecuteBin', [Map, String, String, List], + { + params, stepName, metaData, creds -> + if(stepName.equals("transportRequestReqIDFromGit")) { + calledWithParameters = params + } + } + ) + + def transportRequestId = StepHelpers.getTransportRequestId(nullScript, [transportRequestId: '100']) + + assert transportRequestId == '100' + assert calledWithParameters == null + } + + @Test + public void reqidViaCommonPipelineEnvironmentTest() { + + nullScript.commonPipelineEnvironment.setValue('transportRequestId', '200') + + def calledWithParameters = null + + helper.registerAllowedMethod( 'piperExecuteBin', [Map, String, String, List], + { + params, stepName, metaData, creds -> + if(stepName.equals("transportRequestReqIDFromGit")) { + calledWithParameters = params + } + } + ) + + def transportRequestId = StepHelpers.getTransportRequestId(nullScript, params) + + assert transportRequestId == '200' + assert calledWithParameters == null + } + + @Test + public void reqidViaCommitHistoryTest() { + + def calledWithParameters = null + + helper.registerAllowedMethod( 'piperExecuteBin', [Map, String, String, List], + { + params, stepName, metaData, creds -> + if(stepName.equals("transportRequestReqIDFromGit")) { + nullScript.commonPipelineEnvironment.setValue('transportRequestId', '300') + } + } + ) + + def transportRequestId = StepHelpers.getTransportRequestId(nullScript, params) + + assert transportRequestId == '300' + assert nullScript.commonPipelineEnvironment.getValue('transportRequestId') == '300' + } + + @Test + public void reqidNotProvidedTest() { + + def calledWithParameters = null + + helper.registerAllowedMethod( 'piperExecuteBin', [Map, String, String, List], + { + params, stepName, metaData, creds -> + if(stepName.equals("transportRequestReqIDFromGit")) { + calledWithParameters = params + } + } + ) + + loggingRule.expect('[INFO] Retrieving transportRequestId from commit history') + loggingRule.expect('[WARN] Cannot retrieve transportRequestId from commit history') + + def transportRequestId = StepHelpers.getTransportRequestId(nullScript, params) + + assert transportRequestId == null + assert calledWithParameters != null + } + + @Test + public void docidViaConfigTest() { + + nullScript.commonPipelineEnvironment.setValue('changeDocumentId', 'unused') + + def calledWithParameters = null + + helper.registerAllowedMethod( 'piperExecuteBin', [Map, String, String, List], + { + params, stepName, metaData, creds -> + if(stepName.equals("transportRequestDocIDFromGit")) { + calledWithParameters = params + } + } + ) + + def changeDocumentId = StepHelpers.getChangeDocumentId(nullScript, [changeDocumentId: '100']) + + assert changeDocumentId == '100' + assert calledWithParameters == null + } + + @Test + public void docidViaCommonPipelineEnvironmentTest() { + + nullScript.commonPipelineEnvironment.setValue('changeDocumentId', '200') + + def calledWithParameters = null + + helper.registerAllowedMethod( 'piperExecuteBin', [Map, String, String, List], + { + params, stepName, metaData, creds -> + if(stepName.equals("transportRequestDocIDFromGit")) { + calledWithParameters = params + } + } + ) + + def changeDocumentId = StepHelpers.getChangeDocumentId(nullScript, params) + + assert changeDocumentId == '200' + assert calledWithParameters == null + } + + @Test + public void docidViaCommitHistoryTest() { + + def calledWithParameters = null + + helper.registerAllowedMethod( 'piperExecuteBin', [Map, String, String, List], + { + params, stepName, metaData, creds -> + if(stepName.equals("transportRequestDocIDFromGit")) { + nullScript.commonPipelineEnvironment.setValue('changeDocumentId', '300') + } + } + ) + + def changeDocumentId = StepHelpers.getChangeDocumentId(nullScript, params) + + assert changeDocumentId == '300' + assert nullScript.commonPipelineEnvironment.getValue('changeDocumentId') == '300' + } + + @Test + public void docidNotProvidedTest() { + + def calledWithParameters = null + + helper.registerAllowedMethod( 'piperExecuteBin', [Map, String, String, List], + { + params, stepName, metaData, creds -> + if(stepName.equals("transportRequestDocIDFromGit")) { + calledWithParameters = params + } + } + ) + + loggingRule.expect('[INFO] Retrieving changeDocumentId from commit history') + loggingRule.expect('[WARN] Cannot retrieve changeDocumentId from commit history') + + def changeDocumentId = StepHelpers.getChangeDocumentId(nullScript, params) + + assert changeDocumentId == null + assert calledWithParameters != null + } } diff --git a/vars/transportRequestUploadFile.groovy b/vars/transportRequestUploadFile.groovy index 7fc7df43b..98e00cca8 100644 --- a/vars/transportRequestUploadFile.groovy +++ b/vars/transportRequestUploadFile.groovy @@ -211,10 +211,10 @@ void call(Map parameters = [:]) { def changeDocumentId = null if(backendType == BackendType.SOLMAN) { - changeDocumentId = getChangeDocumentId(cm, script, configuration) + changeDocumentId = getChangeDocumentId(script, configuration) } - def transportRequestId = getTransportRequestId(cm, script, configuration) + def transportRequestId = getTransportRequestId(script, configuration) configHelper .mixin([changeDocumentId: changeDocumentId?.trim() ?: null,