[轉]Create a Sub-task and Link to Parent Issue in Jira
https://library.adaptavist.com/
Overview
This script creates a sub-task in Jira with the minimum fields required, then automatically links the sub-task to a specified parent issue.
Example
I want to split my team's workload into smaller chunks using sub-tasks; however, it is very time-consuming to do this manually. To save time, I can use this snippet to create sub-tasks of issues and automatically link these to the parent issue.
Good to Know
- If the specified reporter does not exist, the logger in user shall be assigned as reporter.
- If the specified priority does not exist, the default priority shall be assigned.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.config.manager.PrioritySchemeManager
// the issue key of the parent issue
final parentIssueKey = 'JRA-1'
// the issue type for the new issue - should be of type subtask
final issueTypeName = 'Sub-task'
// 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 parentIssue = ComponentAccessor.issueManager.getIssueByCurrentKey(parentIssueKey)
assert parentIssue : "Could not find parent issue with key $parentIssueKey"
def subtaskIssueTypes = constantsManager.allIssueTypeObjects.findAll { it.subTask }
def subTaskIssueType = subtaskIssueTypes.findByName(issueTypeName)
assert subTaskIssueType : "Could not find subtask issue type with name $issueTypeName. Avaliable subtask issue types are ${subtaskIssueTypes*.name.join(", ")}"
// 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 priorityId = constantsManager.priorities.findByName(priorityName)?.id ?: prioritySchemeManager.getDefaultOption(parentIssue)
def issueInputParameters = issueService.newIssueInputParameters().with {
setProjectId(parentIssue.projectObject.id)
setIssueTypeId(subTaskIssueType.id)
setReporterId(reporter.username)
setSummary(summary)
setPriorityId(priorityId)
}
def validationResult = issueService.validateSubTaskCreate(loggedInUser, parentIssue.id, issueInputParameters)
assert validationResult.valid : validationResult.errorCollection
def issueResult = issueService.create(loggedInUser, validationResult)
assert issueResult.valid : issueResult.errorCollection
def subtask = issueResult.issue
ComponentAccessor.subTaskManager.createSubTaskIssueLink(parentIssue, subtask, loggedInUser)