The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
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
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:-
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:-
Below are some test screens:-
1) Below is the version screen for the Communication project before the script is executed.
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:-
3) Now, if you go to the version page once again, you will see that the versions have been added, as shown below:-
I hope this helps to solve your question. :)
Thank you and Kind Regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @vasanth
I'm glad to hear the solution helped. :)
Please accept the answer.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Connect with like-minded Atlassian users at free events near you!
Find an eventConnect with like-minded Atlassian users at free events near you!
Unfortunately there are no Community Events near you at the moment.
Host an eventYou're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.