Automate the Creation of an Issue in Jira

Overview

This script saves you the time and effort of creating issues manually. You can add this script to anywhere in Jira that allows you to add a custom script e.g. listeners, workflow functions, REST Endpoints. You can either add this script as a standalone script, or as part of a larger script to create some serious automations!

Example

I am a Product Manager and I need to create a number of issues weekly for different departments. For example, I need marketing to provide me with their weekly analytics report for the product campaigns we have running. Previously, I was having to spend time every week manually assigning these issues. However, this script means that the issues are scheduled automatically to be created weekly.

Good to Know

  • If the reporter user does not exist, it's assigned to the logged in user.
  • Issue type defined must exist in the project.
  • (Server) If priority defined does not exist, it takes the project default priority.
  • (Cloud) If priority defined does not exist, it takes the first priority that the 'priority' endpoint returns.

Requirements

Jira Jira (7.3 - 7.13)

import com.atlassian.jira.component.ComponentAccessor

// the project key under which the issue will get created
final String projectKey = "TEST"

// the issue type for the new issue
final String issueTypeName = "Bug"

// user with that user key will be the reporter of the issue
final String reporterKey = "auser"

// the summary of the new issue
final String summary = "Groovy Friday"

// the priority of the new issue
final String priorityName = "Major"

def issueService = ComponentAccessor.issueService
def constantsManager = ComponentAccessor.constantsManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def project = ComponentAccessor.projectManager.getProjectObjByKey(projectKey)
assert project : "Could not find project with key $projectKey"

def issueType = constantsManager.allIssueTypeObjects.findByName(issueTypeName)
assert issueType : "Could not find issue type with name $issueTypeName"

// if we cannot find user with the specified key or this is null, then set as a  reporter the logged in user
def reporter = ComponentAccessor.userManager.getUserByKey(reporterKey) ?: loggedInUser

// if we cannot find the priority with the given name or if this is null, then set the default priority
def priority = constantsManager.priorities.findByName(priorityName) ?: constantsManager.defaultPriority

def issueInputParameters = issueService.newIssueInputParameters().with {
    setProjectId(project.id)
    setIssueTypeId(issueType.id)
    setReporterId(reporter.key)
    setSummary(summary)
    setPriorityId(priority.id)
}

def validationResult = issueService.validateCreate(loggedInUser, issueInputParameters)
assert validationResult.isValid() : validationResult.errorCollection

def result = issueService.create(loggedInUser, validationResult)
assert result.isValid() : result.errorCollection
Discovered an issue? Report it here

Suggested for you

Appears in these Collections