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:.

7 replies on “How to sync Components between Jira projects

  1. Hi
    thanks for your script
    But i would like add function for when i change the name of component , he change too in the project «dest_prj» because actually when i change the name of component in the projet «src_prj» he change good but in the project «src_prj» he create a new component with the name
    Thanks in advance

    Like

  2. good day. in this example, there are 2 dest projects, but what if there are 70 projects? How can I select all other projects except the source ?

    Like

      1. An example using Permission Manager in order to show only the projects user has access:

        import com.atlassian.jira.component.ComponentAccessor
        import com.atlassian.jira.permission.ProjectPermissions
        
        def permissionManager = ComponentAccessor.getPermissionManager()
        def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
        def projects = permissionManager.getProjects(ProjectPermissions.BROWSE_PROJECTS, user)
        

        Like

      2. Another example:

        import com.atlassian.jira.component.ComponentAccessor
        def projectManager = ComponentAccessor.getProjectManager()
        def sb = new StringBuffer()
        // Iterate over each Project name and get the info you needprojectManager.getProjectObjects().each {
        sb.append("Project Name: " + it.getName() + ", Project Lead: " + it.getLeadUserName() + "\n")
        }
        return sb.toString()
        

        Like

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 )

Facebook photo

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

Connecting to %s