1. package com.onresolve.jira.groovy.canned.workflow.postfunctions
  2. import com.atlassian.jira.component.ComponentAccessor
  3. import com.atlassian.jira.config.ConstantsManager
  4. import com.atlassian.jira.config.SubTaskManager
  5. import com.atlassian.jira.event.issue.IssueEvent
  6. import com.atlassian.jira.issue.Issue
  7. import com.atlassian.jira.issue.MutableIssue
  8. import com.atlassian.jira.util.ErrorCollection
  9. import com.atlassian.jira.util.SimpleErrorCollection
  10. import com.atlassian.jira.workflow.JiraWorkflow
  11. import com.onresolve.jira.groovy.canned.CannedScript
  12. import com.onresolve.jira.groovy.canned.utils.CannedScriptUtils
  13. import com.onresolve.jira.groovy.canned.utils.ConditionUtils
  14. import com.onresolve.jira.groovy.canned.utils.WorkflowUtils
  15. import com.opensymphony.workflow.loader.ActionDescriptor
  16. import com.opensymphony.workflow.loader.StepDescriptor
  17. class CreateSubTask extends AbstractCloneIssue implements CannedScript{
  18. public static final String FIELD_SUBTASK_SUMMARY = 'FIELD_SUBTASK_SUMMARY'
  19. public static final String FIELD_SUBTASK_ACTION = 'FIELD_SUBTASK_ACTION'
  20. String getName() {
  21. return "Create a sub-task."
  22. }
  23. public String getHelpUrl() {
  24. "https://jamieechlin.atlassian.net/wiki/display/GRV/Built-In+Scripts#Built-InScripts-Createasubtask"
  25. }
  26. String getDescription() {
  27. return "Create a sub-task. Will optionally reopen a matching sub-task."
  28. }
  29. List getCategories() {
  30. ["Function", "Listener"]
  31. }
  32. List getParameters(Map params) {
  33. [
  34. ConditionUtils.getConditionParameter(),
  35. [
  36. Name:FIELD_TARGET_ISSUE_TYPE,
  37. Label:"Target Issue Type",
  38. Type: "list",
  39. Description:"""Target issue type. Leave blank for the same issue type as the source issue.
  40. <br>NOTE: This issue type must be valid for the target project""",
  41. Values: CannedScriptUtils.getAllSubTaskIssueTypes(true),
  42. ],
  43. [
  44. Name:FIELD_SUBTASK_SUMMARY,
  45. Label:"Subtask Summary",
  46. Description:"""Optionally set the summary. If blank it will be inherited from the parent.
  47. <br>This is useful in conjunction with automatic reopening of subtasks""",
  48. ],
  49. super.getOverridesParam(),
  50. [
  51. Name:FIELD_SUBTASK_ACTION,
  52. Label:"Subtask Action",
  53. Type: "list",
  54. Description:"""Optionally set an action that will be applied to a sub-task if a
  55. matching subtask already exists""",
  56. Values: CannedScriptUtils.getAllWorkflowActions(true),
  57. ],
  58. ]
  59. }
  60. public ErrorCollection doValidate(Map params, boolean forPreview) {
  61. SimpleErrorCollection errorCollection = new SimpleErrorCollection()
  62. if (!params[FIELD_TARGET_ISSUE_TYPE]) {
  63. errorCollection.addError(FIELD_TARGET_ISSUE_TYPE, "You must provide the target issue type.")
  64. }
  65. // todo: validation for issue type if set
  66. return errorCollection
  67. }
  68. Map doScript(Map params) {
  69. MutableIssue issue = params['issue'] as MutableIssue
  70. def user = WorkflowUtils.getUser(params)
  71. Boolean doIt = ConditionUtils.processCondition(params[ConditionUtils.FIELD_CONDITION] as String, issue, false, params)
  72. if (! doIt) {
  73. return [:]
  74. }
  75. def String subtaskAction = params[FIELD_SUBTASK_ACTION]
  76. subtaskAction = subtaskAction?.replaceAll(/ .*/, "")
  77. String subtaskSummary = params[FIELD_SUBTASK_SUMMARY]
  78. if (issue.getIssueTypeObject().isSubTask()) {
  79. log.warn ("This issue ($issue) is already a sub-task... doing nothing.")
  80. return [:]
  81. }
  82. // if we have a subtask action, and the subtask exists, and the action is applicable... do it
  83. Collection<MutableIssue> subTasks = issue.getSubTaskObjects()
  84. if (subtaskAction && subtaskSummary && subTasks*.summary.contains(subtaskSummary)) {
  85. subTasks.each {MutableIssue sub ->
  86. if (sub.summary == subtaskSummary) {
  87. if (WorkflowUtils.hasAction(sub, subtaskAction as Integer)) {
  88. try {
  89. // actionIssue(sub, subtaskAction as Integer, WorkflowUtils.getUser(params))
  90. WorkflowUtils.actionIssue(null, sub, subtaskAction as Integer, user, [:])
  91. }
  92. catch (Exception e) {
  93. log.error(e.message, e)
  94. }
  95. }
  96. else {
  97. log.debug("Action not applicable")
  98. }
  99. }
  100. }
  101. return [:]
  102. }
  103. params = super.doScript (params)
  104. Issue newIssue = params['newIssue'] as Issue
  105. def subTaskManager = componentManager.getSubTaskManager()
  106. // get a fresh copy of the issue, otherwise the old version gets indexed by the createSubTaskIssueLink method
  107. // issue = ComponentAccessor.getIssueManager().getIssueObject(issue.id)
  108. subTaskManager.createSubTaskIssueLink(issue, newIssue, user)
  109. params
  110. }
  111. String getDescription(Map params, boolean forPreview) {
  112. ConstantsManager constantsManager = componentManager.getConstantsManager()
  113. StringBuffer sb = new StringBuffer()
  114. sb << getName() + "<br>Subtask will be created with issue type: <b>" +
  115. (params[FIELD_TARGET_ISSUE_TYPE] ? constantsManager.getIssueTypeObject(params[FIELD_TARGET_ISSUE_TYPE] as String)?.name
  116. : "same as parent") + "</b>"
  117. sb.toString()
  118. }
  119. public Boolean isFinalParamsPage(Map params) {
  120. true
  121. }
  122. }