Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy - add watchers from role and multi user field

Derek Mart
Contributor
April 18, 2020

Greetings!

Jira Server 8.5.4

I have a groovy script that, if run from the console, will add watchers from a role and from a custom field.  When I run it as a workflow script bound to Create issue transition, it does not add the watchers.  I've been staring at this for too long and was hoping someone could shed some new light on it.

[Edit: added the component watchers code and a few other things I tested]

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;

import com.atlassian.jira.security.roles.ProjectRole;
import com.atlassian.jira.security.roles.ProjectRoleManager;
import com.atlassian.jira.security.roles.ProjectRoleActors;

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.IssueManager;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

def myLog = Logger.getLogger("com.onresolve.jira.groovy");
myLog.setLevel(Level.DEBUG);

def logMe(message) {
    if (!binding.hasVariable("issue")) {
        log.debug("$message");
    } else {
        myLog.debug("$message");
    }
}

MutableIssue myIssue;
if (!binding.hasVariable("issue")) {
    IssueManager issueManager = ComponentAccessor.getIssueManager();
    myIssue = issueManager.getIssueByCurrentKey("KEY-111");
} else {
    myIssue = issue;
}

logMe("$myIssue");

def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager);
def defaultCc = projectRoleManager.getProjectRoleActors(projectRoleManager.getProjectRole("Default Cc"),myIssue.getProjectObject());

logMe("$defaultCc");

def watcherManager = ComponentAccessor.getWatcherManager();
for (ApplicationUser watcher:defaultCc.getUsers())
{
    watcherManager.startWatching(watcher,myIssue);
    logMe("$watcher");
}

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def componentWatchersField = customFieldManager.getCustomFieldObjectByName("Component Watchers");
def componentWatchers = myIssue.getCustomFieldValue(componentWatchersField);

logMe("$componentWatchers");

for (ApplicationUser componentWatcher:(ArrayList<ApplicationUser>) componentWatchers)
{
    watcherManager.startWatching(componentWatcher,myIssue);
    logMe("$componentWatcher");
}

1 answer

0 votes
Derek Mart
Contributor
April 19, 2020

I moved the

def myLog = Logger.getLogger("com.onresolve.jira.groovy");
myLog.setLevel(Level.DEBUG);

to inside logMe def

def logMe(message) {
   def myLog = Logger.getLogger("com.onresolve.jira.groovy");
   myLog.setLevel(Level.DEBUG);
    if (!binding.hasVariable("issue")) {
        log.debug("$message");
    } else {
        myLog.debug("$message");
    }
}

This seems to have fixed the issue of watchers from the role, but now I have the issue of watchers not coming from the custom field. I tried an event listener as well as post function script, but it seems like the Component Watchers field is not available in the same way as it is in console.

I switched 

def componentWatchersField = customFieldManager.getCustomFieldObjectByName("Component Watchers");

with 

def componentWatchersField = customFieldManager.getCustomFieldObject("customfield_18100");

because of deprecation warning, but this did not help.

Suggest an answer

Log in or Sign up to answer