Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,298,492
Community Members
 
Community Events
165
Community Groups

Scriptrunner: Latest public comment in custom email

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

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! :)

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

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
Community showcase
Published in Jira Service Management

Coming Soon: Insight Changing to Assets

The 2020 acquisition of Mindville added powerful asset and configuration management capabilities to Jira Service Management in the form of Insight. Following the completion of that integration, custo...

287 views 2 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you