Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How remove workflow from workflow scheme?

PatrykG May 4, 2016

Hello,

 

My JIRA plugin creating new IssueType, Workflow and adding it to existing WorkflowScheme via:

GenericValue workflowScheme = workflowSchemeManager.getWorkflowScheme(project);
workflowSchemeManager.addWorkflowToScheme(workflowScheme, jiraWorkflow.getName(), issueType.getId());

Unfortunately I cannot find any method to remove workflow from workflow scheme.

 

I would be grateful for any hint how to do it.

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
PatrykG May 6, 2016

Thanks to Nic Brough [Adaptavist] I found solution. As Nic said it's needed to create draft workflow scheme.

Note that below solution missing migration of issues  = can broke all issues that using workflow that we removing.

package com.mycompany.plugin.service;

import org.springframework.stereotype.Component;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.workflow.AssignableWorkflowScheme;
import com.atlassian.jira.workflow.DraftWorkflowScheme;
import com.atlassian.jira.workflow.JiraWorkflow;
import com.atlassian.jira.workflow.WorkflowScheme;
import com.atlassian.jira.workflow.WorkflowSchemeManager;
import com.google.common.collect.Iterables;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class MyWorkflowSchemeService {
	private WorkflowSchemeManager workflowSchemeManager = ComponentAccessor.getComponent(WorkflowSchemeManager.class);
	public void unlinkFromWorkflowSchemes(JiraWorkflow jiraWorkflow) {
		Iterable<WorkflowScheme> workflowSchemes = workflowSchemeManager.getSchemesForWorkflowIncludingDrafts(jiraWorkflow);
		log.debug("Found {} workflow schemes for \"{}\"", Iterables.size(workflowSchemes), jiraWorkflow.getName());
		for (WorkflowScheme workflowScheme : workflowSchemes) {
			log.debug("Unlinking workflow \"{}\" from  workflowScheme \"{}\"", jiraWorkflow.getName(), workflowScheme.getName());
			if (workflowScheme instanceof AssignableWorkflowScheme) {
				this.unlinkFromWorkflowSchemes((AssignableWorkflowScheme) workflowScheme, jiraWorkflow);
			} else {
				log.debug("WorkflowScheme \"{}\" is not instance of AssignableWorkflowScheme - cannot unlink", workflowScheme.getName());
			}
		}
	}
	private void unlinkFromWorkflowSchemes(AssignableWorkflowScheme assignableWorkflowScheme, JiraWorkflow jiraWorkflow) {
		DraftWorkflowScheme draftWorkflowScheme = getDraft(assignableWorkflowScheme);
		draftWorkflowScheme = draftWorkflowScheme.builder().removeWorkflow(jiraWorkflow.getName()).build();
		workflowSchemeManager.replaceSchemeWithDraft(draftWorkflowScheme);
		workflowSchemeManager.clearWorkflowCache();
	}
	private DraftWorkflowScheme getDraft(AssignableWorkflowScheme assignableWorklflowScheme) {
		if (workflowSchemeManager.hasDraft(assignableWorklflowScheme)) {
			log.debug("Draft for \"{}\" already exists. Using existing draft.", assignableWorklflowScheme.getName());
			return workflowSchemeManager.getDraftForParent(assignableWorklflowScheme);
		}
		return workflowSchemeManager.createDraftOf(null, assignableWorklflowScheme);
	}
}
1 vote
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 4, 2016

I think you have to create a draft workflow scheme with the new list of workflows (i.e. the same as the existing one, minus the one you want to delete) and then make the draft live. 

I'm not sure how you're going to pass it all the data about what to migrate existing issues to though.

TAGS
AUG Leaders

Atlassian Community Events