Hello,
in JIRA 7 i have a Groovy-Script which add a user to the watchlist after a transition.
now i want to remove this user after a transition from the watchlist.
here is the "add-user to watchlist-script":
def watcherManager = ComponentAccessor.getWatcherManager() def userManager = ComponentAccessor.getUserManager() def watchUsers = {usernames -> usernames.each { def user = userManager.getUserByKey(it.toString()) watcherManager.startWatching(user, issue) } } def users = ["hrconfidential"] watchUsers(users)
what is the way back?
In the watcherManager there is a function, which should do the trick.
@Nonnull public Collection<Issue> stopWatching (ApplicationUser user, Collection<Issue> issues, Context taskContext)
it does not work
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
file:/data/jira/home/groovyscripts/removehr.groovy: 1: You defined a method without body. Try adding a body, or declare it abstract. at line: 1 column: 1. File: file:/data/jira/home/groovyscripts/removehr.groovy @ line 1, column 1.
public Collection<Issue> stopWatching (ApplicationUser user, Collection<Issue> issues, Context taskContext)
^
1 error
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I shared only the method declaration , you have to invoke the method with required parameters.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Tarun Sapra is almost there, though I think I'd use the method signature that doesn't take a Context parameter.
Assuming your current code works, you could reuse it almost verbatim.
import com.atlassian.jira.component.ComponentAccessor def watcherManager = ComponentAccessor.getWatcherManager() def userManager = ComponentAccessor.getUserManager() def stopWatch = {usernames -> usernames.each { def user = userManager.getUserByName(it.toString()) watcherManager.stopWatching(user, issue) } } def users = ["hrconfidential"] stopWatch(users)
Pedantic code style note: I'm assuming that you actually have a whole list of users, which is why you're bothering to put "hrconfidential" inside of a list. If not, you could save yourself the closure and .each iteration and just call startWatching and stopWatching on a string variable.
import com.atlassian.jira.component.ComponentAccessor def watcherManager = ComponentAccessor.getWatcherManager() def userManager = ComponentAccessor.getUserManager() def userName = "hrconfidential" def user = userManager.getUserByName(userName) watcherManager.stopWatching(user, issue)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
thank you for your reply.
yes ur right it is only one user i want to remove from the watchlist.
i tried your code but a new error occurs:
2016-08-10 16:07:16,412 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2016-08-10 16:07:16,412 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: TS-7806, actionId: 41, file: /data/jira/home/groovyscripts/removehr.groovy groovy.lang.MissingPropertyException: No such property: ComponentAccessor for class: removehr at removehr.run(removehr.groovy:1)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Have you imported the ComponenttAccessor class?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
sorry ur right. here is now the script.
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.*;
def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
def userName = "hrconfidential"
def user = userManager.getUserByKey(userName)
watcherManager.stopWatching(user, issue)
but now no error occurs and the user is still in the watchlist.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
def user = userManager.getUserByKey(userName) change it to
def user = userManager.getUserByName(userName)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For error you have to see the log files.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Also import watcherManager
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I updated the script in my answer with those method names and imports. You shouldn't need to import the watcherManager since you're getting it via ComponentAccessor, unless you do a type declaration.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Did you see the log files? atlassian-jira.log?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
yes there is an error ur right
2016-08-11 09:52:27,445 http-nio-9001-exec-16 ERROR ad 592x433x1 19sqanc 172.16.177.197,10.0.0.63 /secure/WorkflowUIDispatcher.jspa [c.a.j.issue.watchers.DefaultWatcherManager] You must specify a user.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
ok now i now why the last error occurs.
i have define the displayname and not the username of that user.
with this script no error occurs but it does not work:
import com.atlassian.jira.component.ComponentAccessor def watcherManager = ComponentAccessor.getWatcherManager() def userManager = ComponentAccessor.getUserManager() def userName = "hrconfidential" def user = userManager.getUserByName(userName) watcherManager.stopWatching(user, issue)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Add proper logging and print the user object and what line of the script is showing the error?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
put the post-function in the end of the list of post transition operations
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.