PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/sorin/jira-plugin-intellij
Groovy | 116 lines | 93 code | 20 blank | 3 comment | 5 complexity | d54a8d5c5157d25639ef9f7c3a1f3244 MD5 | raw file
  1. package com.onresolve.jira.groovy.canned.workflow.postfunctions
  2. import com.atlassian.jira.ComponentManager
  3. import com.atlassian.jira.issue.Issue
  4. import com.atlassian.jira.issue.MutableIssue
  5. import com.atlassian.jira.util.ErrorCollection
  6. import com.atlassian.jira.workflow.JiraWorkflow
  7. import com.onresolve.jira.groovy.canned.CannedScript
  8. import com.onresolve.jira.groovy.canned.utils.CannedScriptUtils
  9. import com.onresolve.jira.groovy.canned.utils.WorkflowUtils
  10. import com.opensymphony.workflow.loader.ActionDescriptor
  11. import com.opensymphony.workflow.loader.StepDescriptor
  12. import org.apache.log4j.Category
  13. import com.atlassian.crowd.embedded.api.User
  14. class ResolveParentAfterSubtasks implements CannedScript{
  15. ComponentManager componentManager = ComponentManager.getInstance()
  16. Category log = Category.getInstance(ResolveParentAfterSubtasks.class)
  17. def projectManager = componentManager.getProjectManager()
  18. public final static String FIELD_PARENTACTION = "FIELD_PARENTACTION"
  19. public final static String FIELD_RESOLUTION_ID = "FIELD_RESOLUTION_ID"
  20. String getName() {
  21. return "Transition parent when all subtasks are resolved"
  22. }
  23. public String getHelpUrl() {
  24. "https://studio.plugins.atlassian.com/wiki/display/GRV/Built-In+Scripts#Built-InScripts-Transitionparentwhenallsubtasksareresolved"
  25. }
  26. String getDescription() {
  27. return """This will do the given action on the parent when all sub-tasks are resolved<br>
  28. """
  29. }
  30. List getCategories() {
  31. ["Function"]
  32. }
  33. Integer getActionId(Issue issue, String actionName) {
  34. JiraWorkflow workflow = componentManager.getWorkflowManager().getWorkflow(issue)
  35. StepDescriptor step = workflow.getLinkedStep(issue.status)
  36. ActionDescriptor ad = step.getActions().find {it.name == actionName} as ActionDescriptor
  37. ad?.id
  38. }
  39. List getParameters(Map params) {
  40. [
  41. [
  42. Name:FIELD_PARENTACTION,
  43. Label:"Parent action",
  44. Description:"Choose the action to do on the parent when the sub-tasks are resolved",
  45. Type: "list",
  46. Values: CannedScriptUtils.getAllWorkflowActions(false),
  47. ],
  48. [
  49. Label:"Resolution",
  50. Name:FIELD_RESOLUTION_ID,
  51. Type: "list",
  52. Description:"Resolution to use on the parent",
  53. Values: CannedScriptUtils.getResolutionOptions(true),
  54. ],
  55. // todo: need to allow a sub-task resolution
  56. ]
  57. }
  58. public ErrorCollection doValidate(Map params, boolean forPreview) {
  59. // todo: check this is on a sub-task type
  60. null
  61. }
  62. Map doScript(Map params) {
  63. log.debug ("TestCondition.doScript with params: ${params}");
  64. String actionName = params[FIELD_PARENTACTION] as String
  65. MutableIssue subtask = params['issue'] as MutableIssue
  66. User user = WorkflowUtils.getUser(params)
  67. String resolutionId = params[FIELD_RESOLUTION_ID] as String
  68. log.debug ("actionName: $actionName")
  69. log.debug ("subtask: $subtask")
  70. log.debug ("subtask.isSubTask(): ${subtask.isSubTask()}")
  71. // if this action is resolve and all sub-tasks are resolved
  72. if (subtask.isSubTask()) {
  73. MutableIssue parent = subtask.getParentObject() as MutableIssue
  74. boolean allResolved = parent.getSubTaskObjects().every {Issue issue ->
  75. issue.resolution
  76. }
  77. if (allResolved) {
  78. log.debug ("Resolve parent")
  79. Integer actionId = actionName?.replaceAll(/ .*/, "") as Integer
  80. if (WorkflowUtils.hasAction(parent, actionId)) {
  81. WorkflowUtils.resolveIssue(parent, actionId, user, resolutionId, [:])
  82. }
  83. else {
  84. log.warn("Action name: $actionName not found for this step.")
  85. }
  86. }
  87. }
  88. return params
  89. }
  90. String getDescription(Map params, boolean forPreview) {
  91. getName()
  92. }
  93. public Boolean isFinalParamsPage(Map params) {
  94. true
  95. }
  96. }