JIRA: Add watchers at issue creation

After struggling with this feature request from users for quite some time, I have a way now to add a list of watchers to an issue automatically at creation.

The secret here is (1) you need to add a scripted post function to the creation step in your workflow (so this may mean seperate workflows for individual projects) and (2) you need the Script Runner plugin.

Add the following groovy script somewhere in your CATALINA_BASE and call it in your workflow.

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", "usernames"]
   watchUsers(users)

Special thanks to Brian LeGros for help on this too!

Advertisements

  1. #1 by Vijay on July 19, 2011 - 12:28 pm

    Thanks a lot for the script. I have been struggling with it since long time.

  2. #2 by Matt Doar on July 20, 2011 - 5:23 pm

    You could also create a postfunction with a configurable list of users to add. Doing this per issue is a bit harder, but can be done if you add a new userpicker custom field and configure the postfunction to name the field to pull users from.

  3. #3 by Wayne Gjaltema on January 25, 2012 - 5:53 pm

    I couldn’t get this to work for me. I have Jira 4.3.4. I get a null pointer exception on line watcherManager.startWatching(user, issue.getGenericValue())

    The problem seems to be with the second parameter – the issue.

  4. #4 by Lokesh on February 29, 2012 - 10:47 am

    I get the error as follows & it is not settign watchers

    2012-02-29 10:24:33,558 QuartzWorker-0 ERROR ServiceRunner KYCTIG [jira.issue.watchers.DefaultWatcherManager] You must specify an issue.
    2012-02-29 10:24:33,559 QuartzWorker-0 ERROR ServiceRunner KYCTIG [jira.issue.watchers.DefaultWatcherManager] You must specify an issue.
    2012-02-29 10:24:33,559 QuartzWorker-0 ERROR ServiceRunner KYCTIG [jira.issue.watchers.DefaultWatcherManager] You must specify an issue.

    Any idea where this “issue” needs to be set

    • #5 by Justin on April 5, 2012 - 5:22 pm

      I think this has to do with the ordering of the post function. Make sure the Create Issue post function executes *before* the autowatch post function.

  5. #6 by thagrasshoppa on May 16, 2012 - 2:55 pm

    I created the post function fine, and can create new task without issue, but it never adds watchers.

    Is it possible for you to go into any more detail regarding this process? I see some differences in my screenshot vs yours…(new vs open after create issue.)

    Thanks for creating this!!

  6. #7 by jechlin on May 17, 2012 - 7:23 pm

    The code as written is for a post-function, not a service. Lokesh seems to be running it as a service, in which case “issue” doesn’t refer to anything.

  7. #8 by jechlin on May 17, 2012 - 7:23 pm

    The script as written is for a post-function… if you’re running it as a service as Lokesh is, issue would not refer to anything.

  8. #9 by thagrasshoppa on May 22, 2012 - 1:10 pm

    I have run this as post function, but it looks like it does nothing. No errors, but no watchers either.

  9. #10 by Tim Cusack on May 30, 2012 - 6:41 pm

    Anyone have any other ideas? I am not really sure where to find out if there is an error, I have looked through the normal logs, but I do not really see any errors that stand out.

    • #11 by Justin on May 31, 2012 - 1:16 am

      Where on the server is your script located? How are you calling this in your workflow (screenshot)? What version of JIRA are you running?

      • #12 by Tim Cusack on May 31, 2012 - 2:50 pm

        I can not seem to attach a screenshot here, but here is a grab of the text in the workflow section:

        All Validators (1) Post Functions (4)
        Add a new post function to the unconditional result of the transition.

        Creates the issue originally.
        — THEN
        Script c:\Program Files\Atlassian\JIRA 4.3\atlassian-jira\WEB-INF\scripts\groovy\AddWatcher.groovy will be run.
        Edit | Move Up | Move Down | Delete
        — THEN
        Re-index an issue to keep indexes in sync with the database.
        — THEN
        Fire a Issue Created event that can be processed by the listeners.
        Edit

        We are running Jira 4.3.

        I wonder, do you have to restart Jira in order to have this script run?

        That is the only thing I have not done.

      • #13 by Justin on August 22, 2012 - 8:30 pm

        Sorry it took me a while to reply to this. Do you have the groovyrunner plugin in your lib (plugins v1) path? If you do not, you will need to install that and restart JIRA for this post function to work.

  10. #14 by Colin moore on August 22, 2012 - 8:26 pm

    can you execute this script using Jelly Runner? I’m not familiar with Jelly Scripting myself so am unsure if the jelly runner tool can execute the above code

    • #15 by Justin on August 22, 2012 - 8:28 pm

      Nope — this is strictly a groovy post function.

  11. #16 by Matt Doar on January 24, 2014 - 2:14 am

    Thanks, this was helpful today

  12. #17 by sgaur on June 4, 2014 - 9:15 pm

    Is there a way to only add watchers/users to specific components?

  13. #18 by Jörg Spiegelhoff on September 1, 2016 - 12:26 pm

    You can also use a plugin which is able to add watchers from create screen…have a look at https://marketplace.atlassian.com/plugins/de.codecentric.jira.wes/server/overview

    The discussed Use case is described here: https://codecentric.atlassian.net/wiki/display/WE/Post-functions#Post-functions-ManageWatchers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: