Merge "update ceph pipline for refactored grains"
diff --git a/src/com/mirantis/mk/Atlassian.groovy b/src/com/mirantis/mk/Atlassian.groovy
index 2f067e4..33abced 100644
--- a/src/com/mirantis/mk/Atlassian.groovy
+++ b/src/com/mirantis/mk/Atlassian.groovy
@@ -28,6 +28,8 @@
     String responseText = ''
     if (responseCode == 200) {
         responseText = req.getInputStream().getText()
+    } else if (req.getErrorStream()) {
+        println "Request error: ${req.getErrorStream().getText()}"
     }
     req = null // to reset the connection
     return [ 'responseCode': responseCode, 'responseText': responseText ]
@@ -46,12 +48,18 @@
 **/
 
 List extractJIRA(String commitMsg, String matcherRegex = '([A-Z]+-[0-9]+)') {
-  String msg = new String(commitMsg.decodeBase64())
-  def matcher = (msg =~ matcherRegex)
-  List tickets = []
+    String msg
+    try {
+        msg = new String(commitMsg.decodeBase64())
+    } catch (e) {
+        // use commitMsg as is if cannot decode so we can use the same function for plaintext too
+        msg = commitMsg
+    }
+    def matcher = (msg =~ matcherRegex)
+    List tickets = []
 
-  matcher.each{ tickets.add(it[0]) }
-  return tickets
+    matcher.each{ tickets.add(it[0]) }
+    return tickets
 }
 
 /**
@@ -72,6 +80,23 @@
 
 /**
  *
+ * Update Jira field
+ *
+ * @param uri (string) JIRA url to post message to
+ * @param auth (string) authentication data
+ * @param field (string) name of field to update
+ * @param message (string) json which should update given field. Format depends on field to be updated
+ *
+**/
+
+def updateField(String uri, String auth, String field, String message) {
+    String messageBody = message.replace('"', '\\"').replace('\n', '\\n')
+    String payload = """{"fields": { "${field}": "${messageBody}" }}"""
+    callREST("${uri}", auth, 'PUT', payload)
+}
+
+/**
+ *
  * Post comment to list of JIRA issues.
  *
  * @param uri (string) base JIRA url, each ticket ID appends to it
@@ -84,8 +109,29 @@
 def postMessageToTickets(String uri, String auth, String message, List tickets) {
     tickets.each{
         if ( callREST("${uri}/${it}", auth)['responseCode'] == 200 ) {
-            println "Updating ${uri}/${it} ...".replaceAll('rest/api/2/issue', 'browse')
+            println "Add comment to ${uri}/${it} ...".replaceAll('rest/api/2/issue', 'browse')
             postComment("${uri}/${it}", auth, message)
         }
     }
 }
+
+/**
+ *
+ * Update Jira field on given list of Jira issues
+ *
+ * @param uri (string) base JIRA url, each ticket ID appends to it
+ * @param auth (string) authentication data
+ * @param field (string) name of field to update
+ * @param message (string) json which should update given field. Format depends on field to be updated
+ * @param tickets list of ticket IDs to post message to
+ *
+**/
+
+def updateFieldOnTickets(String uri, String auth, String field, String message, List tickets) {
+    tickets.each{
+        if (callREST("${uri}/${it}", auth)['responseCode'] == 200 ) {
+            println "Update '${field}' field on ${uri}/${it} ...".replaceAll('rest/api/2/issue', 'browse')
+            updateField("${uri}/${it}", auth, field, message)
+        }
+    }
+}