PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/resources/com/onresolve/jira/groovy/canned/workflow/postfunctions/CreateSubTask.groovy

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