Get All Comments in a JIRA Scriptrunner Scripted Field

Allyson Norris July 14, 2020

I need to be able to view all the comments on an issue, with the date and author in a scripted field. 

I was trying to play around with this but could not figure out how to get it to work for all comments: 

import com.atlassian.jira.component.ComponentAccessor

def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def commentManager = ComponentAccessor.getCommentManager()
def rendererManager = ComponentAccessor.getRendererManager()
def comments = commentManager.getCommentsForUser(issue, user)

if (comments) {
def commentAuthor = comments.last().getUpdateAuthorFullName()
def commentDate = comments.last().getUpdated()
def commentBody = rendererManager.getRenderedContent("atlassian-wiki-renderer", comments.last().body, issue.issueRenderContext)

return "<p><span style=\"font-size:smaller;color:#a9a9a9;font-style:italic\">" + commentDate.format('YYYY-MM-dd') + " | " + commentAuthor + commentBody + "</span></p>"
}

1 answer

1 accepted

3 votes
Answer accepted
Italo Qualisoni [e-Core]
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.
July 14, 2020

You need to focus in below line, right now you are only getting the last comment data:

def commentBody = rendererManager.getRenderedContent("atlassian-wiki-renderer", comments.last().body, issue.issueRenderContext)

You should iterate your comment list, something like this:

import com.atlassian.jira.component.ComponentAccessor
def issue = ComponentAccessor.getIssueManager().getIssueObject("AUTO-249")
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def commentManager = ComponentAccessor.getCommentManager()
def rendererManager = ComponentAccessor.getRendererManager()
def comments = commentManager.getCommentsForUser(issue, user)

def htmlResponse = "";

comments.each({ comment ->
def commentAuthor = comment.getUpdateAuthorFullName()
def commentDate = comment.getUpdated()
def commentBody = rendererManager.getRenderedContent("atlassian-wiki-renderer", comment.body, issue.issueRenderContext)

htmlResponse += "<p><span style=\"font-size:smaller;color:#a9a9a9;font-style:italic\">" + commentDate.format('YYYY-MM-dd') + " | " + commentAuthor + commentBody + "</span></p>";
})
return htmlResponse;

commentUntitled.png

Allyson Norris July 14, 2020

@Italo Qualisoni [e-Core] Thank you so much! Could you help with the error I am getting when I put it in? It says that the Variable Issue masked a binding variable of the same name and I am not sure how to fix it. Error.PNG

Italo Qualisoni [e-Core]
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.
July 14, 2020

Where are you using this code?

Probably script runner provides you the issue variable in the context, you can remove/comment this line and see if will this work as expected?

Allyson Norris July 14, 2020

@Italo Qualisoni [e-Core] I tweaked it to say issues and it actually started working! Thank you so much! 

The other question is, is there a way to get the most recent comments on top? 

Italo Qualisoni [e-Core]
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.
July 14, 2020

@Allyson Norris ,

That's great.

Try to change

comments.each....

to

comments.reverse().each

 This will reverse the list and should fix your issue

Like Srikanth Ganipisetty likes this
Allyson Norris July 15, 2020

@Italo Qualisoni [e-Core]  Thank you so much that works perfectly! 

Ramaiah Pendli November 10, 2022

@Italo Qualisoni [e-Core] can we get the latest to the previous comment details.

Suggest an answer

Log in or Sign up to answer