Snippets

Adaptavist Script Runner Jira Cloud - Post Function to Create an issue and link to it

Created by Kristian Walker last modified
/*
 * This script post function provides an example script for SR for Jira cloud of how to create an issue and link the created issue back to the original issue which triggered the workflow post function.
 * "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 key of the project to create the issue in.
def projectKey = '<ProjectKeyHere>'

// Get the issue type to use to create the issue with
def issueType = get('/rest/api/2/issuetype')
        .asObject(List)
        .body
        .find { it['name'] == '<IssueTypeHere>' }['id']

// Specify the field values to set on the newly created issue
def newSummary = 'An issue created by a post function'
def newDescription = 'This issue was created by a post function and was linked to the issue which created it'


// Create the issues with the specified issue type and set the specified field values.
def createIssue = post('/rest/api/2/issue')
        .header('Content-Type', 'application/json')
        .body(
        [
                fields: [
                        summary    : newSummary,
                        description: newDescription,
                        project    : [
                                key: projectKey
                        ],
                        issuetype  : [
                                id: issueType
                        ]
                ]
        ])
        .asObject(Map)


// Get the issue key of the newly created issue so that we are able to link it to the issue which it was created from.
def createdIssueKey = createIssue.body.key as String
logger.info("The ${createdIssueKey} issue was created")

// Link the issues together

//  Get the issue id for the current issue
def issueId = issue.id

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

// validate that the issue link created correctly
assert link.status == 201

Comments (0)

HTTPS SSH

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