2018-06-26 13:00:48 +02:00
|
|
|
import com.sap.piper.GitUtils
|
|
|
|
import groovy.transform.Field
|
|
|
|
import hudson.AbortException
|
|
|
|
|
2018-07-06 09:05:26 +02:00
|
|
|
import com.sap.piper.ConfigurationHelper
|
2018-06-26 13:00:48 +02:00
|
|
|
import com.sap.piper.ConfigurationMerger
|
|
|
|
import com.sap.piper.cm.ChangeManagement
|
|
|
|
import com.sap.piper.cm.ChangeManagementException
|
|
|
|
|
|
|
|
@Field def STEP_NAME = 'checkChangeInDevelopment'
|
|
|
|
|
|
|
|
@Field Set parameterKeys = [
|
2018-06-25 15:23:56 +02:00
|
|
|
'changeDocumentId',
|
2018-06-25 12:37:33 +02:00
|
|
|
'cmClientOpts',
|
2018-06-26 13:00:48 +02:00
|
|
|
'credentialsId',
|
|
|
|
'endpoint',
|
|
|
|
'failIfStatusIsNotInDevelopment',
|
2018-07-09 14:52:30 +02:00
|
|
|
'gitFrom',
|
|
|
|
'gitTo',
|
2018-07-09 14:54:49 +02:00
|
|
|
'gitChangeDocumentLabel',
|
2018-07-09 14:52:30 +02:00
|
|
|
'gitFormat'
|
2018-06-26 13:00:48 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
@Field Set stepConfigurationKeys = [
|
2018-06-25 15:23:56 +02:00
|
|
|
'changeDocumentId',
|
2018-06-25 12:37:33 +02:00
|
|
|
'cmClientOpts',
|
2018-06-26 13:00:48 +02:00
|
|
|
'credentialsId',
|
|
|
|
'endpoint',
|
|
|
|
'failIfStatusIsNotInDevelopment',
|
2018-07-09 14:52:30 +02:00
|
|
|
'gitFrom',
|
|
|
|
'gitTo',
|
2018-07-09 14:54:49 +02:00
|
|
|
'gitChangeDocumentLabel',
|
2018-07-09 14:52:30 +02:00
|
|
|
'gitFormat'
|
2018-06-26 13:00:48 +02:00
|
|
|
]
|
|
|
|
|
2018-07-06 09:05:26 +02:00
|
|
|
@Field Set generalConfigurationKeys = stepConfigurationKeys
|
|
|
|
|
2018-06-26 13:00:48 +02:00
|
|
|
def call(parameters = [:]) {
|
|
|
|
|
|
|
|
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
|
|
|
|
|
2018-07-06 09:04:04 +02:00
|
|
|
def script = parameters.script ?: [commonPipelineEnvironment: commonPipelineEnvironment]
|
|
|
|
|
2018-06-26 13:00:48 +02:00
|
|
|
GitUtils gitUtils = parameters?.gitUtils ?: new GitUtils()
|
|
|
|
|
2018-07-06 09:04:04 +02:00
|
|
|
ChangeManagement cm = parameters?.cmUtils ?: new ChangeManagement(script, gitUtils)
|
2018-06-26 13:00:48 +02:00
|
|
|
|
2018-07-06 09:05:26 +02:00
|
|
|
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()
|
2018-06-26 13:00:48 +02:00
|
|
|
|
2018-07-10 11:09:20 +02:00
|
|
|
def changeId = configuration.changeDocumentId
|
|
|
|
|
|
|
|
if(changeId?.trim()) {
|
|
|
|
|
|
|
|
echo "[INFO] ChangeDocumentId retrieved from parameters."
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
echo "[INFO] Retrieving ChangeDocumentId from commit history [from: ${configuration.gitFrom}, to: ${configuration.gitTo}]." +
|
|
|
|
"Searching for pattern '${configuration.gitChangeDocumentLabel}'. Searching with format '${configuration.gitFormat}'."
|
|
|
|
|
|
|
|
try {
|
|
|
|
changeId = cm.getChangeDocumentId(
|
|
|
|
configuration.gitFrom,
|
|
|
|
configuration.gitTo,
|
|
|
|
configuration.gitChangeDocumentLabel,
|
|
|
|
configuration.gitFormat
|
|
|
|
)
|
|
|
|
if(changeId?.trim()) {
|
|
|
|
echo "[INFO] ChangeDocumentId '${changeId}' retrieved from commit history"
|
|
|
|
} else {
|
|
|
|
throw new ChangeManagementException("ChangeId is null or empty.")
|
|
|
|
}
|
|
|
|
} catch(ChangeManagementException ex) {
|
|
|
|
throw new AbortException(ex.getMessage())
|
2018-06-26 13:00:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isInDevelopment
|
|
|
|
|
|
|
|
echo "[INFO] Checking if change document '$changeId' is in development."
|
|
|
|
|
|
|
|
withCredentials([usernamePassword(
|
|
|
|
credentialsId: configuration.credentialsId,
|
|
|
|
passwordVariable: 'password',
|
|
|
|
usernameVariable: 'username')]) {
|
|
|
|
|
|
|
|
try {
|
2018-06-25 12:37:33 +02:00
|
|
|
isInDevelopment = cm.isChangeInDevelopment(changeId, configuration.endpoint, username, password, configuration.cmClientOpts)
|
2018-06-26 13:00:48 +02:00
|
|
|
} catch(ChangeManagementException ex) {
|
|
|
|
throw new AbortException(ex.getMessage())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isInDevelopment) {
|
|
|
|
echo "[INFO] Change '${changeId}' is in status 'in development'."
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
if(configuration.failIfStatusIsNotInDevelopment.toBoolean()) {
|
|
|
|
throw new AbortException("Change '${changeId}' is not in status 'in development'.")
|
|
|
|
|
|
|
|
} else {
|
|
|
|
echo "[WARNING] Change '${changeId}' is not in status 'in development'. Failing the pipeline has been explicitly disabled."
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|