It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Help with Groovy scripted field

Bryan Karsh Oct 26, 2013

Hi,

This is a weird one. I am trying to make a scripted field that tracks the last commenter of an issue. I actually have something that works pretty well.

However, I want to modify it to be able to omit a user (weird use case, I know).

Example: let's say there are 4 comments on an issue, from user A, and user B. I want to look at all comments in an issue, but *only* report the last comment from user A.

This is what I have so far(see below). It doesn't seem to work. Any tips appreciated!

import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)

if (comments) {
    comments.each {
        if (it.authorUser.toString().contains ('userB')) {

            comments.remove(it)

        }

    }

}

comments.last().authorUser

Not exactly sure what I doing wrong here. I am new to groovy, so perhaps I am doing some silly syntactical mistake? I get errors like so in the logs:

javax.script.ScriptException: java.util.ConcurrentModificationException

 A stacktrace has been logged.

2013-10-26 14:39:17,594 http-bio-8080-exec-23 ERROR bryank 879x1040x1 1ba2xk 10.26.101.27 /rest/gadget/1.0/issueTable/jql [onresolve.jira.groovy.GroovyCustomField] javax.script.ScriptException: java.util.NoSuchElementException: Cannot access last() element from an empty List

2 answers

0 votes
Bryan Karsh Oct 26, 2013

Okay,

I got it to work (at least so far, by doing this):

import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)

comments.removeAll { it.authorUser.toString().contains ('userB') }
comments.last().authorUser

If there are any better approaches, let me know!

0 votes
JamieA Oct 26, 2013

Something like:

import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()
commentManager.getComments(issue)*.author.reverse().findResult {it.author != "baduser"}

JamieA Oct 26, 2013

.reverse() might not be necessary... not sure what order they are returned in by default.

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Marketplace Apps & Integrations

Webinar alert! An IT Admin's Guide to Securing Collaboration at Scale

Hello, Atlassian Community! My name is Dave Meyer and I'm a Principal Product Manager at Atlassian. I wanted to give this community a heads up about an upcoming Webinar that might be of interest...

163 views 2 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you