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

93 lines
2.7 KiB
Groovy
Raw Normal View History

2018-06-26 13:00:48 +02:00
import com.sap.piper.GitUtils
import groovy.transform.Field
import hudson.AbortException
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 = [
'changeDocumentId',
'cmClientOpts',
2018-06-26 13:00:48 +02:00
'credentialsId',
'endpoint',
'failIfStatusIsNotInDevelopment',
'gitFrom',
'gitTo',
'gitChangeDocumentLabel',
'gitFormat'
2018-06-26 13:00:48 +02:00
]
@Field Set stepConfigurationKeys = [
'changeDocumentId',
'cmClientOpts',
2018-06-26 13:00:48 +02:00
'credentialsId',
'endpoint',
'failIfStatusIsNotInDevelopment',
'gitFrom',
'gitTo',
'gitChangeDocumentLabel',
'gitFormat'
2018-06-26 13:00:48 +02:00
]
def call(parameters = [:]) {
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
prepareDefaultValues script: this
GitUtils gitUtils = parameters?.gitUtils ?: new GitUtils()
ChangeManagement cm = parameters?.cmUtils ?: new ChangeManagement(parameters.script, gitUtils)
Map configuration = ConfigurationMerger.merge(parameters.script, STEP_NAME,
parameters, parameterKeys,
stepConfigurationKeys)
def changeId
try {
changeId = cm.getChangeDocumentId(configuration)
2018-06-26 13:00:48 +02:00
if(! changeId?.trim()) {
throw new ChangeManagementException("ChangeId is null or empty.")
}
} catch(ChangeManagementException ex) {
throw new AbortException(ex.getMessage())
}
boolean isInDevelopment
echo "[INFO] Checking if change document '$changeId' is in development."
withCredentials([usernamePassword(
credentialsId: configuration.credentialsId,
passwordVariable: 'password',
usernameVariable: 'username')]) {
try {
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
}
}
}
}