how to extract last comment author using a Groovy script?

Peter Bengov
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 24, 2015

I understand that running a JQL like issueFunction in lastCommented("inGroup support") isn't possible via Script Runner, so I'm trying to extract the user which made the last comment with Scripted Fields and save it into the User Picker template. 

I'm using the following code snippet to extract the user

def lastCommenter = ComponentAccessor.getUserUtil().getUserObject(ComponentManager.getComponentInstanceOfType(CommentManager.class).getComments(issue).last().author);

2 problems with this:

  • If indeed I chose the User Picker template, then the value that is returned to the ticket (under users section) is Anonymous.
  • If I use the Text Field template, I get the user + number like so: user.name:1000

Can you suggest a way to either extract the the tickets that had their last comment by a user from a specific group or a way to return the current value via Groovy?

Thanks

4 answers

1 accepted

6 votes
Answer accepted
Alejo Villarrubia [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.
March 25, 2015

Anonymous is returned because the User Picker is expecting a different data type different from the one returned by getUserObject.

You can use the following snippet to show the user in a User Picker template:

import com.atlassian.jira.component.ComponentAccessor

def comments = ComponentAccessor.commentManager.getComments(issue)
if (comments) {
    return comments.last().authorApplicationUser
}

On the other hand, if you want to extract the value to a Text Field template, you can do something like this:

import com.atlassian.jira.component.ComponentAccessor

def comments = ComponentAccessor.commentManager.getComments(issue)
if (comments) {
    return comments.last().authorFullName
}

Hope that helps

Alejo Villarrubia [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.
March 25, 2015

I just updated my answer to avoid getting nPE when there are no comments in the issues

JamieA
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 25, 2015

Thanks Alejo. Of these two, you should use the first one, then you can configure a searcher and use it in JQL queries. authorApplicationUser is relatively new in jira, come back to us if it doesn't work and we can give you an alternative.

Peter Bengov
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 25, 2015

Thank you both. The first groovy script indeed works like a charm. Added my vote for the new feature in GRV-671

Samuel April 28, 2015

@Alejo Villarrubia [Adaptavist] is there a way to add "last comment + author's name", using single scripted field?

0 votes
Shreya Rawal May 14, 2015

Hi All,

I am running into similar issue with a different script:

 

changeItems = componentManager.changeHistoryManager.getAllChangeItems(issue)
if (changeItems?.size()>0) {
    componentManager.userUtil.getUserObject(changeItems.sort(false).last().user)
} else {
    null
}

 

Text Field returns: username:12345
User Picker returns: Anonymous
Any suggestion?
Thanks!! 
0 votes
Alejo Villarrubia [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.
March 25, 2015

Hi @Peter Ben , having a lastCommented function is a good idea. I've just created an improvement suggestion here: https://jamieechlin.atlassian.net/browse/GRV-671

0 votes
JamieA
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 24, 2015

Most likely this is due to the "wrong" type of user being returned... we'll check it out tomorrow.

Suggest an answer

Log in or Sign up to answer