blob: 8aa0cfe6b2bae157e06edc74baba80637953f89d [file] [log] [blame]
package com.mirantis.mcp
/**
* Send POST request to url with some params
* @param urlString url
* @param paramString JSON string
*/
def sendPostRequest(String urlString, String paramString){
def url = new URL(urlString)
def conn = url.openConnection()
conn.setDoOutput(true)
def writer = new OutputStreamWriter(conn.getOutputStream())
writer.write(paramString)
writer.flush()
String line
def reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))
while ((line = reader.readLine()) != null) {
println line
}
writer.close()
reader.close()
}
/**
* Send slack notification
* @param buildStatusParam message type (success, warning, error)
* @param jobName job name param, if empty env.JOB_NAME will be used
* @param buildNumber build number param, if empty env.BUILD_NUM will be used
* @param buildUrl build url param, if empty env.BUILD_URL will be used
* @param channel param, default is '#mk-ci'
* @param credentialsId slack hook url credential, default is 'SLACK_WEBHOOK_URL'
* @param description Additional text related to Job result
*/
def jobResultNotification(String buildStatusParam, String channel = "#mk-ci",
String jobName=null,
Number buildNumber=null, String buildUrl=null,
String credentialsId="SLACK_WEBHOOK_URL",
String description='') {
def jobNameParam = jobName != null && jobName != "" ? jobName : env.JOB_NAME
def buildUrlParam = buildUrl != null && buildUrl != "" ? buildUrl : env.BUILD_URL
def buildNumberParam = buildNumber != null && buildNumber != "" ? buildNumber : env.BUILD_NUMBER
def common = new com.mirantis.mk.Common()
cred = common.getCredentialsById(credentialsId)
hook_url_parsed = cred.getSecret().toString()
switch(buildStatusParam.toLowerCase()) {
case "success":
colorCode = "#00FF00"
colorName = "green"
break
case "unstable":
colorCode = "#FFFF00"
colorName = "yellow"
break
case "failure":
colorCode = "#FF0000"
colorName = "red"
break
default:
colorCode = "#808080"
colorName = "grey"
}
queryString = 'payload={' +
"'text':'${buildStatusParam.toUpperCase()}: Job <${buildUrlParam}|${jobNameParam} [${buildNumberParam}]>\\n ${description}', " +
"'color':'${colorCode}'," +
"'pretext': '', " +
'"icon_url": "https://cfr.slack-edge.com/ae7f/img/services/jenkins-ci_192.png",' +
"'channel': '${channel}', " +
'}'
sendPostRequest(hook_url_parsed, queryString)
}
/*
node {
jobResultNotification(
"success",
"#test_reclass_notify",
"test-reclass-system",
44,
"https://ci.mcp.mirantis.net/",)
}
*/