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.
Follow these steps:
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.
Status status = issue.getStatusObject(); com.opensymphony.workflow.loader.StepDescriptor currentStep = workFlow.getLinkedStep(status);
List<ActionDescriptor> actions = currentStep.getActions();
Here, actions
is a list of com.opensymphony.workflow.loader.ActionDescriptor
.
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()) }
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?
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.
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 }
18.217.138.138