1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-11-24 08:32:32 +02:00
sap-jenkins-library/vars/slackSendNotification.groovy
Stephan Aßmus 8169d56ef7
Groovy: Load step defaults also from stages section in defaults (#1943)
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2020-08-26 15:32:58 +02:00

87 lines
3.0 KiB
Groovy

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