Use Case: Adding Project Role Users as Watchers to an Issue

One of our customers wanted to adapt their issue workflow such that specific users got automatically added as
watchers to an issue when it gets created.

The following script will iterate over all users who have the role ‘ProjectWatcher’ and automatically be added as a watcher.

/*
** Add watchers from project role 'ProjectWatcher
** Code compatible with JIRA 6.2.3
*/
  
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.watchers.WatcherManager
 
 
 
/*
**    Set watcher based on the configuration of the CustomerWatcher (RADAR-408)
 */
ProjectRoleManager prm = ComponentAccessor.getComponentOfType(ProjectRoleManager.class)
WatcherManager watcherManager = ComponentAccessor.getWatcherManager()
 
ProjectRole customerWatcherRole = prm.getProjectRole("ProjectWatcher")
if (! customerWatcherRole) {
    log.error("Role 'CustomerWatcher' not found - is it configured ?")
    return
}
 
 
def customerWatchers = prm.getProjectRoleActors(customerWatcherRole, issue.getProjectObject())
if (! customerWatchers) {
    log.debug("Project Role Actors for role CustomerWatcher in project '"+issue.getProjectObject().getKey()+"' is null")
    return
}
 
 
for (ApplicationUser targetWatcher:customerWatchers.getApplicationUsers()) {
    watcherManager.startWatching(targetWatcher, issue)
}
 

If you need help implementing this script, please contact us. We’d love to help.

Outline

Subscribe to our newsletter to receive Idalko’s insights & events

    Related Articles