Snippets

Adaptavist Look up Reporter and Create Issue

Created by Kristian Walker last modified
/*
* This example script console script should be conifgured to run as the ScriptRunner Add-On User and shows how to search for a user and set them as the reporter when creating an issue.
* "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 projectKey = '<Project Key Here>'
def projectId = get("/rest/api/2/project/${projectKey}").asObject(Map).body.id

def taskType = get("/rest/api/2/issuetype/project?projectId=${projectId}").asObject(List).body.find { it['name'] == 'Task' }['id']

def userToFind ="<Name of User To Find Here>"

def userSearchRequest = get("/rest/api/3/user/search")
        .queryString("query", userToFind)
        .asObject(List)

// Assert that the API call to the  user search API returned a success response
assert userSearchRequest.status == 200

// Get the accountId for the user returned by the search
def userAccountId = userSearchRequest.body.accountId[0].toString()

if(userAccountId){

def createIssue = post('/rest/api/2/issue')
        .header('Content-Type', 'application/json')
        .body(
                [
                        fields: [
                                summary    : 'Issue Created By Scriptrunner',
                                reporter: [id: userAccountId],
                                project    : [
                                        key: projectKey
                                ],
                                issuetype  : [
                                        id: taskType
                                ]
                        ]
                ])
        .asString()

// Assert the issue was created correclty
assert createIssue.status >=200 && createIssue.status <=300

}

Comments (0)

HTTPS SSH

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