Hello everyone! I have a code which add watchers in issue. but now I need to add a user only if the task : task type is "my task type" and it has a field "my cf select list" and the option "Yes" is selected there. Please tell me how can I do this? Thank you.
import com.atlassian.jira.component.ComponentAccessordef userManager = ComponentAccessor.getUserManager()def watcherManager = ComponentAccessor.getWatcherManager()def watcher = ["username1","username2"]watcher.each{userName->def user=ComponentAccessor.userManager.getUserByName(userName)watcherManager.startWatching(user, issue)}
Hi, have you found a solution? I'm facing the same problem.
The issue is created of the specific type and has the specific value for the customfield, correct (the field is of what type (text, number, list single or multiple choice, etc.))?
I can give you old examples that I already have, and they can be rewritten using HAPI.How to set a single user as a watcher:
import com.atlassian.jira.component.ComponentAccessordef userName = 'user_name' // Name of the userdef userManager = ComponentAccessor.getUserManager()def user = userManager.getUserByName(userName)if (!user) {log.warn("User ${userName} not found")return}def watcherManager = ComponentAccessor.getWatcherManager()// Apply when the issue is of "Change Request" typeif (issue.getIssueTypeId() == '42') {watcherManager.startWatching(user, issue)}
How to set two users as watchers:
import com.atlassian.jira.component.ComponentAccessordef firstUserName = 'user_name' // Name of the userdef secondUserName = 'user_name' // Name of the userdef userManager = ComponentAccessor.getUserManager()def firstUser = userManager.getUserByName(firstUserName)def secondUser = userManager.getUserByName(secondUserName)if (!firstUser) {log.warn("User with name ${firstUserName} not found")}if (!secondUser) {log.warn("User with name ${secondUserName} not found")}def watcherManager = ComponentAccessor.getWatcherManager()// Apply when the issue is of "Bug" typeif (issue.getIssueTypeId() == '1') {watcherManager.startWatching(firstUser, issue)watcherManager.startWatching(secondUser, issue)}
How to set all the people from DL into watchers (leave only one FOR loop):
import com.atlassian.jira.component.ComponentAccessordef DL = 'WFT Business Desk Atlassian Support' // Replace the name to your DLdef groupManager = ComponentAccessor.getGroupManager()def watcherManager = ComponentAccessor.getWatcherManager()def membersOfDL = groupManager.getUsersInGroup(DL, false) // without inactive usersif (membersOfDL) {// Use only one method and delete the others// Using enhanced for-loopfor (user in membersOfDL) {watcherManager.startWatching(user, issue)}// Using FORfor (int i = 0; i < membersOfDL.size(); i++) {watcherManager.startWatching(membersOfDL[i], issue)}// Using EACHmembersOfDL.each {watcherManager.startWatching(it, issue)}// Using FOREACHfor (member in membersOfDL) {watcherManager.startWatching(member, issue)}}
I came up with the following solution that meets my needs:
import com.atlassian.jira.component.ComponentAccessor// get instances of the user and watcher managersdef userManager = ComponentAccessor.getUserManager()def watcherManager = ComponentAccessor.getWatcherManager()// define the list of users to be added as watchersdef watcher = ['user1','user2','user3','user4']// add each user from the list as a watcher to the current issuewatcher.each{userName-> def user=ComponentAccessor.userManager.getUserByName(userName) watcherManager.startWatching(user, issue)}
It looks like you're new here. Sign in or register to get started.