Use Scriptrunner to email body of comment after Issue Commented

Jonathan Davidson May 10, 2017

Hi, so I'm inexperienced and don't know where to look. Basically I am setting up emails to trigger when a comment is left on a particular Project. I would like the emails to have the body text of the comments left.

I am using Scriptrunner and setting an event with Script Listeners. I can successfully call the JIRA issue into the subject line via $issue, a la:

Issue $issue requires changes

In the body of the email, I can use ${issue.description} to pull in the entire description, but I'm interested in the comments. I've shot in the dark with ${issue.comments} and ${getComments($issue).last()} and the like, but I realize I need documentation and help. Could someone point me in the correct direction?

4 answers

2 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Thanos Batagiannis _Adaptavist_
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.
May 10, 2017

Hi Jonathan

Try the following in the email template

<%
def comments = com.atlassian.jira.component.ComponentAccessor.commentManager.getComments(issue)?.collect {
    "Comment body: ${it.body} | Author: ${it.authorFullName} | Created: ${it.created}"
}?.join("<br>")
%>

$comments

It will add in the email all the comments for the particular issue. 

Please let me know if this does the trick.

regards, Thanos

Jonathan Davidson May 10, 2017

Thanks, Thanos! That definitely works wonders, and I love that I can have all the components in order to reformat as I like!

"Author: ${it.authorFullName}\n  Comment body: ${it.body}\nCreated: ${it.created}"

Author: Davidson, Jonathan
  Comment body: What I want changed is this.... blah
Created: Tue May 09 11:29:32 EDT 2017

Jonathan Davidson May 10, 2017

I marked this as the answer because you are awesome, but if possible, could you expand on how to select only particular comments? If there are multiple comments, your .collect() grabs them all and then spits out the format provided for each one, with the join() stringed between them.

What if I want to select a particular one or the last one?

Jonathan Davidson May 15, 2017

For anyone interested in the last comment, this also appears to work for pulling only the last comment:

def comments = commentManager.getComments(sourceIssue)

comments.last().body

Randy Erbacci July 12, 2017

Hi Johnathan, I also need the last comment. I've tried your suggestion but I can't get it to work. Since I have very little experience scripting, would you be able to give me the exact code to put in my email template to pull only the last comment in an issue?

Jonathan Davidson July 12, 2017

Hi Randy,

Ultimately I set the comments into a newly created Issue's description field, rather than sending via email:

import com.atlassian.jira.component.ComponentAccessor
def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(sourceIssue)
if (comments) {
    def desc = sourceIssue.description
issue.description = '*Manager comment:* ' + comments.last().body + '\r\n \r\n \r\n----\r\n' + desc } else { issue.description = "No comment set upon transitioning task" }

I can't help you troubleshoot, but probably wrap it up in tags and then call whatever you name the variable.

Best,

Jonathan

0 votes
Answer accepted
Jonathan Davidson July 12, 2017
<%
def comments = com.atlassian.jira.component.ComponentAccessor.commentManager.getComments(issue).last()?.collect {
    "Comment body: ${it.body} | Author: ${it.authorFullName} | Created: ${it.created}"
}?.join("<br>")
%>

$comments

Think it's the addition of .last() in the code that Thanos provided

Randy Erbacci July 13, 2017

Thanks Jonathan, that definitely helps and I'm able to include the comments in email notifications that gets sent as a post-function. One thing, an issue with no comments prevents the email notification from ever going out. Any ideas on that? Thanks again for your help.

0 votes
Monique vdB
Community Manager
Community Managers are Atlassian Team members who specifically run and moderate Atlassian communities. Feel free to say hello!
July 12, 2017

@Jonathan Davidson I'm sorry some of your posts got caught in our spam quaratine, I have released them all now. 

Jonathan Davidson July 13, 2017

Thanks @Monique vdB, I apologize that there's now a lot of similar posts with approximately the same content. Can I delete them? When editing them, I only see the option to change text, not to delete the post altogether.

Monique vdB
Community Manager
Community Managers are Atlassian Team members who specifically run and moderate Atlassian communities. Feel free to say hello!
July 13, 2017

@Jonathan Davidson thanks for changing the text -- now it's obvious which ones to delete. I can do that for you :) 

0 votes
Jonathan Davidson July 12, 2017

Here's the code in cloning an issue, if you want the last comment, which is where I'd ultimately ended up by the time I wrote my last reply:

import com.atlassian.jira.component.ComponentAccessor
def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(sourceIssue)
if (comments) {
    def desc = sourceIssue.description 
    issue.description = '*Manager comment:* ' + comments.last().body + '\r\n \r\n \r\n----\r\n' + desc
}
else {
    issue.description = "No comment set upon transitioning with Remove access prompt"
}
TAGS
AUG Leaders

Atlassian Community Events