Create Workflow through Java API

Hey guys, I’m having a hard time searching on docs a way to create a Worklfow through Java API and updating a Workflow Scheme for all existing projects.

This is what I got so far…

    private JiraWorkflow createCustomWorkflow() {
        JiraWorkflow workflow = null;

        // Check if it already exists
        Collection<JiraWorkflow> workflows = workflowManager.getWorkflows();
        for (JiraWorkflow jiraWorkflow : workflows){
            if(workflow.getName().equals("Our Fancy Workflow"))
                workflow = jiraWorkflow;
        }

        // Create if not
        if (workflow == null){
            // Create Draft
            workflow = workflowManager
                    .createDraftWorkflow(getUser(), "Our Fancy Workflow");
        }

        // Make sure all statuses exist
        List<Status> statuses = getStatuses();

        // add them to workflow
        for(Status status : testRunStatuses){
            StepDescriptor newStep = DescriptorFactory.getFactory().createStepDescriptor();
            newStep.setName(status.getName());
            newStep.setId(WorkflowUtil.getNextId(workflow.getDescriptor().getSteps()));
            newStep.getMetaAttributes().put("jira.status.id", status.getId());

            newStep.setParent(workflow.getDescriptor());
            workflow.getDescriptor().addStep(newStep);
        }

        // TODO
        // add Transitions
        for(int i = 0; i < transitions.values().length; i++){
            Collection<ActionDescriptor> actions = testRunWorkflow.getActionsByName(transitions.values()[i].getName());
            if(actions.size() == 0){
                // create action & add it to our JiraWorkflow - but how
                ActionDescriptor transition = new ActionDescriptor().setName();
            }

        }
        // Do I need to persist aka call store() on the workflow?


        return workflow;
    }

Is there any interface I might use to add transitions and properly store them? Thanks in advance

For each transition, you need to create a ResultDescriptor:

    ResultDescriptor result =
        DescriptorFactory.getFactory().createResultDescriptor();
    result.setStep(barStep.getId());
    result.setOldStatus("Foo");
    result.setStatus("Bar");

Then you need to give that result as the unconditional result of your action:

action.setParent(fooStep);
action.setUnconditionalResult(result);

Then

fooStep.getActions().add(action);
3 Likes