Snippets

Adaptavist Add a blocks link to the parent issue if any child issue has a blocks link

Created by Kristian Walker
/*
 * This script provides some example code that can be run on the script listeners page in Jira cloud and shows how to add a blcoks link to the issue when a child link has a blocks link.
 * This script should be configured to run on the Isseu Updated event. 
 * 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." 
*/

// The JQL query to find all child issues below the parent issue
def jqlQuery = "parent = ${issue.key}"

// Run the JQL search
def allChildIssues = get("/rest/api/2/search")
        .queryString('jql', jqlQuery)
        .header('Content-Type', 'application/json')
        .asObject(Map)
        .body
        .issues

// Loop over all child issues returned
allChildIssues.each { childIssue ->

    // Fetch the issue links for each child issue
    def issueLinks = childIssue.fields.issuelinks

    // If the child issue contains issue links
    if (issueLinks) {

        // Check if the issue has a link type of "blocks" or is blocked by another issue
        def hasBlocksLink = issueLinks.any { link ->
            link.type.outward == "blocks"
        }
        def hasIsBlockedByLink = issueLinks.any { link ->
            link.type.inward == "blocks" && link.inwardIssue.key == issue.key
        }

        if (hasBlocksLink || hasIsBlockedByLink) {
            // Create the issue link between both issues
            def link = post('/rest/api/2/issueLink')
                    .header('Content-Type', 'application/json')
                    .body([
                            type        : [name: "Blocks"],
                            outwardIssue: [key: issue.key],  // This is the issue that the link 'starts' at
                            inwardIssue : [key: childIssue.key]  // You'll need to specify an issue ID or key here
                    ])
                    .asString()
        }
    }
}

Comments (0)

HTTPS SSH

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