Synchronize versions between projects

Camille Lecerf December 21, 2017

How could I sycnhronize versions between 2 projects please ? Or copy them from one to another ?

 

ps : I have scriptrunner so if you want to share your script with me I would be grateful to you.

 

Camille

4 answers

3 accepted

3 votes
Answer accepted
Stefaan Quackels
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 21, 2017

You can actually also rely on a synchronizer plugin such as Exalate. This plugin would allow to keep synchronized versions (and issues under it) between two different projects.

Let me know if you need any additional help figuring this out.

1 vote
Answer accepted
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 21, 2017

In ScriptRunner your script would look something like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.version.Version

def projectManager = ComponentAccessor.getProjectManager()
def projectSource = projectManager.getProjectObjByKey("SOURCE_PROJECT_KEY") // change project name here
def projectDestination = projectManager.getProjectObjByKey("DESTINATION_PROJECT_KEY") // change project name here

def versionManager = ComponentAccessor.getVersionManager()
List<Project> projectList = new ArrayList<>();
projectList.add(projectSource)

Collection<String> versionList = versionManager.getAllVersionsForProjects(projectList, false).stream().map{it.getName()}.collect()

for (version in versionList) {
log.debug("Now adding version " + version + " to " + projectDestination.name)
versionManager.createVersion(version, new Date(), new Date() + 7, "New version description", projectDestination.id, null)
}

You need to add logic how to handle already existing versions. 

Verhás István
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 8, 2018

Hi @Alexey Matveev,

Good job. I have enhanced it a little, not to loose any metadata of versions

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.version.Version

def projectManager = ComponentAccessor.getProjectManager()
def projectSource = projectManager.getProjectObjByKey("SOURCE_PROJECT_KEY") // change project name here
def projectDestination = projectManager.getProjectObjByKey("DESTINATION_PROJECT_KEY") // change project name here

def versionManager = ComponentAccessor.getVersionManager()
List<Project> projectList = new ArrayList<>();
projectList.add(projectSource)

Collection<Version> versionList = versionManager.getAllVersionsForProjects(projectList, false)

for (version in versionList) {
log.debug("Now adding version " + version + " to " + projectDestination.name)
versionManager.createVersion(version.name, version.startDate, version.releaseDate, version.description, projectDestination.id,null, version.released)
}

The existing version logic is still missing.

Sarath January 15, 2019

how to modify the code where if the version in the source project has word 'abc' then it should be created in project a, if the version in the source has word 'xyz' then it should be created in project b?

Verhás István
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 15, 2019

Hi @Sarath,

You have to define two destination projects, projectDestinationA and projectDestinationB. In the last line of createVersion you need an 'if' to select the proper project to create the version in.

Be warned that you have only specified only two cases of four ones. Missing the case when both abc and xyz can be found in the source version, and the other missing case when none of them is there.

 

Regards,

Istvan

Like Sarath likes this
Sarath January 18, 2019
Thank you @Verhás István. I modified in this way: 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.version.Version

def projectManager = ComponentAccessor.getProjectManager()
def projectSource = projectManager.getProjectObjByKey("KEY") // change project name here
def projectDestination1 = projectManager.getProjectObjByKey("KEY1") // change project name here
def projectDestination2 = projectManager.getProjectObjByKey("KEY2") // change project name here
def projectDestination3 = projectManager.getProjectObjByKey("KEY3") // change project name here

def versionManager = ComponentAccessor.getVersionManager()
List<Project> projectList = new ArrayList<>();
projectList.add(projectSource)

Collection<Version> versionList = versionManager.getAllVersionsForProjects(projectList, false)

for (version in versionList) {
def versionname = version.getName()

if (versionname.length() >= 3 && versionname.substring(0, 3).equalsIgnoreCase("abc")) {
log.debug("Now adding version " + version + " to " + projectDestination1.name)
versionManager.createVersion(version.name, version.startDate, version.releaseDate, version.description, projectDestination1.id,null, version.released)
} else if (versionname.length() >= 10 && versionname.substring(0, 10).equalsIgnoreCase("def")) {
log.debug("Now adding version " + version + " to " + projectDestination2.name)
versionManager.createVersion(version.name, version.startDate, version.releaseDate, version.description, projectDestination2.id,null, version.released)
} else if (versionname.length() >= 7 && versionname.substring(0, 7).equalsIgnoreCase("ghi")) {
log.debug("Now adding version " + version + " to " + projectDestination3.name)
versionManager.createVersion(version.name, version.startDate, version.releaseDate, version.description, projectDestination3.id,null, version.released)
} else {
//do nothing
}
}

My question is, how I need to load it as a custom listener? 

Steps I followed to create the file:

1. Select ""Custom listener"
2. ProjectKey: Selected both source and destination projects
3. Events: I'm not sure what to select, I selected generic one "projectupdatedEvent"
4. Used the above inline code.

Though there are no errors in the code it wasn't copying the version names.

Verhás István
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 18, 2019

Hi @Sarath,

The modification seems to be perfect. Actually, this script is intended to be used in script runner terminal window, not as a listener. This script is able to synchronize the existing versions, while the listener approach targets the versions to be created in the future.

As it is mentioned above please visit the documentation of the Version Listener at https://scriptrunner.adaptavist.com/latest/jira/listeners.html#_version_synchroniser_listener .

 

Regards,

Istvan 

1 vote
Answer accepted
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 21, 2017

Script runner has built in script for it (in Script listener section)

Version synchroniser
Synchronises versions across multiple projects

Out of the box it's not supported in JIRA

https://jira.atlassian.com/browse/JRASERVER-2698?focusedCommentId=1691393&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-1691393

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 21, 2017
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 21, 2017

from the docs

"

Please understand the functionality of the listener before implementing it in your production environment. The listener propagates the following:

  1. version creates to all target projects, when a version is created in the source project"

Like Sarath likes this
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 21, 2017

And if you want to use groovy script in the script console to copy the version via a script then it can be easily achieved using VersionManager

https://docs.atlassian.com/DAC/javadoc/jira/7.1.0-m01/reference/com/atlassian/jira/project/version/VersionManager.html

You can get all Versions for specific project and then create versions for desired projects.

Like wajih zouaoui likes this
Camille Lecerf December 21, 2017

Thank you @Tarun Sapra for your great help ! I am going to use the script listener "Version synchroniser" 

 

Thank you also @Alexey Matveev and @Stefaan Quackels for your help

0 votes
Fede Espinoza June 26, 2019

Jira CLI has `copyVersion` and `copyVersions` options.
https://bobswift.atlassian.net/wiki/spaces/JCLI/pages/6684679/Reference#Reference-copyVersion

copyVersion : Copy a version from one project to the same project or another project.
copyVersions : Copy all versions from one project to another project.
e.g. jira.sh --action copyVersions --project "zjiracli" --toProject "zjiracliC" --replace

 

Suggest an answer

Log in or Sign up to answer