blob: 5cb51c8e4e8f2f0a86f5209477cf2ef9254a47ac [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'
32 */
33def jobResultNotification(String buildStatusParam, String jobName=null,
34 Number buildNumber=null, String buildUrl=null,
35 String channel = "#mk-ci",
36 String credentialsId="SLACK_WEBHOOK_URL") {
37 def jobNameParam = jobName != null && jobName != "" ? jobName : env.JOB_NAME
38 def buildUrlParam = buildUrl != null && buildUrl != "" ? buildUrl : env.BUILD_URL
39 def buildNumberParam = buildNumber != null && buildNumber != "" ? buildNumber : env.BUILD_NUMBER
40
41
42 def common = new com.mirantis.mk.Common()
43 cred = common.getCredentialsById(credentialsId)
44 hook_url_parsed = cred.getSecret().toString()
45 if (buildStatusParam.toLowerCase().equals("success")) {
46 colorCode = "#00FF00"
47 colorName = "green"
48 } else if (buildStatusParam.toLowerCase().equals("unstable")) {
49 colorCode = "#FFFF00"
50 colorName = "yellow"
51 } else if (buildStatusParam.toLowerCase().equals("failure")) {
52 colorCode = "#FF0000"
53 colorName = "red"
54 }
55
56 queryString = 'payload={' +
57 "'text':'${buildStatusParam.toUpperCase()}: Job <${buildUrlParam}|${jobNameParam} [${buildNumberParam}]>', " +
58 "'color':'${colorCode}'," +
59 "'pretext': '', " +
60 '"icon_url": "https://cfr.slack-edge.com/ae7f/img/services/jenkins-ci_192.png",' +
61 "'channel': '${channel}', " +
62 '}'
63 sendPostRequest(hook_url_parsed, queryString)
64
65}
66
67/*
68node {
69 jobResultNotification(
70 "success",
71 "test-reclass-system",
72 44,
73 "https://ci.mcp.mirantis.net/",
74 "#test_reclass_notify")
75}
76*/