blob: 2f067e47a68162f49add66a9a881bcb6591d39dd [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()
31 }
32 req = null // to reset the connection
33 return [ 'responseCode': responseCode, 'responseText': responseText ]
34}
35
36/**
37 *
38 * Exract JIRA ticket numbers from the commit
39 * message for Gerrit change request.
40 *
41 * @param commitMsg string to parse for JIRA ticket IDs
42 * @param matcherRegex (string) regex to match JIRA issue IDs in commitMsg. Default: '([A-Z]+-[0-9]+)'
43 *
44 * @return list of JIRA ticket IDs
45 *
46**/
47
48List extractJIRA(String commitMsg, String matcherRegex = '([A-Z]+-[0-9]+)') {
Alexandr Lovtsov6b7e6ab2020-04-02 17:20:02 +030049 String msg = new String(commitMsg.decodeBase64())
Oleg Gelbukhda64f342020-03-27 20:10:35 -070050 def matcher = (msg =~ matcherRegex)
51 List tickets = []
52
53 matcher.each{ tickets.add(it[0]) }
54 return tickets
55}
56
57/**
58 *
59 * Post a text message in comments to a JIRA issue.
60 *
61 * @param uri (string) JIRA url to post message to
62 * @param auth (string) authentication data
Alexandr Lovtsovf8258c02020-04-02 20:13:05 +030063 * @param message (string) message to post to a JIRA issue as comment
Oleg Gelbukhda64f342020-03-27 20:10:35 -070064 *
65**/
66
Alexandr Lovtsovf8258c02020-04-02 20:13:05 +030067def postComment(String uri, String auth, String message) {
68 String messageBody = message.replace('"', '\\"').replace('\n', '\\n')
69 String payload = """{"body": "${messageBody}"}"""
70 callREST("${uri}/comment", auth, 'POST', payload)
Oleg Gelbukhda64f342020-03-27 20:10:35 -070071}
72
73/**
74 *
75 * Post comment to list of JIRA issues.
76 *
77 * @param uri (string) base JIRA url, each ticket ID appends to it
78 * @param auth (string) authentication data
79 * @param message (string) payload to post to JIRA issues as comment
80 * @param tickets list of ticket IDs to post message to
81 *
82**/
83
84def postMessageToTickets(String uri, String auth, String message, List tickets) {
85 tickets.each{
86 if ( callREST("${uri}/${it}", auth)['responseCode'] == 200 ) {
Alexandr Lovtsovf8258c02020-04-02 20:13:05 +030087 println "Updating ${uri}/${it} ...".replaceAll('rest/api/2/issue', 'browse')
Oleg Gelbukhda64f342020-03-27 20:10:35 -070088 postComment("${uri}/${it}", auth, message)
89 }
90 }
91}