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
Community moderators have prevented the ability to post new answers.
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)
Hi Jozef,
I think that might do. So the "userid" would be added as a watcher to the issue post function?
Thanks...Susan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I hope so - you can test from Scriptrunner adding line
issue = ComponentAccessor.getIssueManager().getIssueObject("KEY-123")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
At the end of the code, you have this:
if (checkValue) { watchUsers(user) }
But you never define the "user" variable.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What is the -> 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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It's "->". I corrected the source.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Is it in the list of postfunctions *after* the issue creation? The error message hints at issue == null...
Henning
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Henning. Was so focused on the code that I forgot the sequence of events. That was it. Works beautifully :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.
Groovy Script Runner post function.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For what kind of script? Post function?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.