Scripted Field to copy latest comment

Kathryn Allison January 6, 2014

Hey all,

I need to create a scripted field [free text] that will copy the last comment made to a ticket whenever comments are made.

Any ideas?

Thanks in advanced :)

4 answers

1 accepted

5 votes
Answer accepted
Kathryn Allison January 7, 2014

Added.body and got what I needed.

import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()

def comments = commentManager.getComments(issue)

if (comments) {

comments.last().body

}

mahmoud atout January 25, 2015

thx for the code it is working but now I am trying to reverse it so the comments section can copy the custom field... sorry for the question but i am a newbie thx

Carol Jones March 11, 2016

@Kathryn Allison, @Henning Tietgens (or anyone smile) – do you know if there's a way to show this new scripted field in wiki markup?  Some of our users have been using wiki markup within their comments and this new field is awesome as it allows us not have open each ticket to view the comments.  However it's displaying the raw text, not sure if that can be configurable/scripted or not as my searches aren't coming up with much.  TIA!

Joshua DeClerck July 25, 2017

@Carol Jones2 

I just managed to get this working based on frankensteining various other scripts here and there. Since this discussion is still on the first page of google results for searches on this topic, I'll add a comment here. Have a look at the current state of my scripted field:

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) {
rendererManager.getRenderedContent("atlassian-wiki-renderer", comments.last().body, issue.issueRenderContext)
}

Changing the returned value to one that uses the renderer manager gave me formatted results. It works just fine if you set the field type to "multi-line text", but you can set it to "HTML" if you want it to be more literally correct. The result is the same for both in JIRA 7.4.

Note, if you copy and use my version of the script, it also contains another modification to restrict which comment is returned based on what each user is permitted to see. Kind of defeats the point of comment visibility levels if this field circumvents them, after all. XD

Like # people like this
Tobias Schander August 8, 2017

Thanks! Great Script.

Holan Shetlar August 14, 2017

@Joshua DeClerck - Thanks for the code - it works GREAT! How would I add the commenter's name and date to the script?  Thanks again!

Joshua DeClerck August 14, 2017

@Holan Shetlar - Oh man, I am not a developer and as a result, this is not guaranteed to be good quality code, but ok.

First, there are two sets of comment author/date info you can get: (1) creator/created, and (2) updater/updated. If a comment hasn't been modified, (2) will return the same values as (1). I'm opting to use (2) because of the extra versatility. I'm also opting to use the function to retrieve the author's "full name", rather than username.

It's possible to include logic to determine whether the comment has been updated or not, and print the name/date info differently depending on that, but I won't be doing that this time.

Using the same as my earlier example, I changed the if block this way:

if (comments) {
def commentAuthor = comments.last().getUpdateAuthorFullName() // alt = getAuthorFullName()
def commentDate = comments.last().getUpdated() // alt = getCreated()
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>"
}

[Edited for a slightly nicer looking, less English-y outcome, but date only]
[More editing because I used generated HTML and even as a noob I could see it was ugly, oops - maybe a little better now?]

I dumped the original output into a commentBody variable, and made two more variables for the author and date. You can replace getUpdateAuthorFullName() and getUpdated() with other functions as documented in Atlassian's developer docs here.

The last line with the return command is for your template. It works with regular HTML, so add formatting to the text however you like. My example just inserts a simple line break after the body and concats the date and author.

I'm pretty sure my example is riddled with bad habits. I'm not sure if defining variables like that inside an if block is good code, or if using return like that is good code. I'm positive my template is bad code (not internationalized). And the date is just an ugly datetime string with no formatting. But I think this can get you started. I hope so, at least!

Pradeep Kumar November 1, 2017

Hi All,

 

I am getting below error while I am trying to capture last comments into different custom field.

 

can someone please help me on fixing below issue.

 

Error1.png

Henning Tietgens
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.
November 1, 2017

Hi, it seems like you're writing a listener. The variable "issue" is not directly available, you have to use "event.issue" instead.

Henning

Linda H May 3, 2019

Hi Joshua,  This is so AWESOME!!!!  Thank you so much!!!!   works in Jira 7.13.1

Joey Klein May 5, 2020

@Joshua DeClerck - - this is awesome!! I cant figure out how to have everything print on one line (drop the line break).  can you help?

Henning Tietgens
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 5, 2020

Take a look at https://www.tutorialspoint.com/groovy/groovy_replaceall.htm and use this to replace all line breaks within the comment body with an empty string.

Like Joey Klein likes this
Joshua DeClerck May 5, 2020

@Joey Klein - Ah, yeah unfortunately Henning's suggestion might not work in this particular case. It comes down to the return body here:

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

The key parts are commentBodycommentDate, and commentAuthor. What I shared made some subjective style choices, like the YYYY-MM-dd format (change this to your heart's content) and the layout. The fact that the date and author are both inside a paragraph tag (<p>...</p>) is what's putting them on a separate line, and why simply removing line break characters won't be enough.

You should be able to replace the "<p>..." part between commentBody and commentDate to whatever separator you want to use. Likewise for the " | " between the date and author. Just be sure to remove the closing /span and/or /p tags if you remove their opening counterparts.

Like Henning Tietgens likes this
Joey Klein May 5, 2020

thanks @Joshua DeClerck .  very helpful 

Henning Tietgens
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 6, 2020

Yes, my "line break" was more a general term, which could be translated to "whatever your line break is". I thought @Joey Klein was talking about the comment body itself, and I'm not sure what the line breaks within the formatted comment body are. Could be <p> or <br> or similar html tags...

0 votes
Nicole Ahlborn March 2, 2018

Hi All,

first of all thanks for this nice scripted field!

Do you think that there is a possibility to script a field that will copy all comments?

Kind regards,

Nicole

Henning Tietgens
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.
March 4, 2018

Yes, you can use commentManager.getComments(issue) to get all comments of an issue.

Henning

Nicole Ahlborn March 6, 2018

Hello Henning,

thanks for the fast answer!

Now it works.

Kind regards,

Nicole

0 votes
Zeeshan Salim January 4, 2018

Hi Guys,

 

With the codes you have written, were any third party plug-ins used at all?

 

Thanks

Henning Tietgens
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.
January 4, 2018

Hi,

yes, you need the ScriptRunner plugin.

Henning

0 votes
Henning Tietgens
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.
January 7, 2014

Use getComments() from CommentManager to get a list of all comments and then identify the last comment.

Kathryn Allison January 7, 2014

I tried using those 3 exact pieces, but couldn't get the script just right.

I'm not really groovy savvy, so my attempt may have just been a poor one.

Suggest an answer

Log in or Sign up to answer