Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Scriptrunner: Latest public comment in custom email

Darius Mikelevičius June 16, 2020

Hello,

I've been using Built-in script "Custom Email" for scriptrunner.

Command I've used:

if (lastComment)
out << "New Comment:" << lastComment
%>

And it sends out all comments (internal and public) as a emails.

is it possible to show only public comments in custom email body?

2 answers

1 accepted

1 vote
Answer accepted
Mathis Hellensberg
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 18, 2020

Hi @Darius Mikelevičius 

Should be possible :) The script below works for an event listener and is straight from Scriptrunners documentation.

import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.comments.Comment
import groovy.json.JsonSlurper

final SD_PUBLIC_COMMENT = "sd.public.comment"

def event = event as IssueEvent
def user = event.getUser()
def comment = event.getComment()
def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)

def isInternal = { Comment c ->
def commentProperty = commentPropertyService.getProperty(user, c.id, SD_PUBLIC_COMMENT)
.getEntityProperty().getOrNull()

if (commentProperty) {
def props = new JsonSlurper().parseText(commentProperty.getValue())
props['internal']
}
else {
null
}
}

if (comment) {
return isInternal(comment)
}
return false

 You can read more here: https://scriptrunner.adaptavist.com/4.2.0.7/jira/recipes/misc/jira-service-desk.html

Hope it helps! :)

Darius Mikelevičius June 22, 2020

Thanks @Mathis Hellensberg it helped :)

Mathis Hellensberg
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 22, 2020

I glad it did! Thanks for the high five :)

0 votes
Jesus Corral May 16, 2022

This is probably not the most optimal way to do it but in case anyone else finds this thread like I did, this is how I accomplished it in my DC instance.  This uses the 'Send a custom email' ScriptRunner post-function and the script is placed within the 'Condition and Configuration' section.  It uses a modified version of what was supplied above by Mathis!

 

import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.Comment
import groovy.json.JsonSlurper
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserManager

final SD_PUBLIC_COMMENT = "sd.public.comment"

def user = ComponentAccessor.getUserManager().getUserByName("automationbot") //grabs service account to pull list of comments
def allComments = ComponentAccessor.getCommentManager().getComments(issue) //grabs all comments from the trigger issue
def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)

def isInternal = { Comment c ->
def commentProperty = commentPropertyService.getProperty(user, c.id, SD_PUBLIC_COMMENT).getEntityProperty().getOrNull()

if (commentProperty) {
def props = new JsonSlurper().parseText(commentProperty.getValue())
(props['internal'] as String).toBoolean()
}
else {
null
}
} //function created by Adaptavist
List extArray = [] //defines array to hold external comments
if(allComments){ //If a comment exists enter this statement
allComments.each{ comment -> //sort through each comment
if(!isInternal(comment)){ //if the comment is external i.e. NOT internal
extArray.add(comment.body) //add the comment body to the extArray
}
}
if(extArray){ //if the extArray isn't empty pass variable 'CommentPassed' to templates
config.CommentPassed = extArray.last() //passes the last external comment i.e. most recent
}
else{ //if the extArray doesn't exist all comments are internal
config.CommentPassed = "No comment to display"
}
}
else{ //Case for no comments on the issue
config.CommentPassed = "No comment to display"
}

 

NOTE: Account used to sort the comments MUST have browse project permission to view comment properties.  Otherwise the comments may not be sorted as expected.  Found in the cloud API documentation https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-issue-comment-properties/ but I assume it's the same for DC and Server.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events