2019-02-13 17:45:35 +02:00
|
|
|
import static com.sap.piper.Prerequisites.checkScript
|
|
|
|
|
|
|
|
import com.sap.piper.ConfigurationHelper
|
2019-03-26 13:14:29 +02:00
|
|
|
import com.sap.piper.GenerateDocumentation
|
2019-02-13 17:45:35 +02:00
|
|
|
import com.sap.piper.Utils
|
|
|
|
import groovy.transform.Field
|
2019-10-23 13:38:31 +02:00
|
|
|
import groovy.text.GStringTemplateEngine
|
2019-02-13 17:45:35 +02:00
|
|
|
|
|
|
|
@Field String STEP_NAME = getClass().getName()
|
|
|
|
|
|
|
|
@Field Set GENERAL_CONFIG_KEYS = []
|
|
|
|
@Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS.plus([
|
2019-03-26 13:14:29 +02:00
|
|
|
/**
|
|
|
|
* Allows overriding the Slack Plugin Integration Base Url specified in the global configuration.
|
|
|
|
*/
|
2019-02-13 17:45:35 +02:00
|
|
|
'baseUrl',
|
2019-03-26 13:14:29 +02:00
|
|
|
/**
|
|
|
|
* Allows overriding of the default massaging channel from the plugin configuration.
|
|
|
|
*/
|
2019-02-13 17:45:35 +02:00
|
|
|
'channel',
|
2019-03-26 13:14:29 +02:00
|
|
|
/**
|
|
|
|
* Defines the message color`color` defines the message color.
|
|
|
|
* @possibleValues one of `good`, `warning`, `danger`, or any hex color code (eg. `#439FE0`)
|
|
|
|
*/
|
2019-02-13 17:45:35 +02:00
|
|
|
'color',
|
2019-03-26 13:14:29 +02:00
|
|
|
/**
|
|
|
|
* The credentials id for the Slack token.
|
|
|
|
* @possibleValues Jenkins credentials id
|
|
|
|
*/
|
2019-02-13 17:45:35 +02:00
|
|
|
'credentialsId',
|
2019-03-26 13:14:29 +02:00
|
|
|
/**
|
|
|
|
* Send a custom message into the Slack channel.
|
|
|
|
*/
|
2019-02-13 17:45:35 +02:00
|
|
|
'message'
|
|
|
|
])
|
|
|
|
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
|
|
|
|
|
2019-03-26 13:14:29 +02:00
|
|
|
/**
|
|
|
|
* Sends notifications to the Slack channel about the build status.
|
|
|
|
*
|
|
|
|
* Notification contains:
|
|
|
|
*
|
2019-05-22 17:11:15 +02:00
|
|
|
* * Build status
|
|
|
|
* * Repo Owner
|
|
|
|
* * Repo Name
|
|
|
|
* * Branch Name
|
|
|
|
* * Jenkins Build Number
|
|
|
|
* * Jenkins Build URL
|
2019-03-26 13:14:29 +02:00
|
|
|
*/
|
|
|
|
@GenerateDocumentation
|
2019-02-13 17:45:35 +02:00
|
|
|
void call(Map parameters = [:]) {
|
|
|
|
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
|
|
|
|
def utils = parameters.juStabUtils ?: new Utils()
|
|
|
|
def script = checkScript(this, parameters) ?: this
|
|
|
|
// load default & individual configuration
|
|
|
|
Map config = ConfigurationHelper.newInstance(this)
|
|
|
|
.loadStepDefaults()
|
|
|
|
.mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS)
|
|
|
|
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
|
|
|
|
.mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName?:env.STAGE_NAME, STEP_CONFIG_KEYS)
|
|
|
|
.mixin(parameters, PARAMETER_KEYS)
|
|
|
|
.use()
|
|
|
|
|
|
|
|
new Utils().pushToSWA([step: STEP_NAME], config)
|
|
|
|
|
|
|
|
def buildStatus = script.currentBuild.result
|
|
|
|
// resolve templates
|
2019-10-23 13:38:31 +02:00
|
|
|
config.color = GStringTemplateEngine.newInstance().createTemplate(config.color).make([buildStatus: buildStatus]).toString()
|
2019-02-13 17:45:35 +02:00
|
|
|
if (!config?.message){
|
|
|
|
if (!buildStatus) {
|
|
|
|
echo "[${STEP_NAME}] currentBuild.result is not set. Skipping Slack notification"
|
|
|
|
return
|
|
|
|
}
|
2019-10-23 13:38:31 +02:00
|
|
|
config.message = GStringTemplateEngine.newInstance().createTemplate(config.defaultMessage).make([buildStatus: buildStatus, env: env]).toString()
|
2019-02-13 17:45:35 +02:00
|
|
|
}
|
|
|
|
Map options = [:]
|
|
|
|
if(config.credentialsId)
|
|
|
|
options.put('tokenCredentialId', config.credentialsId)
|
2020-01-13 17:28:12 +02:00
|
|
|
for(String entry : ['baseUrl','channel','color','message'])
|
2019-02-13 17:45:35 +02:00
|
|
|
if(config.get(entry))
|
|
|
|
options.put(entry, config.get(entry))
|
|
|
|
slackSend(options)
|
|
|
|
}
|
|
|
|
}
|