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 | 6b7e6ab | 2020-04-02 17:20:02 +0300 | [diff] [blame] | 51 | String msg = new String(commitMsg.decodeBase64()) |
Oleg Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 52 | def matcher = (msg =~ matcherRegex) |
| 53 | List tickets = [] |
| 54 | |
| 55 | matcher.each{ tickets.add(it[0]) } |
| 56 | return tickets |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * |
| 61 | * Post a text message in comments to a JIRA issue. |
| 62 | * |
| 63 | * @param uri (string) JIRA url to post message to |
| 64 | * @param auth (string) authentication data |
Alexandr Lovtsov | f8258c0 | 2020-04-02 20:13:05 +0300 | [diff] [blame] | 65 | * @param message (string) message to post to a JIRA issue as comment |
Oleg Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 66 | * |
| 67 | **/ |
| 68 | |
Alexandr Lovtsov | f8258c0 | 2020-04-02 20:13:05 +0300 | [diff] [blame] | 69 | def postComment(String uri, String auth, String message) { |
| 70 | String messageBody = message.replace('"', '\\"').replace('\n', '\\n') |
| 71 | String payload = """{"body": "${messageBody}"}""" |
| 72 | callREST("${uri}/comment", auth, 'POST', payload) |
Oleg Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * |
Alexandr Lovtsov | b26ad20 | 2020-04-03 14:56:39 +0300 | [diff] [blame^] | 77 | * Update Jira field |
| 78 | * |
| 79 | * @param uri (string) JIRA url to post message to |
| 80 | * @param auth (string) authentication data |
| 81 | * @param field (string) name of field to update |
| 82 | * @param message (string) json which should update given field. Format depends on field to be updated |
| 83 | * |
| 84 | **/ |
| 85 | |
| 86 | def updateField(String uri, String auth, String field, String message) { |
| 87 | String messageBody = message.replace('"', '\\"').replace('\n', '\\n') |
| 88 | String payload = """{"fields": { "${field}": "${messageBody}" }}""" |
| 89 | callREST("${uri}", auth, 'PUT', payload) |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * |
Oleg Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 94 | * Post comment to list of JIRA issues. |
| 95 | * |
| 96 | * @param uri (string) base JIRA url, each ticket ID appends to it |
| 97 | * @param auth (string) authentication data |
| 98 | * @param message (string) payload to post to JIRA issues as comment |
| 99 | * @param tickets list of ticket IDs to post message to |
| 100 | * |
| 101 | **/ |
| 102 | |
| 103 | def postMessageToTickets(String uri, String auth, String message, List tickets) { |
| 104 | tickets.each{ |
| 105 | if ( callREST("${uri}/${it}", auth)['responseCode'] == 200 ) { |
Alexandr Lovtsov | b26ad20 | 2020-04-03 14:56:39 +0300 | [diff] [blame^] | 106 | println "Add comment to ${uri}/${it} ...".replaceAll('rest/api/2/issue', 'browse') |
Oleg Gelbukh | da64f34 | 2020-03-27 20:10:35 -0700 | [diff] [blame] | 107 | postComment("${uri}/${it}", auth, message) |
| 108 | } |
| 109 | } |
| 110 | } |
Alexandr Lovtsov | b26ad20 | 2020-04-03 14:56:39 +0300 | [diff] [blame^] | 111 | |
| 112 | /** |
| 113 | * |
| 114 | * Update Jira field on given list of Jira issues |
| 115 | * |
| 116 | * @param uri (string) base JIRA url, each ticket ID appends to it |
| 117 | * @param auth (string) authentication data |
| 118 | * @param field (string) name of field to update |
| 119 | * @param message (string) json which should update given field. Format depends on field to be updated |
| 120 | * @param tickets list of ticket IDs to post message to |
| 121 | * |
| 122 | **/ |
| 123 | |
| 124 | def updateFieldOnTickets(String uri, String auth, String field, String message, List tickets) { |
| 125 | tickets.each{ |
| 126 | if (callREST("${uri}/${it}", auth)['responseCode'] == 200 ) { |
| 127 | println "Update '${field}' field on ${uri}/${it} ...".replaceAll('rest/api/2/issue', 'browse') |
| 128 | updateField("${uri}/${it}", auth, field, message) |
| 129 | } |
| 130 | } |
| 131 | } |