Snippets

Adaptavist Jira Cloud - Trim down comments on Issues that have more than a specified number of comments

Created by Kristian Walker last modified
/*
* 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 below
def projectKey = ""

// Specify the number of comments below
int maximumNumberOfComments = 0

// Validate that a projectKey and maximum number of comments has been specified
if (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 archived
def jqlQuery = "project = ${projectKey} and numberOfComments > ${maximumNumberOfComments}"

// Search for the issues we want to update
def searchReq = get("/rest/api/2/search")
        .queryString("jql", jqlQuery)
        .asObject(Map)

// Verify the search completed successfully
assert searchReq.status >= 200 && searchReq.status < 300

// Save the search results as a Map
Map searchResult = searchReq.body

// Loop over each issue
searchResult.issues.each { Map issue ->
// 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 comments to fetch
while (true) {
    // Get all comments off the issue
    def getComments = get("/rest/api/3/issue/${issue.key}/comment")
            .queryString("orderBy", "created")
            .queryString('startAt', startAt)
            .queryString('maxResults', pageSize)
            .asObject(Map)

// Validate the comments were fetched correctly
    assert getComments.status >= 200 && getComments.status < 300

    // Get the items returned
    def itemsOnPage = getComments.body.comments

    // If there are no more projects to fetch exit out of the loop
    if (!itemsOnPage) {
        break
    }

    // Add the items to the list
    allItems.addAll(itemsOnPage)

    // Increment the starting index for the next page
    startAt += pageSize
}

// Get all the Ids of the comments that exceed the maximum value to delete
    def commentsToDelete = allItems*.id.drop(maximumNumberOfComments)

// Loop over each comment and delete it
    commentsToDelete.each { id ->
        def deleteComment = delete("/rest/api/3/issue/${issue.key}/comment/${id}")
                .asObject(String)

        // Validate the comment was deleted correctly
        assert deleteComment.status >= 200 && deleteComment.status < 300
    }
    // Log out what issue comments were trimmed down
    logger.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)

HTTPS SSH

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