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 = 'status' 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) httpRequest ( customHeaders: [[name: 'User-Agent', value: 'Jenkins']], url: "${config.endpoint}", contentType: 'APPLICATION_JSON', httpMode: 'POST', consoleLogResponseBody: true, requestBody: json, validResponseCodes: "100:599" ) }