Obtaining available workflow actions programmatically

Often in our programs, we may come across the need to retrieve the current workflow actions available on the issue. Let us have a look at how to do this using the JIRA API.

How to do it...

Follow these steps:

  1. Retrieve the JIRA workflow object associated with the issue:
            JiraWorkflow workFlow = 
            ComponentAccessor.getWorkflowManager().getWorkflow(issue);

    Here, the issue is the current issue, which is an instance of the com.atlassian.jira.issue.Issue class.

  2. Get the issue status and use it to retrieve the current workflow step linked to the issue:
            Status status = issue.getStatusObject();
            com.opensymphony.workflow.loader.StepDescriptor currentStep = 
             workFlow.getLinkedStep(status);
  3. Retrieve the set of available actions from the current step:
            List<ActionDescriptor> actions = currentStep.getActions();

    Here, actions is a list of com.opensymphony.workflow.loader.ActionDescriptor.

  4. Iterate on the ActionDescriptors and get the details for each action, depending on the requirement! The name of the available action can be printed as follows:
            for (ActionDescriptor action : actions) { 
                System.out.println("Action: "+action.getName())
            }

How it works...

WorkflowManager is used to perform a lot of operations related to workflows, such as creating/updating/deleting a workflow, copying it, creating a draft, and so on. Here, we used it to retrieve the workflow object, based on the issue selected. Please check the API (http://docs.atlassian.com/jira/latest/com/atlassian/jira/workflow/WorkflowManager.html) for a full list of available operations using WorkflowManager.

Once we retrieve the JIRA workflow, we get the current step using the status. As you have seen before in this chapter, a workflow status is linked to one, and only one workflow step. Once we get the step, we can get a load of information from it, including the available actions from that step.

Jolly good?

There's more...

Even though the recipe shows how to retrieve the action name, it is the ID that is used while programmatically progressing an issue through its workflow.

Getting the action ID's given name

Once the action name is available, you can easily retrieve the action ID by iterating on the list of actions, as shown in the following lines of code:

private int getActionIdForTransition(List<ActionDescriptor> actions, String actionName) { 
  for (ActionDescriptor action : actions) { 
    if (action.getName().equals(actionName)) { 
      return action.getId(); 
    } 
  } 
  return -1; // Handle invalid action 
} 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.138.138