Checking the author of a comment in a Script Listener

Daniel Ehrlich May 1, 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
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 1, 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 1, 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
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 1, 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
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 1, 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 1, 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
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 2, 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 2, 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
TAGS
AUG Leaders

Atlassian Community Events