Cloning a Bundle to Another Project

You can automatically clone bundle actions to another project. Following script listens for “BundleCreatedEvent” to copy a bundle from one project to another and also listens for “BundlecontentAddedEvent” for automatically updating bundles inside another project. It checks existence of version and component in the target project and if it can’t find a component or version with the same name as source project, it creates them.

import org.slf4j.*; import com.deniz.jira.versioning.*; import com.deniz.jira.versioning.bundles.*; import com.deniz.jira.versioning.bundles.events.*; import com.atlassian.jira.bc.project.component.*; import com.atlassian.jira.project.version.*; import com.atlassian.jira.project.*; import com.atlassian.jira.component.ComponentAccessor; //please enable logging for package "com.deniz.jira.versioning.scripting" from Administration/System/Logging and Profiling Logger log = LoggerFactory.getLogger(com.deniz.jira.versioning.scripting.ScriptingService.class); def bundleService = ComponentAccessor.getOSGiComponentInstanceOfType(BundleService.class); def projectManager = ComponentAccessor.getComponent(ProjectManager.class); def versionManager = ComponentAccessor.getComponent(VersionManager.class); def componentManager = ComponentAccessor.getComponent(ProjectComponentManager.class); def targetProjectKeys = ["ERP", "CLG"]; def event = parameters.event; def project = parameters.project; targetProjectKeys.each { targetProjectKey -> log.debug("Checking for project:{}", targetProjectKey); def targetProject = projectManager.getProjectByCurrentKey(targetProjectKey); if (event instanceof BundleCreatedEvent) { def sourceBundle = event.getBundle(); log.debug("a new bundle is created:{}", sourceBundle); BundleImp bundle = new BundleImp(sourceBundle); bundle.id = -1; bundle.projectId = targetProject.id; def sourceVersion = versionManager.getVersion(bundle.getVersionId()); if (sourceVersion != null) { //version is optional for bundles. def targetProjectVersions = versionManager.getVersions(targetProject); def targetVersion = targetProjectVersions.find {v -> v.name.equals(sourceVersion.name)}; if (targetVersion == null) { targetVersion = copyVersion(versionManager, targetProject, sourceVersion); } bundle.versionId = targetVersion.id; } bundleService.addBundle(bundle, false); } else if (event instanceof BundleContentAddedEvent) { def sourceBundleContent = event.bundleContent; log.debug("Component/Version is aded to bundle:{}", sourceBundleContent.bundleId); def sourceBundle = bundleService.getBundle(sourceBundleContent.bundleId); def bundlesWithSameName = bundleService.getBundles(sourceBundle.bundleName); targetBundle = bundlesWithSameName.find {b -> b.projectId == targetProject.id}; if (targetBundle) { log.debug("Corresponding bundle in project {} is {}", targetProject.key, targetBundle.id); def sourceComponentVersion = versionManager.getVersion(sourceBundleContent.versionId); //clone version if necessary def targetProjectVersions = versionManager.getVersions(targetProject); def targetComponentVersion = targetProjectVersions.find {v -> v.name.equals(sourceComponentVersion.name)}; if (targetComponentVersion == null) { targetComponentVersion = copyVersion(versionManager, targetProject, sourceComponentVersion); } //clone project if necessary def sourceComponent = componentManager.getProjectComponent(sourceBundleContent.componentId); def targetComponent = componentManager.findByComponentName(targetProject.id, sourceComponent.name); if (targetComponent == null) { targetComponent = copyComponent(componentManager, targetProject, sourceComponent); } def bundleContent = new BundleContentImp(); bundleContent.versionId = targetComponentVersion.id; bundleContent.componentId = targetComponent.id; bundleContent.bundleId = targetBundle.id; bundleService.addContentToBundle(bundleContent); } } } def copyVersion(versionManager, targetProject, sourceVersion) { log.debug("Creating target version '{}' in target project '{}'", sourceVersion.name, targetProject.key); return versionManager.createVersion( sourceVersion.name, sourceVersion.startDate, sourceVersion.releaseDate, sourceVersion.description, targetProject.id, null, //sequence sourceVersion.released ); } def copyComponent(componentManager, targetProject, sourceComponent) { log.debug("Creating target component '{}' in target project '{}'", sourceComponent.name, targetProject.key); return componentManager.create( sourceComponent.name, sourceComponent.description, sourceComponent.lead, sourceComponent.assigneeType, targetProject.id ); }