blob: 6cc73bcb17bbca42505de9c443934897f1dbc7c9 [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()
47 if (buildStatusParam.toLowerCase().equals("success")) {
48 colorCode = "#00FF00"
49 colorName = "green"
50 } else if (buildStatusParam.toLowerCase().equals("unstable")) {
51 colorCode = "#FFFF00"
52 colorName = "yellow"
53 } else if (buildStatusParam.toLowerCase().equals("failure")) {
54 colorCode = "#FF0000"
55 colorName = "red"
56 }
57
58 queryString = 'payload={' +
vnaumove4f9dad2019-03-27 16:39:40 +010059 "'text':'${buildStatusParam.toUpperCase()}: Job <${buildUrlParam}|${jobNameParam} [${buildNumberParam}]>\\n ${description}', " +
Kirill Mashchenko0e4c04f2018-05-18 14:20:08 +030060 "'color':'${colorCode}'," +
61 "'pretext': '', " +
62 '"icon_url": "https://cfr.slack-edge.com/ae7f/img/services/jenkins-ci_192.png",' +
63 "'channel': '${channel}', " +
64 '}'
65 sendPostRequest(hook_url_parsed, queryString)
66
67}
68
69/*
70node {
71 jobResultNotification(
72 "success",
Kirill Mashchenko2fb0f972018-05-22 14:20:21 +030073 "#test_reclass_notify",
Kirill Mashchenko0e4c04f2018-05-18 14:20:08 +030074 "test-reclass-system",
75 44,
Kirill Mashchenko2fb0f972018-05-22 14:20:21 +030076 "https://ci.mcp.mirantis.net/",)
Kirill Mashchenko0e4c04f2018-05-18 14:20:08 +030077}
78*/