JIRA - Groovy - Remove User from Wachtlist

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?

2 answers

1 vote
Tarun Sapra Community Champion Aug 08, 2016

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

Tarun Sapra Community Champion Aug 09, 2016

I shared only the method declaration , you have to invoke the method with required parameters.

@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 -&gt;
  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)

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)

Tarun Sapra Community Champion Aug 10, 2016

Have you imported the ComponenttAccessor class?

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.

Tarun Sapra Community Champion Aug 10, 2016

def user = userManager.getUserByKey(userName) change it to

 def user = userManager.getUserByName(userName)

Tarun Sapra Community Champion Aug 10, 2016

For error you have to see the log files.

Tarun Sapra Community Champion Aug 10, 2016

Also import  watcherManager

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.

there is no error but it does not work.

Tarun Sapra Community Champion Aug 10, 2016

Did you see the log files? atlassian-jira.log?

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.

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)
Tarun Sapra Community Champion Aug 10, 2016

Add proper logging and print the user object and what line of the script is showing the error?

Tarun Sapra Community Champion Aug 10, 2016

put the post-function in the end of the list of post transition operations

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Monday in Jira Ops

Jira Ops Early Access Program Update #1: Announcing our next feature and a new integration

Thanks for signing up for Jira Ops! I’m Matt Ryall, leader for the Jira Ops product team at Atlassian. Since this is a brand new product, we’ll be delivering improvements quickly and sharing updates...

417 views 0 8
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