Skip to content

Instantly share code, notes, and snippets.

@Kirill
Last active August 11, 2022 14:53
Show Gist options
  • Save Kirill/86e8ed8d591fa6f0e567689804d85ef5 to your computer and use it in GitHub Desktop.
Save Kirill/86e8ed8d591fa6f0e567689804d85ef5 to your computer and use it in GitHub Desktop.
import groovy.json.JsonOutput
def call(Map config=[:]) {
def status = 'STARTED'
if (config.status) {
status = "${config.status}"
}
def emoji = ''
switch(status) {
case 'STARTED':
emoji = ':checkered_flag: '
break
case 'SUCCESSFUL':
emoji = ':circleacceptgreenmark: '
break
case 'ABORT':
emoji = ':large_blue_circle: '
break
case 'FAILURE':
emoji = ':circledenyredmark: '
break
default:
emoji = ''
break
}
def attachment = [
fallback: "You have new message Mattermost"
]
if (config.text) {
attachment["text"] = "${config.text}"
} else if (config.message) {
attachment["text"] = "${config.message}"
} else {
attachment["text"] = ""
}
def pretext = ""
if (config.pretext) {
pretext = "${config.pretext}"
} else {
pretext = emoji + "Job [${env.JOB_NAME} #${env.BUILD_NUMBER}](${env.BUILD_URL})"
}
attachment["pretext"] = "${pretext}"
if (config.color) {
attachment["color"] = "${config.color}"
}
if (config.title) {
attachment["title"] = "${config.title}"
}
if (config.footer) {
attachment["footer"] = "${config.footer}"
}
def channel = 'bot_test'
if (config.channel) {
channel = config.channel
}
def username = 'Jenkins'
if (config.username) {
username = config.username
}
def data = [
channel: "${channel}",
username: "${username}",
attachments: [attachment]
]
def json = JsonOutput.toJson(data)
try {
httpRequest (
customHeaders: [[name: 'User-Agent', value: 'svc_bot']],
url: config.endpoint,
contentType: 'APPLICATION_JSON',
httpMode: 'POST', consoleLogResponseBody: true,
requestBody: json,
validResponseCodes: "100:599"
)
} catch(Exception e) {
currentBuild.result = 'SUCCESS'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment