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

Checking the author of a comment in a Script Listener

Daniel Ehrlich May 01, 2014

Creating a script listener for 'Issue Commented'. Need the listener condition to be true only if the author of the last comment added to the issue is a specific user. How do I get the comment's author in script runner/groovy. In jira-python I would use:

issue.fields.comment.comments[-1].author.name

Are the comments in issue or transientVars? Still I need to know how to reference the name.

Thanks!

1 answer

1 accepted

1 vote
Answer accepted
JamieA May 01, 2014

Something like this, untested:

import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()

def comments = commentManager.getComments(issue)
if (comments) {
    return comments*.authorKey[-1] == "someuser"
}
else {
    return true // or false, whatever you want if no comments
}

Daniel Ehrlich May 01, 2014

I am using the jira-python package that comes from Atlassian. At least there name is on the cover sheet of the documentation. And yes, the module hides almost all of the ugliness one encounters when dealing with REST and JSON.

The example I gave asssumes that you have already connected to jira and retrieved the issue using a client object. If anyone wants a look at the module here is a link to the documentation:

http://jira-python.readthedocs.org/en/latest/

JamieA May 01, 2014

BTW I don't think that is valid python in jira, not unless the authors of the plugin you've been using have added a load of synthetic fields.

JamieA May 01, 2014

OK but that's a remote api, it's just dealing with a chunk of json. In script runner you are using the java api.

Daniel Ehrlich May 01, 2014

OK. I understand the difference. Here is what I came up with based on your code sample above:

// Reopen issue on email comment if in Pending User, Resolved, or Closed status.
// Email handler runs as user ABCDEFG
import com.atlassian.jira.component.ComponentAccessor
def status = issue.getStatusObject().getName()
def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)
if (["Pending User", "Resolved", "Closed"].contains(status) && comments) {
    return comments*.authorKey[-1] == "ABCDEFG"
} else {
    return false
}

Only issues that are in a status of "Pending User", "Resolved" or "Closed" are to be transitioned to "Open". When I plug the above into the Condition Tester it returns false for an issue that is Closed and where that last comment added was authored by ABCDEFG.

Java/Groovy are not my strong suit. Any help would be appreciated.

Thanks.

JamieA May 02, 2014

I just tested that and it worked fine. Is the context here that you're running this code as a condition for a built-in script?

You can put some asserts in so you can see where it goes wrong when you expect it to be true (remember to remove before deploying):

import com.atlassian.jira.component.ComponentAccessor
def status = issue.getStatusObject().getName()
def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)
assert comments

assert status == "Closed"

if (["Pending User", "Resolved", "Closed"].contains(status) && comments) {
    assert comments*.authorKey[-1] == "ABCDEFG"
    return comments*.authorKey[-1] == "admin"
} else {
    return false
}

Daniel Ehrlich May 02, 2014

This is what I finally got to worK

import com.atlassian.jira.component.ComponentAccessor
def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)
if (comments && ["Pending User", "Resolved", "Closed"].contains(issue.status.name)) {
    return comments*.authorKey[-1] == "abcdefg"
} else {
    return false
}

Apparently JIRA stores all users names in lower case in spite of what is displayed in System->User Management. Sigh. Thanks for all of the help!

Suggest an answer

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

Demo Den Ep. 4: Continuous Integration with CircleCI & Jira Software

Get ready! Demo Den Episode 4 is coming your way on Tuesday, May 28, 2018 with a Continuous Integration and Delivery special demo. CircleCI Director of Solutions Engineering, Eddie Webb will show us ...

486 views 0 4
Join discussion

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