blob: 33abcedfb1154c6a30e2329fda48ed15f6050a03 [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 Lovtsov3eb9fc92020-04-07 13:03:53 +030051 String msg
52 try {
53 msg = new String(commitMsg.decodeBase64())
54 } catch (e) {
55 // use commitMsg as is if cannot decode so we can use the same function for plaintext too
56 msg = commitMsg
57 }
58 def matcher = (msg =~ matcherRegex)
59 List tickets = []
Oleg Gelbukhda64f342020-03-27 20:10:35 -070060
Alexandr Lovtsov3eb9fc92020-04-07 13:03:53 +030061 matcher.each{ tickets.add(it[0]) }
62 return tickets
Oleg Gelbukhda64f342020-03-27 20:10:35 -070063}
64
65/**
66 *
67 * Post a text message in comments to a JIRA issue.
68 *
69 * @param uri (string) JIRA url to post message to
70 * @param auth (string) authentication data
Alexandr Lovtsovf8258c02020-04-02 20:13:05 +030071 * @param message (string) message to post to a JIRA issue as comment
Oleg Gelbukhda64f342020-03-27 20:10:35 -070072 *
73**/
74
Alexandr Lovtsovf8258c02020-04-02 20:13:05 +030075def postComment(String uri, String auth, String message) {
76 String messageBody = message.replace('"', '\\"').replace('\n', '\\n')
77 String payload = """{"body": "${messageBody}"}"""
78 callREST("${uri}/comment", auth, 'POST', payload)
Oleg Gelbukhda64f342020-03-27 20:10:35 -070079}
80
81/**
82 *
Alexandr Lovtsovb26ad202020-04-03 14:56:39 +030083 * Update Jira field
84 *
85 * @param uri (string) JIRA url to post message to
86 * @param auth (string) authentication data
87 * @param field (string) name of field to update
88 * @param message (string) json which should update given field. Format depends on field to be updated
89 *
90**/
91
92def updateField(String uri, String auth, String field, String message) {
93 String messageBody = message.replace('"', '\\"').replace('\n', '\\n')
94 String payload = """{"fields": { "${field}": "${messageBody}" }}"""
95 callREST("${uri}", auth, 'PUT', payload)
96}
97
98/**
99 *
Oleg Gelbukhda64f342020-03-27 20:10:35 -0700100 * Post comment to list of JIRA issues.
101 *
102 * @param uri (string) base JIRA url, each ticket ID appends to it
103 * @param auth (string) authentication data
104 * @param message (string) payload to post to JIRA issues as comment
105 * @param tickets list of ticket IDs to post message to
106 *
107**/
108
109def postMessageToTickets(String uri, String auth, String message, List tickets) {
110 tickets.each{
111 if ( callREST("${uri}/${it}", auth)['responseCode'] == 200 ) {
Alexandr Lovtsovb26ad202020-04-03 14:56:39 +0300112 println "Add comment to ${uri}/${it} ...".replaceAll('rest/api/2/issue', 'browse')
Oleg Gelbukhda64f342020-03-27 20:10:35 -0700113 postComment("${uri}/${it}", auth, message)
114 }
115 }
116}
Alexandr Lovtsovb26ad202020-04-03 14:56:39 +0300117
118/**
119 *
120 * Update Jira field on given list of Jira issues
121 *
122 * @param uri (string) base JIRA url, each ticket ID appends to it
123 * @param auth (string) authentication data
124 * @param field (string) name of field to update
125 * @param message (string) json which should update given field. Format depends on field to be updated
126 * @param tickets list of ticket IDs to post message to
127 *
128**/
129
130def updateFieldOnTickets(String uri, String auth, String field, String message, List tickets) {
131 tickets.each{
132 if (callREST("${uri}/${it}", auth)['responseCode'] == 200 ) {
133 println "Update '${field}' field on ${uri}/${it} ...".replaceAll('rest/api/2/issue', 'browse')
134 updateField("${uri}/${it}", auth, field, message)
135 }
136 }
137}