Automate the Creation of an Issue in Jira

automateissue

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 as a standalone script or as part of a larger script to create some serious automation!

Example

I am a product manager, and I need to create several issues weekly for different departments. For example, I need marketing to provide me with a weekly analytics report for live product campaigns. Previously, I was having to spend time every week manually assigning these issues. However, with this script, I can schedule the issues to be created automatically every week.

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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.context.IssueContext import com.atlassian.jira.issue.context.IssueContextImpl import com.atlassian.jira.issue.fields.config.manager.PrioritySchemeManager // the project key under which the issue will get created final projectKey = 'TEST' // the issue type for the new issue final issueTypeName = 'Bug' // user with that user key will be the reporter of the issue final reporterKey = 'auser' // the summary of the new issue final summary = 'Groovy Friday' // the priority of the new issue final priorityName = 'Major' def issueService = ComponentAccessor.issueService def constantsManager = ComponentAccessor.constantsManager def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser def prioritySchemeManager = ComponentAccessor.getComponent(PrioritySchemeManager) 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 issueContext = new IssueContextImpl(project, issueType) as IssueContext def priorityId = constantsManager.priorities.findByName(priorityName)?.id ?: prioritySchemeManager.getDefaultOption(issueContext) def issueInputParameters = issueService.newIssueInputParameters().with { setProjectId(project.id) setIssueTypeId(issueType.id) setReporterId(reporter.name) setSummary(summary) setPriorityId(priorityId) } def validationResult = issueService.validateCreate(loggedInUser, issueInputParameters) assert validationResult.valid : validationResult.errorCollection def result = issueService.create(loggedInUser, validationResult) assert result.valid : result.errorCollection