blob: ef2886205698b008bd912c4f46f60af00498b7b5 [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]+)') {
49 String msg = commitMsg.decodeBase64()
50 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
63 * @param message (string) payload to post to a JIRA issue as comment
64 * @param restrictPostTo string represents JIRA space name to which posting is restricted. Default: PRODX
65 *
66**/
67
68def postComment(String uri, String auth, String message, String restrictPostTo = 'PRODX') {
69 String messageBody = message.replace('"', '\\"')
70 String commentMsg = """{"body": "${messageBody}"}""".replace('\n', '\\n')
71 String jiraSpace = uri.split('/').last().split('-').first()
72
73 // Skip adding worklog entry for some namespaces
74 if ( jiraSpace == restrictPostTo ) {
75 callREST("${uri}/worklog", auth, 'POST', commentMsg)
76 }
77}
78
79/**
80 *
81 * Post comment to list of JIRA issues.
82 *
83 * @param uri (string) base JIRA url, each ticket ID appends to it
84 * @param auth (string) authentication data
85 * @param message (string) payload to post to JIRA issues as comment
86 * @param tickets list of ticket IDs to post message to
87 *
88**/
89
90def postMessageToTickets(String uri, String auth, String message, List tickets) {
91 tickets.each{
92 if ( callREST("${uri}/${it}", auth)['responseCode'] == 200 ) {
93 println "Updating ${uri}/${it} ...".replaceAll('rest/api/2/isse', 'browse')
94 postComment("${uri}/${it}", auth, message)
95 }
96 }
97}