Snippets

Adaptavist Copy Project Versions Script Console Jira Cloud

Updated by Kristian Walker

File Copy Project VersionsScriptConsoleJiraCloud.groovy Modified

  • Ignore whitespace
  • Hide word diff
 // Specify the key of the project to copy the version to
 def projectToCopyVersionTo = "<ProjectKeyHere>"
 
-// get the project versions
-def versions = get("/rest/api/3/project/${masterProjectKey}/versions")
+// get the project object
+def project = get("/rest/api/3/project/${masterProjectKey}")
         .header('Content-Type', 'application/json')
-        .asObject(List).body
+        .asObject(Map).body
+        
+def versions = project.versions
 
 // Loop over each version returned and create a version in the new project
 versions.each {
Updated by Kristian Walker

File Copy Project VersionsScriptConsoleJiraCloud.groovy Modified

  • Ignore whitespace
  • Hide word diff
     }
 
     // Make the rest call to create the version
-    logger.info("Copying the version with the name of ${it.name.toString().replaceAll("\\[", "").replaceAll("\\]", "")}")
+    logger.info("Copying the version with the name of: ${it.name.toString().replaceAll("\\[", "").replaceAll("\\]", "")}")
     def createVersion = post("/rest/api/2/version")
             .header("Content-Type", "application/json")
             .body([
Updated by Kristian Walker

File Copy Project VersionsScriptConsoleJiraCloud.groovy Modified

  • Ignore whitespace
  • Hide word diff
     }
 
     // Make the rest call to create the version
+    logger.info("Copying the version with the name of ${it.name.toString().replaceAll("\\[", "").replaceAll("\\]", "")}")
     def createVersion = post("/rest/api/2/version")
             .header("Content-Type", "application/json")
             .body([
             "releaseDate": releaseDateValue,
             "project": projectToCopyVersionTo
     ]).asString()
-}
+}
+
+return "Versions Copied. Check the 'Logs' tab for more details"
Updated by Kristian Walker

File Copy Project VersionsScriptConsoleJiraCloud.groovy Modified

  • Ignore whitespace
  • Hide word diff
 * "All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner
 * license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed or
 * amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
-*/
+*/ 
+
+// NOTE: This script can only copy versions to a project where the versions do not already exist.  
 
 // Specify the master project  to get the versions form
 def masterProjectKey = "<ProjectKeyHere>"
Created by Kristian Walker

File Copy Project VersionsScriptConsoleJiraCloud.groovy Added

  • Ignore whitespace
  • Hide word diff
+/*
+* This example script  console script shows how you can get the versions from one project and copy them to another project on the Script Console.
+* "All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner
+* license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed or
+* amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
+*/
+
+// Specify the master project  to get the versions form
+def masterProjectKey = "<ProjectKeyHere>"
+
+// Specify the key of the project to copy the version to
+def projectToCopyVersionTo = "<ProjectKeyHere>"
+
+// get the project versions
+def versions = get("/rest/api/3/project/${masterProjectKey}/versions")
+        .header('Content-Type', 'application/json')
+        .asObject(List).body
+
+// Loop over each version returned and create a version in the new project
+versions.each {
+
+    // Define some boolean values for storing the archived and released properties for the version
+    boolean archivedValue;
+    boolean releasedValue
+
+    // Set the boolean values to the values from the returned version
+    if(it.archived.toString() == "true"){ 
+        archivedValue =true;
+    }else{
+        archivedValue =false;
+    }
+
+    if(it.released.toString() == "true"){
+        releasedValue =true;
+    }else{
+        releasedValue =false;
+    }
+
+    // Get todays date as a string for any date properties which dont have a value set
+    def today = new Date().format('yyyy-MM-dd').toString()
+
+    // Declare some variables to store the start and release date values
+    def startDateValue;
+    def releaseDateValue
+
+    // Get the start date  and if it is null set it to todays date as a start date is required when creating a version in a new project
+    if(it.startDate!=null){
+        startDateValue =  it.startDate.toString().replaceAll("\\[", "").replaceAll("\\]", "")
+    }else{
+        startDateValue =  today
+    }
+
+    // Get the release date  and if it is null set it to todays date as a release date is required when creating a version in a new project
+    if(it.releasedDate!=null){
+        releaseDateValue =  it.startDate.toString().replaceAll("\\[", "").replaceAll("\\]", "")
+    }else{
+        releaseDateValue = today
+    }
+
+    // Make the rest call to create the version
+    def createVersion = post("/rest/api/2/version")
+            .header("Content-Type", "application/json")
+            .body([
+            "description": it.description.toString().replaceAll("\\[", "").replaceAll("\\]", "") ?: "", // Get the desceription and pass an emtpy string if it is null
+            "name": it.name.toString().replaceAll("\\[", "").replaceAll("\\]", "") ?: "", // Get the name and pass an emtpy string if it is null
+            "archived": archivedValue, // Get the archived value and pass an emtpy string if it is null
+            "released": releasedValue, // Get the released  value and pass an emtpy string if it is null
+            "startDate": startDateValue,
+            "releaseDate": releaseDateValue,
+            "project": projectToCopyVersionTo
+    ]).asString()
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.