PageRenderTime 77ms CodeModel.GetById 24ms RepoModel.GetById 7ms app.codeStats 0ms

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

https://bitbucket.org/sorin/jira-plugin-intellij
Groovy | 153 lines | 114 code | 24 blank | 15 comment | 5 complexity | 6779e8757738f3e622275aafdb07dbe9 MD5 | raw file
  1. package com.onresolve.jira.groovy.canned.workflow.postfunctions
  2. import com.atlassian.jira.ComponentManager
  3. import com.atlassian.jira.bc.project.component.ProjectComponentManager
  4. import com.atlassian.jira.event.issue.IssueEvent
  5. import com.atlassian.jira.issue.MutableIssue
  6. import com.atlassian.jira.util.ErrorCollection
  7. import com.atlassian.jira.util.SimpleErrorCollection
  8. import com.onresolve.jira.groovy.canned.CannedScript
  9. import com.onresolve.jira.groovy.canned.utils.CannedScriptUtils
  10. import com.onresolve.jira.groovy.canned.utils.ConditionUtils
  11. import com.onresolve.jira.groovy.canned.utils.WorkflowUtils
  12. import com.opensymphony.workflow.spi.WorkflowEntry
  13. import org.apache.log4j.Category
  14. import org.ofbiz.core.entity.DelegatorInterface
  15. import org.ofbiz.core.entity.GenericValue
  16. import com.atlassian.crowd.embedded.api.User
  17. import com.atlassian.jira.util.ImportUtils
  18. import com.atlassian.jira.issue.index.IssueIndexManager
  19. class FasttrackTransition implements CannedScript{
  20. def componentManager = ComponentManager.getInstance()
  21. def projectComponentManager = componentManager.getProjectComponentManager()
  22. Category log = Category.getInstance(FasttrackTransition.class)
  23. def indexManager = ComponentManager.getInstance().getIndexManager()
  24. public static final String FIELD_ACTION = "FIELD_ACTION"
  25. public static final String FIELD_ADDITIONAL_SCRIPT = "FIELD_ADDITIONAL_SCRIPT"
  26. String getName() {
  27. return "Fast-track transition an issue"
  28. }
  29. public String getHelpUrl() {
  30. "https://studio.plugins.atlassian.com/wiki/display/GRV/Built-In+Scripts#Built-InScripts-Fasttracktransitionanissue"
  31. }
  32. String getDescription() {
  33. """If the condition is met, automatically transition this issue to another status
  34. """
  35. }
  36. List getCategories() {
  37. ["Function", "Listener"]
  38. }
  39. List getParameters(Map params) {
  40. [
  41. ConditionUtils.getConditionParameter(),
  42. [
  43. Name:FIELD_ACTION,
  44. Label:"Action",
  45. Type: "list",
  46. Values: CannedScriptUtils.getAllWorkflowActions(false),
  47. Description: """ID of the workflow action that will be applied to this issue."""
  48. ],
  49. [
  50. Name:FIELD_ADDITIONAL_SCRIPT,
  51. Label:"Additional code",
  52. Type:"text",
  53. Description: """Additional script to run before the issue is stored. Use this to modify fields
  54. or add a comment etc.""",
  55. Examples: [
  56. "Set comment (4.1 and above)": "issueInputParameters.setComment(\\'Some comment\\')",
  57. "Set assignee (4.1 and above)" : "issueInputParameters.setAssigneeId(\\'someuser\\')",
  58. "Set comment (4.0 and below)": "workflowTransitionUtil.setParams([\\'comment\\': \\'Some comment\\'])",
  59. "Set assignee (4.0 and below)" : "issue.setAssigneeId(\\'someuser\\')",
  60. ],
  61. ],
  62. ]
  63. }
  64. public ErrorCollection doValidate(Map params, boolean forPreview) {
  65. // todo: check that the transition is valid in this workflow
  66. SimpleErrorCollection errorCollection = new SimpleErrorCollection()
  67. if (! params[FIELD_ACTION]) {
  68. errorCollection.addError(FIELD_ACTION, "You must provide an action.")
  69. }
  70. errorCollection
  71. }
  72. Map doScript(Map params) {
  73. log.debug("params $params")
  74. def String actionId = params[FIELD_ACTION]
  75. actionId = actionId?.replaceAll(/ .*/, "")
  76. MutableIssue issue = params['issue'] as MutableIssue
  77. Boolean doIt = ConditionUtils.processCondition(params[ConditionUtils.FIELD_CONDITION] as String, issue, false, params)
  78. Thread executorThread = new Thread(new Runnable() {
  79. void run() {
  80. Thread.sleep(100)
  81. doTransition(issue, params, actionId)
  82. }
  83. })
  84. if (doIt) {
  85. // possibly only need to do this on buggy versions of jira
  86. executorThread.start()
  87. // doTransition(issue, params, actionId)
  88. }
  89. else {
  90. log.debug "Condition wasn't true"
  91. }
  92. [:]
  93. }
  94. private doTransition(MutableIssue issue, Map params, String actionId) {
  95. /*
  96. This is hack to get around transitioning an issue in the Create transition
  97. The workflow is not marked as active until after all the post-functions have run,
  98. so we cannot transition it until then.
  99. Without setting it to active manually then this code exits without errors:
  100. com.opensymphony.workflow.AbstractWorkflow.doAction
  101. Don't think it will do any harm... but I could be wrong
  102. */
  103. DelegatorInterface gd = (DelegatorInterface) componentManager.getComponentInstanceOfType(DelegatorInterface.class)
  104. GenericValue gv = gd.findByPrimaryKey("OSWorkflowEntry", ["id": issue.getWorkflowId()])
  105. if (gv.get("state") == WorkflowEntry.CREATED) {
  106. gv.set("state", WorkflowEntry.ACTIVATED)
  107. gv.store()
  108. }
  109. // reload issue
  110. issue = ComponentManager.getInstance().getIssueManager().getIssueObject(issue.id)
  111. // when running in a different thread we lose the authcontext - causes weird and wonderful errors,
  112. // hence we set it explicitly
  113. User user = WorkflowUtils.getUser(params)
  114. ComponentManager.getInstance().getJiraAuthenticationContext().setLoggedInUser(user)
  115. WorkflowUtils.actionIssue(params[FIELD_ADDITIONAL_SCRIPT] as String, issue, actionId as Integer, user, params)
  116. boolean wasIndexing = ImportUtils.isIndexIssues();
  117. // reload issue
  118. issue = ComponentManager.getInstance().getIssueManager().getIssueObject(issue.id)
  119. indexManager.reIndex(issue);
  120. ImportUtils.setIndexIssues(wasIndexing);
  121. }
  122. String getDescription(Map params, boolean forPreview) {
  123. getName() + """ : If the condition is met the action ${params[FIELD_ACTION]} will be applied.
  124. <br>Note that this should go after the <b>Fire Event</b> function."""
  125. }
  126. public Boolean isFinalParamsPage(Map params) {
  127. true
  128. }
  129. }