How do I add a specific user as a watcher using script runner

Susan Hauth _Jira Queen_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 9, 2012

Hi,

I'm not great at scripting and wondering if anyone could help me out with a code snippit that would add a specific user as a watcher based on a condition. The condition part is easy using script runner, but there only seems to be a canned script for adding the current user as a watcher which is no what I wish.

Any help is much appreciated.

Susan

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

6 votes
Answer accepted
Jozef Kotlár
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.
October 10, 2012

There are sources of canned scripts - have a look there. It is something like this:

import com.atlassian.jira.component.ComponentAccessor

def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager() 

def user = userManager.getUserObject("userid") 
if (user) watcherManager.startWatching(user, issue.genericValue)

Susan Hauth _Jira Queen_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 10, 2012

Hi Jozef,

I think that might do. So the "userid" would be added as a watcher to the issue post function?

Thanks...Susan

Jozef Kotlár
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.
October 10, 2012

I hope so - you can test from Scriptrunner adding line

issue = ComponentAccessor.getIssueManager().getIssueObject("KEY-123")

2 votes
StrikeAd Infrastructure November 8, 2012

I've found answer here - http://justinit.wordpress.com/2010/12/17/jira-add-watchers-at-issue-creation/

Code is:

package com.onresolve.jira.groovy.canned.workflow.postfunctions

import com.atlassian.jira.ComponentManager

def componentManager = ComponentManager.getInstance()
def watcherManager = componentManager.getWatcherManager()
def userManager = componentManager.getUserUtil()

def watchUsers = {usernames ->
   usernames.each {
         def user = userManager.getUser(it)
         watcherManager.startWatching(user, issue.getGenericValue())
      }
}

   def users = ["comma", "separated", "names"]
   watchUsers(users)

It works perfectly as a Workflow Post-Function (on Jira 5.0.x)

NathanG
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.
April 8, 2016

Something must have changed in JIRA 7, I updated the code to this:

 

import com.atlassian.jira.component.ComponentAccessor
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 = ["comma", "separated", "names"]
watchUsers(users)
Like Johannes likes this
Samuel April 22, 2016

 

Nathan Given  Its not working for me. Here is my code:

 

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf1 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Custom_field-1"}
def cf2 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Custom_field-2"}
def cf1Value = issue.getCustomFieldValue(cf1)
def cf2Value = issue.getCustomFieldValue(cf2)
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 = ["user1", "user2"]
def checkValue = ((cf1Value in "val 1") && (cf2Value in "val 2" || cf2Value in "val 3"))
 
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "--------------- ADD WATCHER/S --------------"
 
if (checkValue) {
    watchUsers(user)
}

In the log, it throws an error message – > "You must specify a user" 

Can you help?

I used following method and its working fine but I cannot remove the watcher manually. It is auto added again if page is refreshed or transitioned or with any updates.

 

watcherManager.startWatching(users, issue)

Thanks

 

NathanG
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.
April 25, 2016

At the end of the code, you have this:

if (checkValue) {
    watchUsers(user)
}

But you never define the "user" variable.

Maya_Chase January 4, 2018

What is the -&gt in the line:

def watchUsers = {usernames ->

I get an error if I put that in as a literal, but I can't figure out what it might be in place of?

Henning Tietgens
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.
January 4, 2018

It's "->". I corrected the source.

William Griffin February 7, 2018

I'm having issues with getting the code above to work in a post function on create. Any help is greatly appreciated...

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.comments.CommentManager

def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
def watchUsers = {usernames ->
usernames.each {
ApplicationUser user = userManager.getUserByName(it.toString())
watcherManager.startWatching(user, issue)
}
}

def users = ["flastname", "flastname"]
watchUsers(users)

------------------------------

Here is the error in post function after execution:

Time (on server): Wed Feb 07 2018 17:02:11 GMT-0500 (EST)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2018-02-07 22:02:11,504 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-02-07 22:02:11,505 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.NullPointerException
 at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:210)
 at com.google.common.cache.LocalCache$LocalManualCache.invalidate(LocalCache.java:4764)
 at com.atlassian.jira.issue.watchers.DefaultWatcherManager.updateWatch(DefaultWatcherManager.java:172)
 at com.atlassian.jira.issue.watchers.DefaultWatcherManager.startWatching(DefaultWatcherManager.java:89)
 at com.atlassian.jira.issue.watchers.DefaultWatcherManager.startWatching(DefaultWatcherManager.java:80)
 at com.atlassian.jira.issue.watchers.WatcherManager$startWatching.call(Unknown Source)
 at Script253$_run_closure1$_closure2.doCall(Script253.groovy:7)
 at Script253$_run_closure1.doCall(Script253.groovy:5)
 at Script253.run(Script253.groovy:11)

 

Jira Version: v7.3.2

Henning Tietgens
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.
February 7, 2018

Is it in the list of postfunctions *after* the issue creation? The error message hints at issue == null...

Henning

William Griffin February 8, 2018

Thanks Henning. Was so focused on the code that I forgot the sequence of events. That was it. Works beautifully :)

Paul Stockinger February 16, 2018

Similar to Samuel's issue, this works great if I hard code the user names in. However, I would like to pick up a few values from single user custom fields on my screen in a later transition (no on create). I have tried several angles to pull in the text from these fields, but the post function logs the following.

ERROR [watchers.DefaultWatcherManager]: You must specify a user.

 

def cfManager = ComponentAccessor.getCustomFieldManager();
def cf = cfManager.getCustomFieldObjectByName("Editor");
String userName = issue.getCustomFieldValue(cf);
def cf1 = cfManager.getCustomFieldObjectByName("Writer");
String userName1 = issue.getCustomFieldValue(cf1);

def changeHolder = new DefaultIssueChangeHolder()
MutableIssue issue = issue

def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
def u1 = ComponentAccessor.getUserManager().getUserByName(userName);
def u2 = ComponentAccessor.getUserManager().getUserByName(userName1);
def watchUsers = {usernames ->
    usernames.each {
        def user = userManager.getUserByKey(it.toString())
        watcherManager.startWatching(user, issue)
    }
}
def users = [userName, u2]
watchUsers(users)

Henning Tietgens
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.
February 19, 2018

Hi Paul,

a single user picker field returns an ApplicationUser object for getCustomFieldValue() and not the username. So in this case you could use this return value directly as argument for startWatching().

Henning

Paul Stockinger February 19, 2018

Thank you Henning. I dropped the watchUsers function and tried a direct write, but the log still throws the same error. I also tried cf and cf1, but those were rejected due to being in string format.

def cfManager = ComponentAccessor.getCustomFieldManager();
def cf = cfManager.getCustomFieldObjectByName("Editor");
String userName = issue.getCustomFieldValue(cf);
def cf1 = cfManager.getCustomFieldObjectByName("Writer");
String userName1 = issue.getCustomFieldValue(cf1);

def changeHolder = new DefaultIssueChangeHolder()
MutableIssue issue = issue

//def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
def u1 = ComponentAccessor.getUserManager().getUserByName(userName);
def u2 = ComponentAccessor.getUserManager().getUserByName(userName1);

watcherManager.startWatching(u1, issue);
watcherManager.startWatching(u2, issue);

Henning Tietgens
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.
February 19, 2018

As I wrote a single user picker field returns an ApplicationUser object for getCustomFieldValue() and not the username (String). So try this.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser

MutableIssue issue = issue
def cfManager = ComponentAccessor.getCustomFieldManager()
def watcherManager = ComponentAccessor.getWatcherManager()

def cf = cfManager.getCustomFieldObjectByName("Editor")
// better would be cfManager.getCustomFieldObject("customfield_12345") to be on the safe side (renaming/translation
// of cfs could cause problems)
def u1 = issue.getCustomFieldValue(cf) as ApplicationUser
def cf1 = cfManager.getCustomFieldObjectByName("Writer")
def u2 = issue.getCustomFieldValue(cf1) as ApplicationUser

watcherManager.startWatching(u1, issue)
watcherManager.startWatching(u2, issue)

Henning

Like Yoav Ashkenazi likes this
Paul Stockinger February 20, 2018

Perfect. Thank you, Henning. I really appreciate it.

0 votes
Susan Hauth _Jira Queen_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 10, 2012

Groovy Script Runner post function.

0 votes
Henning Tietgens
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.
October 10, 2012

For what kind of script? Post function?

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events