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() |
Alexandr Lovtsov | b26ad20 | 2020-04-03 14:56:39 +0300 | [diff] [blame] | 31 | } else if (req.getErrorStream()) { |
| 32 | println "Request error: ${req.getErrorStream().getText()}" |
Oleg Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 33 | } |
| 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 | |
| 50 | List extractJIRA(String commitMsg, String matcherRegex = '([A-Z]+-[0-9]+)') { |
Alexandr Lovtsov | 3eb9fc9 | 2020-04-07 13:03:53 +0300 | [diff] [blame] | 51 | 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 Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 60 | |
Alexandr Lovtsov | 3eb9fc9 | 2020-04-07 13:03:53 +0300 | [diff] [blame] | 61 | matcher.each{ tickets.add(it[0]) } |
| 62 | return tickets |
Oleg Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 63 | } |
| 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 Lovtsov | f8258c0 | 2020-04-02 20:13:05 +0300 | [diff] [blame] | 71 | * @param message (string) message to post to a JIRA issue as comment |
Oleg Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 72 | * |
| 73 | **/ |
| 74 | |
Alexandr Lovtsov | f8258c0 | 2020-04-02 20:13:05 +0300 | [diff] [blame] | 75 | def 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 Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * |
Alexandr Lovtsov | b26ad20 | 2020-04-03 14:56:39 +0300 | [diff] [blame] | 83 | * 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 | |
| 92 | def 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 Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 100 | * 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 | |
| 109 | def postMessageToTickets(String uri, String auth, String message, List tickets) { |
| 110 | tickets.each{ |
| 111 | if ( callREST("${uri}/${it}", auth)['responseCode'] == 200 ) { |
Alexandr Lovtsov | b26ad20 | 2020-04-03 14:56:39 +0300 | [diff] [blame] | 112 | println "Add comment to ${uri}/${it} ...".replaceAll('rest/api/2/issue', 'browse') |
Oleg Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 113 | postComment("${uri}/${it}", auth, message) |
| 114 | } |
| 115 | } |
| 116 | } |
Alexandr Lovtsov | b26ad20 | 2020-04-03 14:56:39 +0300 | [diff] [blame] | 117 | |
| 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 | |
| 130 | def 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 | } |