/src/main/resources/com/onresolve/jira/groovy/canned/workflow/postfunctions/FasttrackTransition.groovy
Groovy | 153 lines | 114 code | 24 blank | 15 comment | 5 complexity | 6779e8757738f3e622275aafdb07dbe9 MD5 | raw file
- package com.onresolve.jira.groovy.canned.workflow.postfunctions
-
- import com.atlassian.jira.ComponentManager
- import com.atlassian.jira.bc.project.component.ProjectComponentManager
- import com.atlassian.jira.event.issue.IssueEvent
- import com.atlassian.jira.issue.MutableIssue
- import com.atlassian.jira.util.ErrorCollection
- import com.atlassian.jira.util.SimpleErrorCollection
- 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.spi.WorkflowEntry
- import org.apache.log4j.Category
- import org.ofbiz.core.entity.DelegatorInterface
- import org.ofbiz.core.entity.GenericValue
- import com.atlassian.crowd.embedded.api.User
- import com.atlassian.jira.util.ImportUtils
- import com.atlassian.jira.issue.index.IssueIndexManager
-
- class FasttrackTransition implements CannedScript{
-
- def componentManager = ComponentManager.getInstance()
- def projectComponentManager = componentManager.getProjectComponentManager()
- Category log = Category.getInstance(FasttrackTransition.class)
- def indexManager = ComponentManager.getInstance().getIndexManager()
-
- public static final String FIELD_ACTION = "FIELD_ACTION"
- public static final String FIELD_ADDITIONAL_SCRIPT = "FIELD_ADDITIONAL_SCRIPT"
-
- String getName() {
- return "Fast-track transition an issue"
- }
-
- public String getHelpUrl() {
- "https://studio.plugins.atlassian.com/wiki/display/GRV/Built-In+Scripts#Built-InScripts-Fasttracktransitionanissue"
- }
-
- String getDescription() {
- """If the condition is met, automatically transition this issue to another status
- """
- }
-
- List getCategories() {
- ["Function", "Listener"]
- }
-
- List getParameters(Map params) {
- [
- ConditionUtils.getConditionParameter(),
- [
- Name:FIELD_ACTION,
- Label:"Action",
- Type: "list",
- Values: CannedScriptUtils.getAllWorkflowActions(false),
- Description: """ID of the workflow action that will be applied to this issue."""
- ],
- [
- Name:FIELD_ADDITIONAL_SCRIPT,
- Label:"Additional code",
- Type:"text",
- Description: """Additional script to run before the issue is stored. Use this to modify fields
- or add a comment etc.""",
- Examples: [
- "Set comment (4.1 and above)": "issueInputParameters.setComment(\\'Some comment\\')",
- "Set assignee (4.1 and above)" : "issueInputParameters.setAssigneeId(\\'someuser\\')",
- "Set comment (4.0 and below)": "workflowTransitionUtil.setParams([\\'comment\\': \\'Some comment\\'])",
- "Set assignee (4.0 and below)" : "issue.setAssigneeId(\\'someuser\\')",
- ],
- ],
- ]
- }
-
- public ErrorCollection doValidate(Map params, boolean forPreview) {
- // todo: check that the transition is valid in this workflow
- SimpleErrorCollection errorCollection = new SimpleErrorCollection()
- if (! params[FIELD_ACTION]) {
- errorCollection.addError(FIELD_ACTION, "You must provide an action.")
- }
- errorCollection
- }
-
- Map doScript(Map params) {
- log.debug("params $params")
-
- def String actionId = params[FIELD_ACTION]
- actionId = actionId?.replaceAll(/ .*/, "")
-
- MutableIssue issue = params['issue'] as MutableIssue
-
- Boolean doIt = ConditionUtils.processCondition(params[ConditionUtils.FIELD_CONDITION] as String, issue, false, params)
-
- Thread executorThread = new Thread(new Runnable() {
- void run() {
- Thread.sleep(100)
- doTransition(issue, params, actionId)
- }
- })
-
- if (doIt) {
- // possibly only need to do this on buggy versions of jira
- executorThread.start()
- // doTransition(issue, params, actionId)
- }
- else {
- log.debug "Condition wasn't true"
- }
-
- [:]
- }
-
- private doTransition(MutableIssue issue, Map params, String actionId) {
- /*
- This is hack to get around transitioning an issue in the Create transition
- The workflow is not marked as active until after all the post-functions have run,
- so we cannot transition it until then.
- Without setting it to active manually then this code exits without errors:
- com.opensymphony.workflow.AbstractWorkflow.doAction
- Don't think it will do any harm... but I could be wrong
- */
- DelegatorInterface gd = (DelegatorInterface) componentManager.getComponentInstanceOfType(DelegatorInterface.class)
- GenericValue gv = gd.findByPrimaryKey("OSWorkflowEntry", ["id": issue.getWorkflowId()])
- if (gv.get("state") == WorkflowEntry.CREATED) {
- gv.set("state", WorkflowEntry.ACTIVATED)
- gv.store()
- }
-
- // reload issue
- issue = ComponentManager.getInstance().getIssueManager().getIssueObject(issue.id)
-
- // when running in a different thread we lose the authcontext - causes weird and wonderful errors,
- // hence we set it explicitly
- User user = WorkflowUtils.getUser(params)
- ComponentManager.getInstance().getJiraAuthenticationContext().setLoggedInUser(user)
-
- WorkflowUtils.actionIssue(params[FIELD_ADDITIONAL_SCRIPT] as String, issue, actionId as Integer, user, params)
-
- boolean wasIndexing = ImportUtils.isIndexIssues();
- // reload issue
- issue = ComponentManager.getInstance().getIssueManager().getIssueObject(issue.id)
- indexManager.reIndex(issue);
- ImportUtils.setIndexIssues(wasIndexing);
- }
-
- String getDescription(Map params, boolean forPreview) {
- getName() + """ : If the condition is met the action ${params[FIELD_ACTION]} will be applied.
- <br>Note that this should go after the <b>Fire Event</b> function."""
- }
-
- public Boolean isFinalParamsPage(Map params) {
- true
- }
- }