Set watchers from a group upon creation of an issue via post function script

Clare Cooper September 18, 2013

Hello,

I use JIRA 5.2.11 and I need to set a group of watchers each time an issue is created. I plan on using workflow and a post function to accomplish this. This is my first groovy script so I'm a newbie. I have cobbled together code and have eliminated some of the errors but now I'm down to:


javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.user.util.DefaultUserManager.getUser() is applicable for argument types: (com.atlassian.jira.crowd.embedded.ofbiz.OfBizUser) values: [mkttest:1] Possible solutions: getUser(java.lang.String), getUsers(), getAllUsers(), getAt(java.lang.String)

which I can't seem to get rid of. Any insights would be helpful. Note that right now, I'm just hard coding the issue number to see if I can get it to work.

Thanks!

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.crowd.embedded.api.Group
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.issue.Issue

def userManager = ComponentAccessor.getUserManager()
def groupManager = ComponentAccessor.getGroupManager()
def issueManager = ComponentAccessor.getIssueManager()
def watcherManager = ComponentAccessor.getWatcherManager()
def group = userManager.getGroup("jira-watcher-list")

def watchUsers = groupManager.getUsersInGroup(group).each
{
def user = userManager.getUser(it)
def issue = issueManager.getIssueObject("TST-2")
watcherManager.startWatching(user, issue)
}

watchUsers(group)

4 answers

1 vote
Clare Cooper September 20, 2013

Thanks to both of you for you inputs.

I was able to get it to work by doing the following:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.crowd.embedded.api.Group
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.issue.Issue

def userManager = ComponentAccessor.getUserManager()
def groupManager = ComponentAccessor.getGroupManager()
def issueManager = ComponentAccessor.getIssueManager()
def watcherManager = ComponentAccessor.getWatcherManager()

groupManager.getUsersInGroup("jira-watcher-list").each {
def issue = issueManager.getIssueObject("TST-2")
watcherManager.startWatching(it, issue)
}

Now, I'm not sure if it's the best way to code it - but it works! :-) Thanks again for your help!

Bharadwaj Jannu
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.
September 20, 2013

that's a happy thing to hear it is working. But I have a doubt that you require this functionality for only issue with key TST-2?

0 votes
Clare Cooper September 22, 2013

True - but my proof of concept is working so now I can generize it! Thanks again!

0 votes
Bharadwaj Jannu
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.
September 18, 2013

Clare,

You can try this Java statement by converting into groovy script and may be it work.

ComponentAccessor.getWatcherManager().startWatching(ComponentAccessor.getUserManager().getUserByKey(it), issue);

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.
September 18, 2013

You are almost there... from a quick look at your code I would say use:

def watchUsers = groupManager.getUsersInGroup(group).each
{
def issue = issueManager.getIssueObject("TST-2")
watcherManager.startWatching(it, issue)
}

Because getUsersInGroup returns a a collection of User.

Clare Cooper September 18, 2013

Thanks for the suggestion. I tried that and now I get:

javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.call() is applicable for argument types: (com.atlassian.crowd.embedded.impl.ImmutableGroup) values: [com.atlassian.crowd.embedded.impl.ImmutableGroup@d1ac903f] Possible solutions: tail(), wait(), any(), max(), last(), wait(long)

Any other thoughts?

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.
September 18, 2013

Your closure is wrong. Try this:

import com.atlassian.jira.component.ComponentAccessor

def userManager = ComponentAccessor.getUserManager()
def groupManager = ComponentAccessor.getGroupManager()
def issueManager = ComponentAccessor.getIssueManager()
def watcherManager = ComponentAccessor.getWatcherManager()
def group = userManager.getGroup("jira-watcher-list")

groupManager.getUsersInGroup(group).each {
    def issue = issueManager.getIssueObject("TST-2")
    watcherManager.startWatching(it, issue)
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events