blob: 7b06fd1dcab1acec92cff3b36ae83462fddff61a [file] [log] [blame]
Oleg Gelbukhda64f342020-03-27 20:10:35 -07001package com.mirantis.mk
2
3/**
4 *
5 * Send HTTP request to JIRA REST API.
6 *
7 * @param uri (string) JIRA url to post message to
8 * @param auth (string) authentication data
9 * @param method (string) HTTP method to call. Default: GET
10 * @param message (string) payload to send to JIRA
11 *
12 * @return map with two elements:
13 * - responseCode
14 * - responseText
15 *
16**/
17def callREST (String uri, String auth, String method = 'GET', String message = null) {
18 String authEnc = auth.bytes.encodeBase64()
19 def req = new URL(uri).openConnection()
20 req.setRequestMethod(method)
21 req.setRequestProperty('Content-Type', 'application/json')
22 req.setRequestProperty('Authorization', "Basic ${authEnc}")
23 if (message) {
24 req.setDoOutput(true)
25 req.getOutputStream().write(message.getBytes('UTF-8'))
26 }
27 Integer responseCode = req.getResponseCode()
28 String responseText = ''
29 if (responseCode == 200) {
30 responseText = req.getInputStream().getText()
Alexandr Lovtsovb26ad202020-04-03 14:56:39 +030031 } else if (req.getErrorStream()) {
32 println "Request error: ${req.getErrorStream().getText()}"
Oleg Gelbukhda64f342020-03-27 20:10:35 -070033 }
34 req = null // to reset the connection
35 return [ 'responseCode': responseCode, 'responseText': responseText ]
36}
37
38/**
39 *
40 * Exract JIRA ticket numbers from the commit
41 * message for Gerrit change request.
42 *
43 * @param commitMsg string to parse for JIRA ticket IDs
44 * @param matcherRegex (string) regex to match JIRA issue IDs in commitMsg. Default: '([A-Z]+-[0-9]+)'
45 *
46 * @return list of JIRA ticket IDs
47 *
48**/
49
50List extractJIRA(String commitMsg, String matcherRegex = '([A-Z]+-[0-9]+)') {
Alexandr Lovtsov6b7e6ab2020-04-02 17:20:02 +030051 String msg = new String(commitMsg.decodeBase64())
Oleg Gelbukhda64f342020-03-27 20:10:35 -070052 def matcher = (msg =~ matcherRegex)
53 List tickets = []
54
55 matcher.each{ tickets.add(it[0]) }
56 return tickets
57}
58
59/**
60 *
61 * Post a text message in comments to a JIRA issue.
62 *
63 * @param uri (string) JIRA url to post message to
64 * @param auth (string) authentication data
Alexandr Lovtsovf8258c02020-04-02 20:13:05 +030065 * @param message (string) message to post to a JIRA issue as comment
Oleg Gelbukhda64f342020-03-27 20:10:35 -070066 *
67**/
68
Alexandr Lovtsovf8258c02020-04-02 20:13:05 +030069def postComment(String uri, String auth, String message) {
70 String messageBody = message.replace('"', '\\"').replace('\n', '\\n')
71 String payload = """{"body": "${messageBody}"}"""
72 callREST("${uri}/comment", auth, 'POST', payload)
Oleg Gelbukhda64f342020-03-27 20:10:35 -070073}
74
75/**
76 *
Alexandr Lovtsovb26ad202020-04-03 14:56:39 +030077 * Update Jira field
78 *
79 * @param uri (string) JIRA url to post message to
80 * @param auth (string) authentication data
81 * @param field (string) name of field to update
82 * @param message (string) json which should update given field. Format depends on field to be updated
83 *
84**/
85
86def updateField(String uri, String auth, String field, String message) {
87 String messageBody = message.replace('"', '\\"').replace('\n', '\\n')
88 String payload = """{"fields": { "${field}": "${messageBody}" }}"""
89 callREST("${uri}", auth, 'PUT', payload)
90}
91
92/**
93 *
Oleg Gelbukhda64f342020-03-27 20:10:35 -070094 * Post comment to list of JIRA issues.
95 *
96 * @param uri (string) base JIRA url, each ticket ID appends to it
97 * @param auth (string) authentication data
98 * @param message (string) payload to post to JIRA issues as comment
99 * @param tickets list of ticket IDs to post message to
100 *
101**/
102
103def postMessageToTickets(String uri, String auth, String message, List tickets) {
104 tickets.each{
105 if ( callREST("${uri}/${it}", auth)['responseCode'] == 200 ) {
Alexandr Lovtsovb26ad202020-04-03 14:56:39 +0300106 println "Add comment to ${uri}/${it} ...".replaceAll('rest/api/2/issue', 'browse')
Oleg Gelbukhda64f342020-03-27 20:10:35 -0700107 postComment("${uri}/${it}", auth, message)
108 }
109 }
110}
Alexandr Lovtsovb26ad202020-04-03 14:56:39 +0300111
112/**
113 *
114 * Update Jira field on given list of Jira issues
115 *
116 * @param uri (string) base JIRA url, each ticket ID appends to it
117 * @param auth (string) authentication data
118 * @param field (string) name of field to update
119 * @param message (string) json which should update given field. Format depends on field to be updated
120 * @param tickets list of ticket IDs to post message to
121 *
122**/
123
124def updateFieldOnTickets(String uri, String auth, String field, String message, List tickets) {
125 tickets.each{
126 if (callREST("${uri}/${it}", auth)['responseCode'] == 200 ) {
127 println "Update '${field}' field on ${uri}/${it} ...".replaceAll('rest/api/2/issue', 'browse')
128 updateField("${uri}/${it}", auth, field, message)
129 }
130 }
131}