Snippets

Adaptavist Script Console Transitions Issues Returned from a JQL Search

Created by Kristian Walker last modified
/*
* This example script console script transitions issues returned from the JQL search which meet the specified conditions.
* "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."
*/


// Variable to store the issue type to be matched on
def issueType = "<IssueTypeNameHere>"

// Variable to store the status to match on
def status = "<StatusNameHere>"

// The ID of the workflow transition to execute
def transitionID = <TransitionIDHere>

def jqlQuery = "<AddJQLQueryHere>"

// Get all issues matching the specified JQL Query
def allmatchingIssues = get("/rest/api/2/search")
        .queryString("jql", jqlQuery)
        .asObject(Map)
        .body
        .issues as List<Map>

// Log out the number of issues returned by the JQL search
logger.info("The number of issues returned by the JQL search = : ${allmatchingIssues.size()}")

// Log out the map of issues returned by the JQL search.
logger.info("The returned issues are " + allmatchingIssues)

// Iterate over each issue returned
allmatchingIssues.each { it ->

    // Get the issue object for each issue returned within the JQL search
    def issue = get('/rest/api/2/issue/' + it.key)
            .header('Content-Type', 'application/json')
            .asObject(Map)

    // Check if the issue in the issue type is in the specified status
    if (issue.body.fields.status.name == "${status}") {
        // check if the issue is of the specified issue type
        if (issue.body.fields.issuetype.name == "${issueType}") {
            // If  all of  the conditions are met transition the issue
            def transition = post('/rest/api/2/issue/' + it.key + '/transitions')
                    .header("Content-Type", "application/json")
                    .body([transition: [id: transitionID]])
                    .asObject(Map)
        }
    }
}
return "Script execution finished"

Comments (1)

  1. Jason Pinkham Account Deactivated

    The search API call is what we were missing. I was going about that specific call the wrong way, but that just put me on the right track. Thank you!

HTTPS SSH

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