Oleg Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 1 | package 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 | **/ |
| 17 | def 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 | |
| 48 | List extractJIRA(String commitMsg, String matcherRegex = '([A-Z]+-[0-9]+)') { |
Alexandr Lovtsov | 6b7e6ab | 2020-04-02 17:20:02 +0300 | [diff] [blame] | 49 | String msg = new String(commitMsg.decodeBase64()) |
Oleg Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 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 |
Alexandr Lovtsov | f8258c0 | 2020-04-02 20:13:05 +0300 | [diff] [blame^] | 63 | * @param message (string) message to post to a JIRA issue as comment |
Oleg Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 64 | * |
| 65 | **/ |
| 66 | |
Alexandr Lovtsov | f8258c0 | 2020-04-02 20:13:05 +0300 | [diff] [blame^] | 67 | def 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 Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 71 | } |
| 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 | |
| 84 | def postMessageToTickets(String uri, String auth, String message, List tickets) { |
| 85 | tickets.each{ |
| 86 | if ( callREST("${uri}/${it}", auth)['responseCode'] == 200 ) { |
Alexandr Lovtsov | f8258c0 | 2020-04-02 20:13:05 +0300 | [diff] [blame^] | 87 | println "Updating ${uri}/${it} ...".replaceAll('rest/api/2/issue', 'browse') |
Oleg Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 88 | postComment("${uri}/${it}", auth, message) |
| 89 | } |
| 90 | } |
| 91 | } |