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

Susan Hauth Community Champion Oct 09, 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

3 votes
Accepted answer

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 Community Champion Oct 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

I hope so - you can test from Scriptrunner adding line

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

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)

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)

 

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

 

At the end of the code, you have this:

if (checkValue) {
    watchUsers(user)
}

But you never define the "user" variable.

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?

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

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

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

Henning

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

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)

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

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);

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

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

For what kind of script? Post function?

0 votes
Susan Hauth Community Champion Oct 10, 2012

Groovy Script Runner post function.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted 47m ago in United States

Topic Tuesday, now on Thursday! :-P

Happy Thursday everyone! This is usually a Tuesday thing but I've been crazy busy.  I finally had a moment to breath and so here we are. Atlassian has several tools and features to help users ...

6 views 0 0
View post

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