Jira copy project components/Fix versions to another project component/Fix versions in Data center

vasanth
Contributor
July 13, 2021

HI 

I am tryin to 

copy project components/Fix versions to another project component/Fix versions in Data center.

I used approach CSV import but it is missing few data still,

can anyone tell any other approach by using Rest API or Script runner through.

 

Thank you 

2 answers

1 accepted

2 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 15, 2021

Hi @vasanth

For your requirement, you will need to use the REST Endpoint to get the versions from the previous project and the ScriptRunner console to copy the versions into the second project.

For the REST Endpoint, you can try something like this:-

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate
convertToJson { MultivaluedMap queryParams ->

def hostUrl = "http://localhost:9091"
def userName = "admin"
def password = "q"
def projectKey = "SFFM"

def httpBuilder = new HTTPBuilder(hostUrl)

def versions = [] as List

def issueJson = httpBuilder.request(Method.GET, ContentType.JSON) { req ->

uri.path = "/rest/api/2/project/${projectKey}/versions"

headers.'Authorization' = "Basic ${"${userName}:${password}".bytes.encodeBase64()}"

response.failure = { resp, reader ->
log.warn "Failed to query JIRA API: ${reader.errorMessages}"
return
}
}
issueJson.collect { Map row ->
versions.add(row.get("name"))
}

return Response.ok(new JsonBuilder(versions).toPrettyString()).build()
}

Please note, the sample codes provided are not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a print screen of the REST Endpoint configuration:-

rest_endpoint_config.png

If you notice in the code, the versions are taken from the  Scripted Field For Mail (SFFM) project. 

Once your REST Endpoint is configured, you will need to invoke it using the ScriptRunner console. 

Below is a sample code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import groovy.json.JsonSlurper

def projectManager = ComponentAccessor.projectManager
def versionManager = ComponentAccessor.versionManager

def baseUrl = "http://localhost:9091"

final String projectKey = "COM"

final Date startDate = null

final Date releaseDate = null

final String description = null

final Long scheduleAfterVersion = null

final boolean released = false

def project = projectManager.getProjectObjByKey(projectKey)

def hostUrl = "${baseUrl}/rest/scriptrunner/latest/custom/convertToJson"

def response = hostUrl.toURL().text

def json = new JsonSlurper().parseText(response)

def artifacts = json.collect().sort()

artifacts.each {
versionManager.createVersion(it.toString(), startDate, releaseDate, description, project.id, scheduleAfterVersion, released)
}

In the code above, the code is to be stored in the Communications (COM) project.

Below is a print screen of the Script Console:-

console_config.png

Below are some test screens:-

1) Below is the version screen for the Communication project before the script is executed.

before.png

2) Once the script is executed on the Script Console, the list of versions will be displayed in a list as shown in the image below:-

console_output.png

3) Now, if you go to the version page once again, you will see that the versions have been added, as shown below:-

after.png

I hope this helps to solve your question. :)

Thank you and Kind Regards,

Ram

vasanth
Contributor
July 20, 2021
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 20, 2021

Hi @vasanth

I'm glad to hear the solution helped. :)

Please accept the answer.

Thank you and Kind regards,

Ram

Like vasanth likes this
vasanth
Contributor
September 29, 2021
Ben Radlinski
Contributor
May 8, 2024

Hello @Ram Kumar Aravindakshan _Adaptavist_ , I'm trying to get this to work in Data Center. I created the REST Endpoint with the Key from the source project and ran the script with the destination project key. I've tried it with http and https. It runs and nothing happens, and the results are empty. I've tried the REST endpoint with my regular user/pass and user/api key (I am a system admin).

Is there any other setting I need to customize other than 

def hostUrl

def userName

def password

def projectKey

def baseUrl

final String projectkey 

?

Any help would be appreciated and I can also open a ticket with Adaptavist if that would be easier.

 

4 votes
Ahmed Salem Iskander
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 6, 2022

Using the Console of ScriptRunner you can use the below code to clone the versions of the Master project to sub-projects.

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("SourceProjectKey") // Source project name here
def projectDestination1 = projectManager.getProjectObjByKey("DestinationProjectKey") // Destination 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) {
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 {
//do nothing
}
}

Eric Sebian
Contributor
February 27, 2024

this did not update the dest project. :(

Eric Sebian
Contributor
February 27, 2024

scratch that. It did. 

Rinaldi Michael May 27, 2024

Works perfectly! Thanks a lot!

Rinaldi James Michael
Contributor
May 30, 2024

I've modified the code to consider a few more scenarios.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Created - 6th June 2022
//Last Modified - 28th May 2024, 03:48 PM
//Authors - Ahmed Salem Iskander (from community post)
//Modifiers - Rinaldi Michael
//References -
//https://community.atlassian.com/t5/Jira-questions/Jira-copy-project-components-Fix-versions-to-another-project/qaq-p/1749456
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.lang.String
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.version.Version
import com.onresolve.scriptrunner.parameters.annotation.*
import java.util.ArrayList


//**********************************************************//
//Get Input
@ShortTextInput(description = 'Type in the Project Key from where you would like to copy all Releases/Versions', label = 'Enter Source Project Key')
String sourceProject

@ShortTextInput(description = 'Type in the Project Key where you would like to copy all Releases/Versions into', label = 'Enter Target Project Key')
String targetProject


//**********************************************************//
//Declare variables
def projectManager = ComponentAccessor.getProjectManager()
def projectSource = projectManager.getProjectObjByKey(sourceProject) // Source project
def projectDestination1 = projectManager.getProjectObjByKey(targetProject) // Destination project
def versionManager = ComponentAccessor.getVersionManager()


//Get source project object
List<Project> projectList = new ArrayList<>();
projectList.add(projectSource)
Collection<Version> versionList = versionManager.getAllVersionsForProjects(projectList, false)


//Get target project object
List<Project> projectList2 = new ArrayList<>();
projectList2.add(projectDestination1)
Collection<Version> versionList2 = versionManager.getAllVersionsForProjects(projectList2, false)


//Set output variable
String printtext = ""

//**********************************************************//
//Recreate all versions in the new project
printtext+="<b>${versionList.size()}</b> Versions will now be copied from <b>${projectSource.getName()}"
printtext+=" (Key: ${projectSource.getKey()})(ID: ${projectSource.id})</b> to <b>${projectDestination1.getName()}"
printtext+=" (Key: ${projectDestination1.getKey()})(ID: ${projectDestination1.id})</b>"


//Loop through all values that should be copied
for(int v=0;v<versionList.size();v++)
{
def version = versionList[v] //Source project version
def versionname = version.getName() //Source project version name


boolean alreadyExists = false
//Verify if a Fix version with the below details already exists
for(int fv=0;fv<versionList2.size();fv++)
{
if(version.name == versionList2[fv].name)
{
def existingPosition = fv

//This part is only needed if every detail of the version should be compared
/*
if(versionList2[v].name==versionList[existingPosition].name
&& versionList2[v].startDate==versionList[existingPosition].startDate
&& versionList2[v].releaseDate==versionList[existingPosition].releaseDate
&& versionList2[v].description==versionList[existingPosition].description
&& versionList2[v].released==versionList[existingPosition].released)
{
*/
printtext+="<br><br>${v+1}. Fix Version/Release with the below details <b>already exist</b> in the destination project<br>"
printtext+="<b>Name</b>: ${versionList2[fv].name}<br>"
printtext+="<b>Start Date</b>: ${versionList2[fv].startDate}<br>"
printtext+="<b>Release Date</b>: ${versionList2[fv].releaseDate}<br>"
printtext+="<b>Description</b>: ${versionList2[fv].description}<br>"
printtext+="<b>Destination Project ID</b>: ${projectDestination1.id}<br>"
printtext+="<b>Is Released?</b>: ${versionList2[fv].released}"

alreadyExists=true
break;
//}
}
}

if(alreadyExists)
continue;

//Create the copy versions
if (versionname.length() >= 3)
{
log.debug("Now adding version " + version + " to " + projectDestination1.name)
versionManager.createVersion(version.name, version.startDate, version.releaseDate, version.description, projectDestination1.id,null, version.released)

printtext+="<br><br>${v+1}. Version created with details:<br>"
printtext+="<b>Name</b>: ${version.name}<br>"
printtext+="<b>Start Date</b>: ${version.startDate}<br>"
printtext+="<b>Release Date</b>: ${version.releaseDate}<br>"
printtext+="<b>Description</b>: ${version.description}<br>"
printtext+="<b>Destination Project ID</b>: ${projectDestination1.id}<br>"
printtext+="<b>Is Released?</b>: ${version.released}"
}
else
{
printtext+="<br>${v+1}. Version not found/added"
}
}

return printtext

 

Like # people like this

Suggest an answer

Log in or Sign up to answer