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>"
}
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;
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That's great.
Try to change
comments.each....
to
comments.reverse().each
This will reverse the list and should fix your issue
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.