Checking the author of a comment in a Script Listener

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

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
}

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/

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.

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.

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.

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
}

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Published Apr 19, 2018 in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

366 views 0 6
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you