/** 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." */importjava.util.Date// Enter the number of days below that you want check that issues have not been updated inintnumberOfDays=10// An array to store the project keys to be archiveddefprojectKeysToArhcive=[];// Define the number of items to be returned in each page of the API calldefpageSize=50// Define where to start at in the API calldefstartAt=0// Define an Array for storing all items returneddefallItems=[]// Loop over the Pages to be returned while their are still more pages of projects to fetchwhile(true){// Get all the projectsdefgetProjects=get('/rest/api/3/project/search').header('Content-Type','application/json').queryString('expand','insight').queryString('startAt',startAt).queryString('maxResults',pageSize).asObject(Map)assertgetProjects.status>=200&&getProjects.status<300// Get the items returneddefitemsOnPage=getProjects.body.values// If there are no more projects to fetch exit out of the loopif(itemsOnPage.isEmpty()){break}// Add the items to the listallItems.addAll(itemsOnPage)// Increment the starting index for the next pagestartAt+=pageSize}// Loop over each projectallItems.each{project->// If the project contains 0 issues add its key to the Array of projects to be archivedif(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 archivedif(project.insight.lastIssueUpdateTime!=null){deftimestamp=Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSSZ",project.insight.lastIssueUpdateTime)defcurrentDate=newDate()defmillisecondsDifference=Math.abs(currentDate.time-timestamp.time)defdaysBetween=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 archivedprojectKeysToArhcive.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("archivedtheprojectwiththekeyof${key}")}return "Archivingcompleted.Checkthelogstabstoseewhatprojectswerearchivedandforanyerrors."
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.