Create a sub-task when parent issue has a specific label.

Place this script in a Custom Listener, to automatically create a sub-task when a specific label gets added in the parent issue.

Requirements

Jira Jira (7.3 - 7.13)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParametersImpl

final String newLabel = "create_subtask"

def issue = event.issue
if (!issue.subTask && newLabel in issue.labels*.label) {
    createSubtask(issue)
}

def createSubtask(Issue parentIssue) {
    def subTaskManager = ComponentAccessor.subTaskManager
    def asUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
    def constantsManager = ComponentAccessor.constantsManager
    def issueService = ComponentAccessor.issueService

    def subtaskIssueType = constantsManager.allIssueTypeObjects.findByName("Sub-task")

    assert subtaskIssueType?.subTask

    // Fill the required fields
    def issueInputParameters = new IssueInputParametersImpl()
    issueInputParameters
        .setProjectId(parentIssue.projectId)
        .setIssueTypeId(subtaskIssueType.id)
        .setSummary('A new subtask')
        .setDescription('A description')
        .setReporterId(asUser.key)

    def createValidationResult = ComponentAccessor.issueService.validateSubTaskCreate(asUser, parentIssue.id, issueInputParameters)
    if (!createValidationResult.valid) {
        log.error createValidationResult.errorCollection
        return
    }

    def newIssue = issueService.create(asUser, createValidationResult).issue
    subTaskManager.createSubTaskIssueLink(parentIssue, newIssue, asUser)
}

Discovered an issue? Report it here

Suggested for you