blob: 8aa0cfe6b2bae157e06edc74baba80637953f89d [file] [log] [blame]
Kirill Mashchenko0e4c04f2018-05-18 14:20:08 +03001package com.mirantis.mcp
2
3/**
4 * Send POST request to url with some params
5 * @param urlString url
6 * @param paramString JSON string
7 */
8def sendPostRequest(String urlString, String paramString){
9 def url = new URL(urlString)
10 def conn = url.openConnection()
11 conn.setDoOutput(true)
12 def writer = new OutputStreamWriter(conn.getOutputStream())
13 writer.write(paramString)
14 writer.flush()
15 String line
16 def reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))
17 while ((line = reader.readLine()) != null) {
18 println line
19 }
20 writer.close()
21 reader.close()
22}
23
24/**
25 * Send slack notification
26 * @param buildStatusParam message type (success, warning, error)
27 * @param jobName job name param, if empty env.JOB_NAME will be used
28 * @param buildNumber build number param, if empty env.BUILD_NUM will be used
29 * @param buildUrl build url param, if empty env.BUILD_URL will be used
30 * @param channel param, default is '#mk-ci'
31 * @param credentialsId slack hook url credential, default is 'SLACK_WEBHOOK_URL'
vnaumove4f9dad2019-03-27 16:39:40 +010032 * @param description Additional text related to Job result
Kirill Mashchenko0e4c04f2018-05-18 14:20:08 +030033 */
Kirill Mashchenko2fb0f972018-05-22 14:20:21 +030034def jobResultNotification(String buildStatusParam, String channel = "#mk-ci",
35 String jobName=null,
Kirill Mashchenko0e4c04f2018-05-18 14:20:08 +030036 Number buildNumber=null, String buildUrl=null,
vnaumove4f9dad2019-03-27 16:39:40 +010037 String credentialsId="SLACK_WEBHOOK_URL",
38 String description='') {
Kirill Mashchenko0e4c04f2018-05-18 14:20:08 +030039 def jobNameParam = jobName != null && jobName != "" ? jobName : env.JOB_NAME
40 def buildUrlParam = buildUrl != null && buildUrl != "" ? buildUrl : env.BUILD_URL
41 def buildNumberParam = buildNumber != null && buildNumber != "" ? buildNumber : env.BUILD_NUMBER
42
43
44 def common = new com.mirantis.mk.Common()
45 cred = common.getCredentialsById(credentialsId)
46 hook_url_parsed = cred.getSecret().toString()
vnaumovea2a8d32020-03-13 16:53:10 +010047
vnaumovefa93102020-03-16 10:39:19 +010048 switch(buildStatusParam.toLowerCase()) {
vnaumovea2a8d32020-03-13 16:53:10 +010049 case "success":
50 colorCode = "#00FF00"
51 colorName = "green"
52 break
53 case "unstable":
54 colorCode = "#FFFF00"
55 colorName = "yellow"
56 break
57 case "failure":
58 colorCode = "#FF0000"
59 colorName = "red"
60 break
61 default:
62 colorCode = "#808080"
63 colorName = "grey"
Kirill Mashchenko0e4c04f2018-05-18 14:20:08 +030064 }
65
66 queryString = 'payload={' +
vnaumove4f9dad2019-03-27 16:39:40 +010067 "'text':'${buildStatusParam.toUpperCase()}: Job <${buildUrlParam}|${jobNameParam} [${buildNumberParam}]>\\n ${description}', " +
Kirill Mashchenko0e4c04f2018-05-18 14:20:08 +030068 "'color':'${colorCode}'," +
69 "'pretext': '', " +
70 '"icon_url": "https://cfr.slack-edge.com/ae7f/img/services/jenkins-ci_192.png",' +
71 "'channel': '${channel}', " +
72 '}'
73 sendPostRequest(hook_url_parsed, queryString)
74
75}
76
77/*
78node {
79 jobResultNotification(
80 "success",
Kirill Mashchenko2fb0f972018-05-22 14:20:21 +030081 "#test_reclass_notify",
Kirill Mashchenko0e4c04f2018-05-18 14:20:08 +030082 "test-reclass-system",
83 44,
Kirill Mashchenko2fb0f972018-05-22 14:20:21 +030084 "https://ci.mcp.mirantis.net/",)
Kirill Mashchenko0e4c04f2018-05-18 14:20:08 +030085}
86*/