Forums

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

JIRA - Groovy - Remove User from Wachtlist

Antonio D'Errico August 8, 2016

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
August 8, 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)
Antonio D'Errico August 8, 2016

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
August 9, 2016

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

Like Евгений Баев likes this
0 votes
Jonny Carter
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 10, 2016

@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)
Antonio D'Errico
Contributor
August 10, 2016

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
August 10, 2016

Have you imported the ComponenttAccessor class?

Antonio D'Errico
Contributor
August 10, 2016

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
August 10, 2016

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

 def user = userManager.getUserByName(userName)

Tarun Sapra
Community Champion
August 10, 2016

For error you have to see the log files.

Tarun Sapra
Community Champion
August 10, 2016

Also import  watcherManager

Jonny Carter
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 10, 2016

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.

Antonio D'Errico
Contributor
August 10, 2016

there is no error but it does not work.

Tarun Sapra
Community Champion
August 10, 2016

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

Antonio D'Errico
Contributor
August 10, 2016

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.

Antonio D'Errico
Contributor
August 10, 2016

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
August 10, 2016

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

Tarun Sapra
Community Champion
August 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
TAGS
atlassian, jira cloud certification, managing jira projects, jira project administration, jira cloud exam, atlassian certification, agile project management, jira workflows, jira permissions, jira training, jira cloud skills, atlassian learning

Become a Certified Jira Service Project Expert 🦸🏻‍♂️

Validate your expertise in managing Jira Service Projects for Cloud. Master configuration, optimize workflows, and manage users seamlessly. Earn global 🗺️ recognition and advance your career as a trusted Jira Service management expert.

Get Certified! ✍️
AUG Leaders

Atlassian Community Events