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

Mariselvam Lakshmanasamy May 27, 2014

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

0 votes
Answer accepted
Mariselvam Lakshmanasamy June 1, 2014

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