Add Project Lead automatically to Watcher list when the issue is created

I'm trying to add a groovy script to custom listener (Admin -> Script Listeners), But it looks something I'm missing since the project lead was not added automatically to watcher list. Any help would be much appreciated! Thank you!!!

Below is the groovy script I'm using:

package com.custom

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.util.ErrorCollection
import com.onresolve.jira.groovy.canned.CannedScript
import com.onresolve.jira.groovy.canned.utils.ConditionUtils
import org.apache.log4j.Category
import com.atlassian.crowd.embedded.api.User
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent


class AddProjectLeadAsWatcher extends AbstractIssueEventListener {

    //Category log = Category.getInstance(AddWatcher.class)

    String getName() {
        "Adds the project lead as a watcher"
    }

    public String getHelpUrl() {
        "https://studio.plugins.atlassian.com/wiki/display/GRV/Listeners#Listeners-AddWatcher"
    }

    String getDescription() {
        "Adds the project lead as a watcher, if condition applies"
    }

    List getCategories() {
        ["Function", "Listener"]
    }

    List getParameters(Map params) {
        [
            ConditionUtils.getConditionParameter()
        ]
    }

    ErrorCollection doValidate(Map params, boolean forPreview) {
        null
    }

    Map doScript(Map params) {
        MutableIssue issue = params['issue'] as MutableIssue

        Boolean doIt = ConditionUtils.processCondition(params[ConditionUtils.FIELD_CONDITION] as String, issue, false, params)
        if (! doIt) {
            return [:]
        }

        ComponentManager componentManager = ComponentManager.getInstance()
        WatcherManager watcherManager = componentManager.getWatcherManager()
        IssueManager issueManager=componentManager.getIssueManager()

	def projectid=issue.project
	ProjectManager projectManager=componentManager.getProjectManager()
	Project project=projectManager.getProjectObj(projectid)
	User projectlead=project.getProjectLead()
        watcherManager.startWatching(projectlead, issue.genericValue) 

        params
    }

    String getDescription(Map params, boolean forPreview) {
        StringBuffer sb = new StringBuffer()
        sb << "Project lead user will be added to watcher list of issue"
        if (params[ConditionUtils.FIELD_CONDITION]) {
            sb << " if this condition is true:<br><pre>${params[ConditionUtils.FIELD_CONDITION]}</pre>"
        }
        else {
            sb << "."
        }
    }

    Boolean isFinalParamsPage(Map params) {
        true
    }
}

1 answer

1 accepted

I'm posting the groovy solution that worked for me. It could help some one.

1. Save the below script as a .groovy file named AddProjectLeadAsWatcher.groovy to the following location on JIRA server:
<JIRA-Installation-Directory>\atlassian-jira\WEB-INF\classes\com\custom

2. Login into Admin panel of JIRA 
	a)	Go to Plugins
	b)	Go to Script Listeners
	c)	Click Custom Listener (Under Add Listener)
	d)	Select required events and projects(on our case, event should be selected as “Issue Created” and project should be selected as “All Projects”)
	e)	Type the name of the groovy class as com.custom.AddProjectLeadAsWatcher
	f)	To ensure the class name is correct, click Preview and if it succeeds, click “Add Listener”

3. Restart JIRA service

package com.custom
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.IssueManager
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.event.api.EventListener
import org.apache.log4j.Category

class AddProjectLeadAsWatcher extends AbstractIssueEventListener {

Category log = Category.getInstance(AddProjectLeadAsWatcher.class)
        
@Override
void workflowEvent (IssueEvent event) {        	
                
	ComponentManager componentManager = ComponentManager.getInstance()
	IssueManager issueManager = componentManager.getIssueManager()		
	GenericValue issueGV = issueManager.getIssue(event.issue.getKey())
			
	def projectid = issueGV.project
	ProjectManager projectManager = componentManager.getProjectManager()
	Project project = projectManager.getProjectObj(projectid)
	User projectlead = project.getLeadUser()
	WatcherManager watcherManager = componentManager.getWatcherManager()
	watcherManager.startWatching(projectlead, issueGV)

        }         
}

					
				
			
			
			
				

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

73 views 0 5
Read article

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