Snippets

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

Created by Kristian Walker last modified
/*
* This example script console script shows how to trim down issue links issues that Exceed a maximum number of links . 
* 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." 
*/

// Specify the project key below
def projectKey = ""

// Specify the number of links below
int maximumNumberOfLinks = 0

// Validate that a projectKey and maximum number of links has been specified
if (projectKey.size() == 0 || maximumNumberOfLinks == 0) {
    return "You must specify a project Key and the maximum number of links to be able to archive the issues"
}

// Construct the JQL to return the issues to be archived
def jqlQuery = "project = ${projectKey} and numberOfLinks > ${maximumNumberOfLinks}"

// 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 ->
    // Get the ID for all issue links
    def getLinks = issue.fields.issuelinks

    // Define an array to store all issue link ids
    def allLinkIds = []

    // Add the Id of each issue link to the array
    getLinks.each { issueLink ->
        allLinkIds.push(issueLink.id)
    }

    // Extract the Id's of the links whcih exceed the maximum limit leaving the newest issue links
    def issueLinksToDelete = allLinkIds.drop(maximumNumberOfLinks)

    // Loop over each issue link and delete it
    issueLinksToDelete.each { id ->
        def deleteIssueLink = delete("/rest/api/3/issueLink/${id}")
                .asObject(String)

        // Validate the issue link was deleted correctly
        assert deleteIssueLink.status >= 200 && deleteIssueLink.status < 300

        // Log out what issue issue links were trimmed down
        logger.info("Issue links trimmed down to leave the ${maximumNumberOfLinks} newest issue links on the ${issue.key} issue.")
    }
}

"Issue links trimmed down to leave the ${maximumNumberOfLinks} newest issue links 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.