Snippets

Adaptavist Jira Cloud - Script Console - Create Epic Issue with Linked Story

Created by Kristian Walker last modified
/*
* This example script console script shows how to create an Epic Issue and then create a story issue inside it. 
* "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." 
*/

// NOTE - The Epic Link field must be pressent on the create screen in the Project where the Story issues are being created. 

// Specify the Project Key for the project to create the issues in below
def projectKey = ''
def projectId = get("/rest/api/2/project/${projectKey}").asObject(Map).body.id

// Get the Ids for the Epic and Story Issue types
def epicType = get("/rest/api/2/issuetype/project?projectId=${projectId}").asObject(List).body.find { it['name'] == 'Epic' }['id']
def storyType = get("/rest/api/2/issuetype/project?projectId=${projectId}").asObject(List).body.find { it['name'] == 'Story' }['id']

// Get  all custom fields
def customFields = get("/rest/api/3/field")
        .asObject(List)
        .body
        .findAll { (it as Map).custom } as List<Map>

// Get the ID for your the Epic Link field
def epicLinkField = customFields.find { it.name == 'Epic Link' }?.id 

// Create the Epic issue
def createEpic = post('/rest/api/2/issue')
        .header('Content-Type', 'application/json')
        .body(
                [
                        fields: [
                                summary    : 'Sample Epic',
                                project    : [
                                        key: projectKey
                                ],
                                issuetype  : [
                                        id: epicType
                                ]
                        ]
                ])
        .asObject(Map)

// Validate that the Epic was created correctly
assert createEpic.status >200 && createEpic.status < 300 

// Get the issue key for the newly created Epic. 
def createdEpicKey =  createEpic.body.key;

// Create the story and link it to the created Epic
def createStory = post('/rest/api/2/issue')
        .header('Content-Type', 'application/json')
        .body(
                [
                        fields: [
                                summary    : 'Sample Story',
                                (epicLinkField): createdEpicKey,
                                project    : [
                                        key: projectKey
                                ],
                                issuetype  : [
                                        id: storyType
                                ]
                        ]
                ])
        .asObject(Map)

// Validate that the Story was created correctly
assert createStory.status >200 && createStory.status < 300 

// Return details of the Key of the Epic and Story issue that was created
return "An Epic was created with the key of: ${createdEpicKey} and has the story with the key of: ${createStory.body.key} created inside it."

Comments (0)

HTTPS SSH

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