- package com.onresolve.jira.groovy.canned.workflow.postfunctions
- import com.atlassian.jira.component.ComponentAccessor
- import com.atlassian.jira.config.ConstantsManager
- import com.atlassian.jira.config.SubTaskManager
- import com.atlassian.jira.event.issue.IssueEvent
- import com.atlassian.jira.issue.Issue
- import com.atlassian.jira.issue.MutableIssue
- import com.atlassian.jira.util.ErrorCollection
- import com.atlassian.jira.util.SimpleErrorCollection
- import com.atlassian.jira.workflow.JiraWorkflow
- import com.onresolve.jira.groovy.canned.CannedScript
- import com.onresolve.jira.groovy.canned.utils.CannedScriptUtils
- import com.onresolve.jira.groovy.canned.utils.ConditionUtils
- import com.onresolve.jira.groovy.canned.utils.WorkflowUtils
- import com.opensymphony.workflow.loader.ActionDescriptor
- import com.opensymphony.workflow.loader.StepDescriptor
- class CreateSubTask extends AbstractCloneIssue implements CannedScript{
- public static final String FIELD_SUBTASK_SUMMARY = 'FIELD_SUBTASK_SUMMARY'
- public static final String FIELD_SUBTASK_ACTION = 'FIELD_SUBTASK_ACTION'
- String getName() {
- return "Create a sub-task."
- }
- public String getHelpUrl() {
- "https://jamieechlin.atlassian.net/wiki/display/GRV/Built-In+Scripts#Built-InScripts-Createasubtask"
- }
- String getDescription() {
- return "Create a sub-task. Will optionally reopen a matching sub-task."
- }
- List getCategories() {
- ["Function", "Listener"]
- }
- List getParameters(Map params) {
- [
- ConditionUtils.getConditionParameter(),
- [
- Name:FIELD_TARGET_ISSUE_TYPE,
- Label:"Target Issue Type",
- Type: "list",
- Description:"""Target issue type. Leave blank for the same issue type as the source issue.
- <br>NOTE: This issue type must be valid for the target project""",
- Values: CannedScriptUtils.getAllSubTaskIssueTypes(true),
- ],
- [
- Name:FIELD_SUBTASK_SUMMARY,
- Label:"Subtask Summary",
- Description:"""Optionally set the summary. If blank it will be inherited from the parent.
- <br>This is useful in conjunction with automatic reopening of subtasks""",
- ],
- super.getOverridesParam(),
- [
- Name:FIELD_SUBTASK_ACTION,
- Label:"Subtask Action",
- Type: "list",
- Description:"""Optionally set an action that will be applied to a sub-task if a
- matching subtask already exists""",
- Values: CannedScriptUtils.getAllWorkflowActions(true),
- ],
- ]
- }
- public ErrorCollection doValidate(Map params, boolean forPreview) {
- SimpleErrorCollection errorCollection = new SimpleErrorCollection()
- if (!params[FIELD_TARGET_ISSUE_TYPE]) {
- errorCollection.addError(FIELD_TARGET_ISSUE_TYPE, "You must provide the target issue type.")
- }
- // todo: validation for issue type if set
- return errorCollection
- }
- Map doScript(Map params) {
- MutableIssue issue = params['issue'] as MutableIssue
- def user = WorkflowUtils.getUser(params)
- Boolean doIt = ConditionUtils.processCondition(params[ConditionUtils.FIELD_CONDITION] as String, issue, false, params)
- if (! doIt) {
- return [:]
- }
- def String subtaskAction = params[FIELD_SUBTASK_ACTION]
- subtaskAction = subtaskAction?.replaceAll(/ .*/, "")
- String subtaskSummary = params[FIELD_SUBTASK_SUMMARY]
- if (issue.getIssueTypeObject().isSubTask()) {
- log.warn ("This issue ($issue) is already a sub-task... doing nothing.")
- return [:]
- }
- // if we have a subtask action, and the subtask exists, and the action is applicable... do it
- Collection<MutableIssue> subTasks = issue.getSubTaskObjects()
- if (subtaskAction && subtaskSummary && subTasks*.summary.contains(subtaskSummary)) {
- subTasks.each {MutableIssue sub ->
- if (sub.summary == subtaskSummary) {
- if (WorkflowUtils.hasAction(sub, subtaskAction as Integer)) {
- try {
- // actionIssue(sub, subtaskAction as Integer, WorkflowUtils.getUser(params))
- WorkflowUtils.actionIssue(null, sub, subtaskAction as Integer, user, [:])
- }
- catch (Exception e) {
- log.error(e.message, e)
- }
- }
- else {
- log.debug("Action not applicable")
- }
- }
- }
- return [:]
- }
- params = super.doScript (params)
- Issue newIssue = params['newIssue'] as Issue
- def subTaskManager = componentManager.getSubTaskManager()
- // get a fresh copy of the issue, otherwise the old version gets indexed by the createSubTaskIssueLink method
- // issue = ComponentAccessor.getIssueManager().getIssueObject(issue.id)
- subTaskManager.createSubTaskIssueLink(issue, newIssue, user)
- params
- }
- String getDescription(Map params, boolean forPreview) {
- ConstantsManager constantsManager = componentManager.getConstantsManager()
- StringBuffer sb = new StringBuffer()
- sb << getName() + "<br>Subtask will be created with issue type: <b>" +
- (params[FIELD_TARGET_ISSUE_TYPE] ? constantsManager.getIssueTypeObject(params[FIELD_TARGET_ISSUE_TYPE] as String)?.name
- : "same as parent") + "</b>"
- sb.toString()
- }
- public Boolean isFinalParamsPage(Map params) {
- true
- }
- }