Snippets

Adaptavist Script Listiner Jira Cloud Extract Text From The Last Comment

Updated by Kristian Walker

File ScriptListinerJiraCloudExtractTextFromLastComment.groovy Modified

  • Ignore whitespace
  • Hide word diff
 def getComments = get("/rest/api/3/issue/${issueKey}/comment")
         .header('Content-Type', 'application/json')
         .asObject(Map)
-        
+
+// If the issue has comments        
+if(getComments.body.total != 0){
 // Save the last comment to a variable called lastComment
 def lastComment = getComments.body.comments.last().body.content.content.text.toString().replaceAll("\\[", "").replaceAll("\\]", "")
 
 // log out the last comment
-logger.info("The Last Comment = " + lastComment)
+logger.info("The Last Comment = " + lastComment)
+}else{
+// If the issue has no comments log out  message to say this.     
+logger.info("The issue has no comments")
+}
Created by Kristian Walker

File ScriptListinerJiraCloudExtractTextFromLastComment.groovy Added

  • Ignore whitespace
  • Hide word diff
+/*
+* This example script listner script must bve configured to fire on the 'Issue Updated' Event and shows how to get all comments on the issue and then to get the last comment and to save the text from it to a varible. 
+* "All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner
+* license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed or
+* amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
+*/
+
+// Get the key of the current issue
+def issueKey = issue.key
+
+// Make a call to the Get Comments API to get all the comments on the issue
+def getComments = get("/rest/api/3/issue/${issueKey}/comment")
+        .header('Content-Type', 'application/json')
+        .asObject(Map)
+        
+// Save the last comment to a variable called lastComment
+def lastComment = getComments.body.comments.last().body.content.content.text.toString().replaceAll("\\[", "").replaceAll("\\]", "")
+
+// log out the last comment
+logger.info("The Last Comment = " + lastComment)
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.