2018-08-09 11:35:33 +02:00
import com.sap.piper.Utils
2018-06-20 11:46:28 +02:00
import groovy.transform.Field
2018-07-06 09:05:26 +02:00
import com.sap.piper.ConfigurationHelper
2018-06-20 11:46:28 +02:00
import com.sap.piper.cm.ChangeManagement
import com.sap.piper.cm.ChangeManagementException
import hudson.AbortException
@Field def STEP_NAME = 'transportRequestUploadFile'
@Field Set generalConfigurationKeys = [
2018-08-03 12:41:50 +02:00
'changeManagement'
2018-06-20 11:46:28 +02:00
]
2018-08-03 12:41:50 +02:00
@Field Set stepConfigurationKeys = generalConfigurationKeys . plus ( [
'applicationId'
] )
@Field Set parameterKeys = stepConfigurationKeys . plus ( [
2018-07-12 09:49:18 +02:00
'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 ]
2018-06-29 10:11:46 +02:00
ChangeManagement cm = parameters . cmUtils ? : new ChangeManagement ( script )
2018-06-20 11:46:28 +02:00
2018-10-17 11:05:20 +02:00
ConfigurationHelper configHelper = ConfigurationHelper . newInstance ( this )
2018-09-07 10:08:16 +02:00
. loadStepDefaults ( )
2018-08-15 10:37:34 +02:00
. 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 ( ) )
. withMandatoryProperty ( 'applicationId' )
. withMandatoryProperty ( 'changeManagement/changeDocumentLabel' )
. withMandatoryProperty ( 'changeManagement/clientOpts' )
. withMandatoryProperty ( 'changeManagement/credentialsId' )
. withMandatoryProperty ( 'changeManagement/endpoint' )
. withMandatoryProperty ( 'changeManagement/git/from' )
. withMandatoryProperty ( 'changeManagement/git/to' )
. withMandatoryProperty ( 'changeManagement/git/format' )
. withMandatoryProperty ( 'filePath' )
2018-07-13 09:28:41 +02:00
Map configuration = configHelper . use ( )
2018-06-20 11:46:28 +02:00
2018-08-09 11:35:33 +02:00
new Utils ( ) . pushToSWA ( [ step: STEP_NAME ] , configuration )
2018-06-28 08:46:23 +02:00
def changeDocumentId = configuration . changeDocumentId
2018-07-10 15:15:54 +02:00
if ( changeDocumentId ? . trim ( ) ) {
echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from parameters."
} else {
2018-07-17 09:21:56 +02:00
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}'."
2018-07-10 15:15:54 +02:00
try {
changeDocumentId = cm . getChangeDocumentId (
2018-07-17 09:21:56 +02:00
configuration . changeManagement . git . from ,
configuration . changeManagement . git . to ,
configuration . changeManagement . changeDocumentLabel ,
configuration . changeManagement . git . format
2018-07-10 15:15:54 +02:00
)
echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from commit history"
} catch ( ChangeManagementException ex ) {
echo "[WARN] Cannot retrieve changeDocumentId from commit history: ${ex.getMessage()}."
}
}
2018-07-12 13:55:01 +02:00
def transportRequestId = configuration . transportRequestId
if ( transportRequestId ? . trim ( ) ) {
echo "[INFO] Transport request id '${transportRequestId}' retrieved from parameters."
} else {
2018-07-31 10:25:32 +02:00
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}'."
2018-07-12 13:55:01 +02:00
try {
transportRequestId = cm . getTransportRequestId (
2018-07-31 10:25:32 +02:00
configuration . changeManagement . git . from ,
configuration . changeManagement . git . to ,
configuration . changeManagement . transportRequestLabel ,
configuration . changeManagement . git . format
2018-07-12 13:55:01 +02:00
)
echo "[INFO] Transport request id '${transportRequestId}' retrieved from commit history"
} catch ( ChangeManagementException ex ) {
echo "[WARN] Cannot retrieve transportRequestId from commit history: ${ex.getMessage()}."
}
}
2018-07-13 09:28:41 +02:00
configuration = configHelper
2018-07-16 11:04:20 +02:00
. mixin ( [ changeDocumentId: changeDocumentId ? . trim ( ) ? : null ,
transportRequestId: transportRequestId ? . trim ( ) ? : null ] , [ 'changeDocumentId' , 'transportRequestId' ] as Set )
2018-07-13 09:28:41 +02:00
. withMandatoryProperty ( 'changeDocumentId' ,
"Change document id not provided (parameter: \'changeDocumentId\' or via commit history)." )
2018-07-16 11:04:20 +02:00
. withMandatoryProperty ( 'transportRequestId' ,
"Transport request id not provided (parameter: \'transportRequestId\' or via commit history)." )
2018-07-13 09:28:41 +02:00
. use ( )
2018-06-20 11:46:28 +02:00
2018-07-12 08:54:04 +02:00
echo "[INFO] Uploading file '${configuration.filePath}' to transport request '${configuration.transportRequestId}' of change document '${configuration.changeDocumentId}'."
2018-06-20 11:46:28 +02:00
try {
2018-07-16 15:41:46 +02:00
2018-07-12 08:54:04 +02:00
cm . uploadFileToTransportRequest ( configuration . changeDocumentId ,
configuration . transportRequestId ,
configuration . applicationId ,
configuration . filePath ,
2018-07-17 09:21:56 +02:00
configuration . changeManagement . endpoint ,
2018-07-16 15:41:46 +02:00
configuration . changeManagement . credentialsId ,
2018-07-17 09:21:56 +02:00
configuration . changeManagement . clientOpts )
2018-07-16 15:41:46 +02:00
2018-06-20 11:46:28 +02:00
} catch ( ChangeManagementException ex ) {
throw new AbortException ( ex . getMessage ( ) )
}
2018-07-16 15:41:46 +02:00
2018-06-20 11:46:28 +02:00
2018-07-12 08:54:04 +02:00
echo "[INFO] File '${configuration.filePath}' has been successfully uploaded to transport request '${configuration.transportRequestId}' of change document '${configuration.changeDocumentId}'."
2018-06-20 11:46:28 +02:00
}
}