1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/vars/transportRequestRelease.groovy

108 lines
4.1 KiB
Groovy

import com.sap.piper.GitUtils
import groovy.transform.Field
import com.sap.piper.ConfigurationHelper
import com.sap.piper.ConfigurationMerger
import com.sap.piper.cm.ChangeManagement
import com.sap.piper.cm.ChangeManagementException
import hudson.AbortException
@Field def STEP_NAME = 'transportRequestRelease'
@Field Set parameterKeys = [
'changeDocumentId',
'cmClientOpts',
'transportRequestId',
'credentialsId',
'endpoint',
'gitFrom',
'gitTo',
'gitTransportRequestLabel',
'gitFormat'
]
@Field Set stepConfigurationKeys = [
'credentialsId',
'cmClientOpts',
'endpoint',
'gitFrom',
'gitTo',
'gitTransportRequestLabel',
'gitFormat'
]
@Field Set generalConfigurationKeys = stepConfigurationKeys
def call(parameters = [:]) {
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
def script = parameters?.script ?: [commonPipelineEnvironment: commonPipelineEnvironment]
ChangeManagement cm = parameters.cmUtils ?: new ChangeManagement(script)
Map configuration = ConfigurationHelper
.loadStepDefaults(this)
.mixinGeneralConfig(script.commonPipelineEnvironment, generalConfigurationKeys)
.mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName?:env.STAGE_NAME, stepConfigurationKeys)
.mixinStepConfig(script.commonPipelineEnvironment, stepConfigurationKeys)
.mixin(parameters, parameterKeys)
.use()
def changeDocumentId = configuration.changeDocumentId
if(!changeDocumentId) throw new AbortException("Change document id not provided (parameter: 'changeDocumentId').")
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.gitFrom}, to: ${configuration.gitTo}]." +
" Searching for pattern '${configuration.gitTransportRequestLabel}'. Searching with format '${configuration.gitFormat}'."
try {
transportRequestId = cm.getTransportRequestId(
configuration.gitFrom,
configuration.gitTo,
configuration.gitTransportRequestLabel,
configuration.gitFormat
)
echo "[INFO] Transport request id '${transportRequestId}' retrieved from commit history"
} catch(ChangeManagementException ex) {
echo "[WARN] Cannot retrieve transportRequestId from commit history: ${ex.getMessage()}."
}
}
if(!transportRequestId) throw new AbortException("Transport Request id not provided (parameter: 'transportRequestId' or via commit history).")
def credentialsId = configuration.credentialsId
if(!credentialsId) throw new AbortException("Credentials id not provided (parameter: 'credentialsId').")
def endpoint = configuration.endpoint
if(!endpoint) throw new AbortException("Solution Manager endpoint not provided (parameter: 'endpoint').")
echo "[INFO] Closing transport request '$transportRequestId' for change document '$changeDocumentId'."
withCredentials([usernamePassword(
credentialsId: credentialsId,
passwordVariable: 'password',
usernameVariable: 'username')]) {
try {
cm.releaseTransportRequest(changeDocumentId, transportRequestId, endpoint, username, password, configuration.cmClientOpts)
} catch(ChangeManagementException ex) {
throw new AbortException(ex.getMessage())
}
}
echo "[INFO] Transport Request '${transportRequestId}' has been successfully closed."
}
}