Add Date to 'Last Comments' Field - script runner

Kevin Seery August 15, 2017

Hi,

 

I would like to have a customer field that showed 'date of last comment'-'last comment' for example:

10/08 - this is the last comment

 

I'm using script runner and can get the last comment but how do you add the date of the last comment at the beginnning of the field?

 

Thanks

Kevin

 

Currnet script:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.component.ComponentAccessor
def commentManager = ComponentAccessor.getCommentManager()
def comment = commentManager.getLastComment (issue)
comment.body

3 answers

1 accepted

2 votes
Answer accepted
Jenna Davis
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.
August 15, 2017

Hello, 

You can do something similar to this:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()
def comment = commentManager.getLastComment (issue)
def date = comment.getCreated()

return ((date as String) + " " + comment.body)

 You may want to mess with the formatting a bit to make it look exactly as you want, but that will make the date the comment was created appear in front of the comment's body. :)

Let me know if you have any problems or questions!

Regards, 

Jenna

Kevin Seery August 21, 2017

Hi Jenna,

 

Thanks so much for the quick response. this works realy well!! :)

How do I play with the format? (sorry very new to all this)

Would like to just show 'Aug 11'

Many Thanks

Kevin 

Kevin Seery August 21, 2017

Hi Jenna,

Found a solution, for the date format ;-)

Thanks some much for your help

Kevin

Jasmin Cua December 19, 2017

Hi Jenna, 

How do I get the last comment date of a public comment only?

Thank you. 

Kumar
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.
December 5, 2018

Hi @Kevin Seery @Jenna Davis @Nic Brough -Adaptavist-  I got a similler task so I I have used this script it worked well thanks 

I'm just wondering that is there any chance that it will copy the user  also who commented .

this script is copying the date and Comment i just want to add to copy the user also who commented 

can you please help to achieve this 

Thanks,

Kumar

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 5, 2018

Off the top of my head, I think you want comment.getAuthor().getName() for the user's login (get.fullName() for their display name if you need that too)

Kumar
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.
December 5, 2018

HI @Nic Brough -Adaptavist- Thanks for your response 

here is the script i found in atlassina community its working as excepted 

here is the script

 

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

 

thanks,

Kumar

Like # people like this
Rich Wolverton April 15, 2020

Nice update to Last Comment. For our purposes I moved the date and author to the front and slightly reformatted the date.

There are other places where this example will come in handy.

We would love the ability to remove blank lines and get single spacing since many of our comments are added from email and have lots of extraneous spacing and data.

Joey Klein May 5, 2020

@Kevin Seery what was your solution for the date format?  also, any way to only show first name?

1 vote
Kumar Aniket February 10, 2020

Hi,

I tried to use the same script in scriptrunner, but I get the below error. Any help with this?

 

Capture.JPG

Andrew Yeaman May 10, 2021

ComponentManager has been replaced by ComponentAccessor.

Simply remove the ComponentManager line.

1 vote
Prakruthi Viswanath May 22, 2018

Where was this script added ? Is it in script runner

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()
def comment = commentManager.getLastComment (issue)
def date = comment.getCreated()

return ((date as String) + " " + comment.body)
Kevin Seery May 23, 2018

Yes the script was added to script runner.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 23, 2018

It's added as a "scripted field".  Add a new custom field, select that type, and place it on the screens you want to see it on.

Then go to admin -> script runner -> scripted fields and add the script there.

Suggest an answer

Log in or Sign up to answer