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
.
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
LikeLike
Hello Salim,
Here you can see an example to obtain the type of event:
https://community.atlassian.com/t5/Marketplace-Apps-Integrations/Jira-Custom-Listener-with-event-ProjectComponent/qaq-p/1047574
Because you must listen events of different types: ProjectComponentUpdatedEvent and ProjectComponentCreatedEvent in your case
Hiope this info helps
Best regards 🙂
LikeLike
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 ?
LikeLike
Use class Project to obtain the Project list ( List getProjectObjects() )
https://docs.atlassian.com/software/jira/docs/api/7.2.2/com/atlassian/jira/project/ProjectManager.html#getProjectObjects–
LikeLike
something doesn’t work for me.
maybe there is an example?
LikeLike
An example using Permission Manager in order to show only the projects user has access:
LikeLike
Another example:
LikeLike