This is an example os Script Listener using the plugin Adaptavist Scriptrunner.

In this example we will try to sync components between Jira projects. This example of sync is only one direction, in other words, there is a “master” Project where we add new Components, and those Components are added at same time to the destination Projects.

For this exercise, we will create a new Script Listener.

The Listener will be configured to listen to the “ProjectComponentCreatedEvent” in the source (master) of Components Jira Project.

As you can see in the picture, just using a code like this and replacing the first two variables (dest_prj, source_prj) by your Project Keys is enough to start the sync.

def dest_prj = ["DESTPRJ1","DESTPRJ2"]
def source_prj = "SOURCEPRJ"

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.bc.project.component.ProjectComponent

def projectManager = ComponentAccessor.getProjectManager()
def projectSource = projectManager.getProjectObjByKey(source_prj).getId() 
def projectComponentManager = ComponentAccessor.getProjectComponentManager()
Collection<ProjectComponent> componentList = projectComponentManager.findAllForProject(projectSource) as Collection<ProjectComponent>

def i = 0
while ( i < dest_prj.size() ) {
	def projectDestinationList = projectManager.getProjectObjByKey(dest_prj[i])
	def project = projectDestinationList

	if (componentList != null) {
         for (component in componentList) {
            //log.debug("Component " + component.getName() ) 
            def componentTemp = projectComponentManager.findByComponentName(project.getId(), component.getName())
            if  (componentTemp == null) {
                //log.debug("Now adding component " + component.getName() + " to " + project.getName())
                def componentTempResult = projectComponentManager.create(component.getName(), component.getDescription(), component.getLead(), 1,project.getId())
                // 1= COMPONENT_LEAD, 2= PROJECT_DEFAULT, 3 =PROJECT_LEAD , 4= UNASSIGNED           
         }        
   		}
	}
	i++
}

That’s all!

By MrAddon

.

Posted by:.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s