Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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 

1 answer

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

Hi @vasanth

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

Please accept the answer.

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you