/** This example script console script shows how to trim down comments on issues who have more than a maximum number of comments . * "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 project key belowdefprojectKey=""// Specify the number of comments belowintmaximumNumberOfComments=0// Validate that a projectKey and maximum number of comments has been specifiedif(projectKey.size()==0||maximumNumberOfComments==0){return"You must specify a project Key and the maximum number of comments to be able to archive the issues"}// Construct the JQL to return the issues to be archiveddefjqlQuery="project = ${projectKey} and numberOfComments > ${maximumNumberOfComments}"// Search for the issues we want to updatedefsearchReq=get("/rest/api/2/search").queryString("jql",jqlQuery).asObject(Map)// Verify the search completed successfullyassertsearchReq.status>=200&&searchReq.status<300// Save the search results as a MapMapsearchResult=searchReq.body// Loop over each issuesearchResult.issues.each{Mapissue->// 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 comments to fetchwhile(true){// Get all comments off the issuedefgetComments=get("/rest/api/3/issue/${issue.key}/comment").queryString("orderBy","created").queryString('startAt',startAt).queryString('maxResults',pageSize).asObject(Map)// Validate the comments were fetched correctlyassertgetComments.status>=200&&getComments.status<300// Get the items returneddefitemsOnPage=getComments.body.comments// If there are no more projects to fetch exit out of the loopif(!itemsOnPage){break}// Add the items to the listallItems.addAll(itemsOnPage)// Increment the starting index for the next pagestartAt+=pageSize}// Get all the Ids of the comments that exceed the maximum value to deletedefcommentsToDelete=allItems*.id.drop(maximumNumberOfComments)// Loop over each comment and delete itcommentsToDelete.each{id->defdeleteComment=delete("/rest/api/3/issue/${issue.key}/comment/${id}").asObject(String)// Validate the comment was deleted correctlyassertdeleteComment.status>=200&&deleteComment.status<300}// Log out what issue comments were trimmed downlogger.info("Comments trimmed down to leave the ${maximumNumberOfComments} newest comments on the ${issue.key} issue.")}"Comments trimmed down to leave the ${maximumNumberOfComments} newest comments on the isues which exceed this limit in the ${projectKey} project"
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.