Snippets

Adaptavist Copy Labels To All Linked Issues

Created by Kristian Walker last modified
/*
* This example script  console script shows how you can get the labels off an issue and copy them over to all of its linked issues.
* "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 IssueKey
def issueKey = '<InsertIssueKeyHere>'

// Make a rest call to return the Issue
def issue = get('/rest/api/2/issue/' + issueKey)
        .header('Content-Type', 'application/json')
        .asObject(Map)
        .body
        
// Get the labels from the issue        
def labels= issue.fields.labels

// Find the linked issues
def linkedIssues = issue.fields.issuelinks

// Define a loop counter
def loopCounter = 0

// Loop over each linked issue
linkedIssues.each{

// Iterate the loop counter on each iteration of the loop
loopCounter ++

// Update each linked issue to set the labels using the key for each linked issue
def updateLinkedIssue = put('/rest/api/2/issue/' +  it.outwardIssue.key)
        .header('Content-Type', 'application/json')
        .body([
        fields:[
                labels: labels
        ]
])
        .asString()

// Print to the log tabs what iteration the loop  is on and the key of the issue that was updated        
println "Iteration ${loopCounter} of the loop set the labels on the ${it.outwardIssue.key} issue."
}

return "The Script has completed updating the labels succsfully on ${loopCounter} issues. \nPlease see the 'Logs' tab for more information on what issues were updated."

Comments (0)

HTTPS SSH

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