1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/vars/transportRequestUploadFile.groovy

167 lines
7.5 KiB
Groovy
Raw Normal View History

2018-06-20 11:46:28 +02:00
import com.sap.piper.GitUtils
import com.sap.piper.Utils
2018-06-20 11:46:28 +02:00
import groovy.transform.Field
import com.sap.piper.ConfigurationHelper
2018-06-20 11:46:28 +02:00
import com.sap.piper.ConfigurationMerger
import com.sap.piper.cm.ChangeManagement
import com.sap.piper.cm.BackendType
2018-06-20 11:46:28 +02:00
import com.sap.piper.cm.ChangeManagementException
import hudson.AbortException
import static com.sap.piper.cm.StepHelpers.getBackendTypeAndLogInfoIfCMIntegrationDisabled
2018-06-20 11:46:28 +02:00
@Field def STEP_NAME = 'transportRequestUploadFile'
@Field Set generalConfigurationKeys = [
'changeManagement'
2018-06-20 11:46:28 +02:00
]
@Field Set stepConfigurationKeys = generalConfigurationKeys.plus([
'applicationId'
])
@Field Set parameterKeys = stepConfigurationKeys.plus([
'changeDocumentId',
'filePath',
'transportRequestId'])
2018-06-20 11:46:28 +02:00
def call(parameters = [:]) {
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
def script = parameters?.script ?: [commonPipelineEnvironment: commonPipelineEnvironment]
ChangeManagement cm = parameters.cmUtils ?: new ChangeManagement(script)
2018-06-20 11:46:28 +02:00
ConfigurationHelper configHelper = ConfigurationHelper
.loadStepDefaults(this)
.mixinGeneralConfig(script.commonPipelineEnvironment, generalConfigurationKeys)
.mixinStepConfig(script.commonPipelineEnvironment, stepConfigurationKeys)
.mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName?:env.STAGE_NAME, stepConfigurationKeys)
.mixin(parameters, parameterKeys)
.addIfEmpty('filePath', script.commonPipelineEnvironment.getMtarFilePath())
Map configuration = configHelper.use()
2018-06-20 11:46:28 +02:00
BackendType backendType = getBackendTypeAndLogInfoIfCMIntegrationDisabled(this, configuration)
if(backendType == BackendType.NONE) return
2018-09-28 13:45:26 +02:00
configHelper
.withMandatoryProperty('changeManagement/changeDocumentLabel')
.withMandatoryProperty('changeManagement/clientOpts')
.withMandatoryProperty('changeManagement/credentialsId')
.withMandatoryProperty('changeManagement/endpoint')
.withMandatoryProperty('changeManagement/type')
.withMandatoryProperty('changeManagement/git/from')
.withMandatoryProperty('changeManagement/git/to')
.withMandatoryProperty('changeManagement/git/format')
.withMandatoryProperty('filePath')
new Utils().pushToSWA([step: STEP_NAME, stepParam1: configuration.changeManagement.type], configuration)
2018-09-18 14:49:06 +02:00
def changeDocumentId = null
if(backendType == BackendType.SOLMAN) {
2018-09-18 14:49:06 +02:00
changeDocumentId = configuration.changeDocumentId
2018-09-18 14:49:06 +02:00
if(changeDocumentId?.trim()) {
2018-09-18 14:49:06 +02:00
echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from parameters."
2018-09-18 14:49:06 +02:00
} else {
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
)
echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from commit history"
} catch(ChangeManagementException ex) {
echo "[WARN] Cannot retrieve changeDocumentId from commit history: ${ex.getMessage()}."
}
}
}
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()}."
}
}
2018-09-18 14:49:06 +02:00
configHelper
.mixin([changeDocumentId: changeDocumentId?.trim() ?: null,
transportRequestId: transportRequestId?.trim() ?: null], ['changeDocumentId', 'transportRequestId'] as Set)
if(backendType == BackendType.SOLMAN) {
2018-09-18 14:49:06 +02:00
configHelper
.withMandatoryProperty('changeDocumentId',
"Change document id not provided (parameter: \'changeDocumentId\' or via commit history).")
.withMandatoryProperty('applicationId')
2018-09-18 14:49:06 +02:00
}
configuration = configHelper
2018-09-18 14:49:06 +02:00
.withMandatoryProperty('transportRequestId',
"Transport request id not provided (parameter: \'transportRequestId\' or via commit history).")
.use()
2018-06-20 11:46:28 +02:00
2018-09-18 14:49:06 +02:00
def uploadingMessage = ["[INFO] Uploading file '${configuration.filePath}' to transport request '${configuration.transportRequestId}'"]
if(backendType == BackendType.SOLMAN)
2018-09-18 14:49:06 +02:00
uploadingMessage << " of change document '${configuration.changeDocumentId}'"
uploadingMessage << '.'
echo uploadingMessage.join()
2018-06-20 11:46:28 +02:00
try {
cm.uploadFileToTransportRequest(backendType,
configuration.changeDocumentId,
configuration.transportRequestId,
configuration.applicationId,
configuration.filePath,
configuration.changeManagement.endpoint,
configuration.changeManagement.credentialsId,
configuration.changeManagement.clientOpts)
2018-06-20 11:46:28 +02:00
} catch(ChangeManagementException ex) {
throw new AbortException(ex.getMessage())
}
2018-06-20 11:46:28 +02:00
2018-09-18 14:49:06 +02:00
def uploadedMessage = ["[INFO] File '${configuration.filePath}' has been successfully uploaded to transport request '${configuration.transportRequestId}'"]
if(backendType == BackendType.SOLMAN)
2018-09-18 14:49:06 +02:00
uploadedMessage << " of change document '${configuration.changeDocumentId}'"
uploadedMessage << '.'
echo uploadedMessage.join()
2018-06-20 11:46:28 +02:00
}
}