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

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

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!

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

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.

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?

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)
}

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);

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

314 views 0 1
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