Copy Versions from one Project to Another

Overview

Bulk copy all versions from a source project to a destination project.

Example

As a project manager, I want to apply the same versions to different projects. I can copy all versions existing in a source project to a destination project.

Good to Know

  • Versions with the same name should not already exist in the destination project.
  • You can fine-tune the versions and data to copy based on specific logic. Check the versions properties at the REST API documentation.

Requirements

Jira Jira

// Specify the master project  to get the versions form
final sourceProjectKey = "SRC"

// Specify the key of the project to copy the version to
final destinationProjectKey = "DST"

// Get the project versions
def versions = get("/rest/api/2/project/${sourceProjectKey}/versions")
    .header('Content-Type', 'application/json')
    .asObject(List).body

// Loop over each version returned and create a version in the new project
def successStatusByVersionId = versions.collectEntries { version ->
    // Copy the version and specify the destination project
    def versionCopy = version.subMap(['name', 'description', 'archived', 'released', 'startDate', 'releaseDate', 'project'])
    versionCopy.project = destinationProjectKey

    // Make the rest call to create the version
    logger.info("Copying the version with id '${version.id}' and name '${version.name}'")
    def createdVersionResponse = post("/rest/api/2/version")
        .header("Content-Type", "application/json")
        .body(versionCopy)
        .asObject(Map)

    // Log out the versions copied or which failed to be copied
    if (createdVersionResponse.status == 201) {
        logger.info("Version with id '${version.id}' and name '${version.name}' copied. New id: ${createdVersionResponse.body.id}")
    } else {
        logger.warn("Failed to copy version with id '${version.id}' and name '${version.name}'. ${createdVersionResponse.status}: ${createdVersionResponse.body}")
    }

    [(version.id): (createdVersionResponse.status == 201)]
}

"Status by source version id (copied?): ${successStatusByVersionId}"
Discovered an issue? Report it here

Suggested for you