Snippets

Adaptavist Jira Cloud - Clone Issue Fields and Link Issue Script Console

Created by Kristian Walker last modified
/*
* This example script console script shows how to clone fields from one issue to another and to link the two issues together as a clone. 
* "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." 
*/

def issueKey = 'IssueKeyHere'

def sourceIssue = get('/rest/api/2/issue/' + issueKey)
        .header('Content-Type', 'application/json')
        .asObject(Map)

// validate that the source issue was returned correctly
assert sourceIssue.status >= 200 && sourceIssue.status < 300

def sourceIssueFields = sourceIssue.body.fields

def clonedIssue = post('/rest/api/2/issue')
        .header('Content-Type', 'application/json')
        .body(
                [
                        // Add any extra fields to clone below
                        fields: [
                                summary    : sourceIssueFields.summary,
                                description: sourceIssueFields.description,
                                project    : [
                                        key: sourceIssueFields.project.key
                                ],
                                issuetype  : [
                                        id: sourceIssueFields.issuetype.id
                                ]
                        ]
                ])
        .asObject(Map)

// validate that the clone issue was created correctly
assert clonedIssue.status >= 200 && clonedIssue.status < 300

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

// validate that the issue link created correctly
assert link.status >= 200 && link.status < 300

return "The ${sourceIssue.body.key} source issue was cloned to the ${clonedIssue.body.key} issue and they have been linked as a clone."

Comments (0)

HTTPS SSH

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