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

vasanth 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

1 vote
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 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 September 29, 2021
2 votes
Ahmed Salem Iskander 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 February 27, 2024

this did not update the dest project. :(

Eric Sebian February 27, 2024

scratch that. It did. 

Suggest an answer

Log in or Sign up to answer