From 058c6c0d9ad7de7b035af03ed94146db12454404 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 28 Sep 2018 15:32:58 +0200 Subject: [PATCH] Condence common code for cm scenario --- src/com/sap/piper/cm/StepHelpers.groovy | 37 +++++++++++++++++++++++++ vars/transportRequestRelease.groovy | 27 ++---------------- vars/transportRequestUploadFile.groovy | 27 ++---------------- 3 files changed, 41 insertions(+), 50 deletions(-) create mode 100644 src/com/sap/piper/cm/StepHelpers.groovy diff --git a/src/com/sap/piper/cm/StepHelpers.groovy b/src/com/sap/piper/cm/StepHelpers.groovy new file mode 100644 index 000000000..bec9030ea --- /dev/null +++ b/src/com/sap/piper/cm/StepHelpers.groovy @@ -0,0 +1,37 @@ +package com.sap.piper.cm; + +import com.cloudbees.groovy.cps.NonCPS + +public class StepHelpers { + + @NonCPS + public static def getTransportRequestId(ChangeManagement cm, def step, Map configuration) { + + def transportRequestId = configuration.transportRequestId + + if(transportRequestId?.trim()) { + + step.echo "[INFO] Transport request id '${transportRequestId}' retrieved from parameters." + + } 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 + } +} diff --git a/vars/transportRequestRelease.groovy b/vars/transportRequestRelease.groovy index e5ecbcd9f..77fe9ccb9 100644 --- a/vars/transportRequestRelease.groovy +++ b/vars/transportRequestRelease.groovy @@ -9,6 +9,7 @@ import com.sap.piper.cm.ChangeManagementException import hudson.AbortException +import static com.sap.piper.cm.StepHelpers.getTransportRequestId @Field def STEP_NAME = 'transportRequestRelease' @@ -48,31 +49,7 @@ def call(parameters = [:]) { new Utils().pushToSWA([step: STEP_NAME], configuration) - def transportRequestId = configuration.transportRequestId - - if(transportRequestId?.trim()) { - - echo "[INFO] Transport request id '${transportRequestId}' retrieved from parameters." - - } else { - - echo "[INFO] Retrieving transport request id from commit history [from: ${configuration.changeManagement.git.from}, to: ${configuration.changeManagement.git.to}]." + - " Searching for pattern '${configuration.gitTransportRequestLabel}'. 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 - ) - - echo "[INFO] Transport request id '${transportRequestId}' retrieved from commit history" - - } catch(ChangeManagementException ex) { - echo "[WARN] Cannot retrieve transportRequestId from commit history: ${ex.getMessage()}." - } - } + def transportRequestId = getTransportRequestId(cm, this, configuration) def changeDocumentId = configuration.changeDocumentId diff --git a/vars/transportRequestUploadFile.groovy b/vars/transportRequestUploadFile.groovy index 98d388667..4507b7e9d 100644 --- a/vars/transportRequestUploadFile.groovy +++ b/vars/transportRequestUploadFile.groovy @@ -9,6 +9,7 @@ import com.sap.piper.cm.ChangeManagementException import hudson.AbortException +import static com.sap.piper.cm.StepHelpers.getTransportRequestId @Field def STEP_NAME = 'transportRequestUploadFile' @@ -80,31 +81,7 @@ def call(parameters = [:]) { } } - def transportRequestId = configuration.transportRequestId - - if(transportRequestId?.trim()) { - - echo "[INFO] Transport request id '${transportRequestId}' retrieved from parameters." - - } else { - - 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 - ) - - echo "[INFO] Transport request id '${transportRequestId}' retrieved from commit history" - - } catch(ChangeManagementException ex) { - echo "[WARN] Cannot retrieve transportRequestId from commit history: ${ex.getMessage()}." - } - } + def transportRequestId = getTransportRequestId(cm, this, configuration) configuration = configHelper .mixin([changeDocumentId: changeDocumentId?.trim() ?: null,