Snippets

Adaptavist Jira Cloud - Script Console - Archive Unused Projects

Updated by Kristian Walker

File ArchiveUnusedProjects.groovy Modified

  • Ignore whitespace
  • Hide word diff
         def currentDate = new Date()
 
         def millisecondsDifference = Math.abs(currentDate.time - timestamp.time)
-        def daysBetween = millisecondsDifference / (1000 * 60 * 60 * 24);
+        def daysBetween = millisecondsDifference / (1000 * 60 * 60 * 24)
 
         def roundedDaysBetween = Math.round(daysBetween)
 
Updated by Kristian Walker

File ArchiveUnusedProjects.groovy Modified

  • Ignore whitespace
  • Hide word diff
         def currentDate = new Date()
 
         def millisecondsDifference = Math.abs(currentDate.time - timestamp.time)
-        def daysBetween = millisecondsDifference / (1000 * 60 * 60 * 24)
+        def daysBetween = millisecondsDifference / (1000 * 60 * 60 * 24);
 
         def roundedDaysBetween = Math.round(daysBetween)
 
Updated by Kristian Walker

File ArchiveUnusedProjects.groovy Modified

  • Ignore whitespace
  • Hide word diff
 
 import java.util.Date
 
-// Enter the number of days below
+// Enter the number of days below that you want check that issues have not been updated in
 int numberOfDays = 10
 
+// An array to store the project keys to be archived
 def projectKeysToArhcive = [];
 
 // Define the number of items to be returned in each page of the API call
Created by Kristian Walker

File ArchiveUnusedProjects.groovy Added

  • Ignore whitespace
  • Hide word diff
+/*
+* This example script console script shows how to fetch all projects and archive any projects which have 0 issues or projects where the issues have not been updated in X days.
+* Note: you must be on a Premium or Enterpise plan to be able to use this archiving API
+* "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." 
+*/
+
+import java.util.Date
+
+// Enter the number of days below
+int numberOfDays = 10
+
+def projectKeysToArhcive = [];
+
+// Define the number of items to be returned in each page of the API call
+def pageSize = 50
+
+// Define where to start at in the API call
+def startAt = 0
+
+// Define an Array for storing all items returned
+def allItems = []
+
+// Loop over the Pages to be returned while their are still more pages of projects to fetch
+while (true) {
+
+    // Get all the projects
+    def getProjects = get('/rest/api/3/project/search')
+            .header('Content-Type', 'application/json')
+            .queryString('expand', 'insight')
+            .queryString('startAt', startAt)
+            .queryString('maxResults', pageSize)
+            .asObject(Map)
+
+    assert getProjects.status >= 200 && getProjects.status < 300
+
+    // Get the items returned
+    def itemsOnPage = getProjects.body.values
+
+    // If there are no more projects to fetch exit out of the loop
+    if (itemsOnPage.isEmpty()) {
+        break
+    }
+
+    // Add the items to the list
+    allItems.addAll(itemsOnPage)
+
+    // Increment the starting index for the next page
+    startAt += pageSize
+}
+
+// Loop over each project
+allItems.each { project ->
+
+    // If the project contains 0 issues add its key to the Array of projects to be archived
+    if (project.insight.totalIssueCount == 0) {
+        projectKeysToArhcive.push(project.key)
+    }
+
+    // If the project has not had any issues in it updated in the specified timeframe then add its key to the Array of projects to be archived
+    if (project.insight.lastIssueUpdateTime != null) {
+
+        def timestamp = Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSSZ", project.insight.lastIssueUpdateTime)
+        def currentDate = new Date()
+
+        def millisecondsDifference = Math.abs(currentDate.time - timestamp.time)
+        def daysBetween = millisecondsDifference / (1000 * 60 * 60 * 24)
+
+        def roundedDaysBetween = Math.round(daysBetween)
+
+        if (roundedDaysBetween >= numberOfDays)
+            projectKeysToArhcive.push(project.key)
+    }
+
+}
+
+// Loop over each key in the array of projects to be archived
+projectKeysToArhcive.each { key ->
+
+    // Archive the project
+    // Note you must be on a Premium or Enterpise plan to be able to use this API
+    def archiveIssues = post("/rest/api/3/project/${key}/archive")
+            .header("Content-Type", "application/json")
+            .asObject(Map);
+    
+   
+
+    logger.info("archived the project with the key of ${key}")
+}
+
+return "Archiving completed. Check the logs tabs to see what projects were archived and for any errors."
HTTPS SSH

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