Script Runner: Copy workflows and associate with a new project

Guy Geron January 6, 2020

Hi,

 

I have a workflow scheme that serves as a template. It holds workflow templates.

When I create a new project, I'd like to copy the template workflows to the new project scheme. 

Is there a script someone can share?

 

Cheers,

Guy

3 answers

1 accepted

2 votes
Answer accepted
Guy Geron January 14, 2020

Thanks @C_ Derek Fields  I've figured it out.

For anyone else that might need this code, here it is:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project

 

def workflowManager = ComponentAccessor.workflowManager
def schemeManager = ComponentAccessor.workflowSchemeManager
def projectManager = ComponentAccessor.projectManager
def issueManager = ComponentAccessor.issueManager
def issueTypeScheme = ComponentAccessor.issueTypeSchemeManager
def userManager = ComponentAccessor.userManager

 

//Create a project objects (Source & Target)
Project jiraProjectSourceObj = projectManager.getProjectObjByName("YOUR SOURCE PROJECT")
Project jiraProjectTargetObj = projectManager.getProjectObjByName("YOUR TARGET PROJECT")

//Create a user object
// need to figure out how to get the current user!
def adminUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

//defining Workflow Scheme
def newWorkflowSchemeGenericObj
def newWorkflowSchemeObj
def newWorkflowSchemeName
def newWorkflowSchemeDescription

// Create Workflows properties
def newWorkflowName = ""
def newWorkflowDescription = "To be used with this project"
def sourceWorkflowName
def jiraWorkflowObj
def jiraWorkflowCloneObj

//Create a issue type scheme object

def sb = new StringBuffer()

try{
//Creating a Workflow Scheme Object
newWorkflowSchemeName = jiraProjectTargetObj.key +" Workflow Scheme"
newWorkflowSchemeDescription = "To be used only in " +jiraProjectTargetObj.key+ " project"
newWorkflowSchemeObj = schemeManager.createSchemeObject(newWorkflowSchemeName, newWorkflowSchemeDescription)



issueTypeScheme.getIssueTypesForProject(jiraProjectSourceObj).each{

sourceWorkflowName = schemeManager.getWorkflowSchemeObj(jiraProjectSourceObj).getActualWorkflow(it.id)
jiraWorkflowObj = workflowManager.getWorkflow(sourceWorkflowName)

//adjusting the name of the workflow to suit our naming convention
newWorkflowName = jiraWorkflowObj.name.replaceAll("Template", jiraProjectTargetObj.key)
jiraWorkflowCloneObj = workflowManager.copyWorkflow(adminUser, newWorkflowName, newWorkflowDescription, jiraWorkflowObj)

//Converting the Scheme Object to a Generic Object so that I can add the workflows to the scheme
newWorkflowSchemeGenericObj = schemeManager.getScheme(newWorkflowSchemeObj.id)
schemeManager.addWorkflowToScheme(newWorkflowSchemeGenericObj, jiraWorkflowCloneObj.name, it.id)

newWorkflowName = ""
}

//Switching Workflow Schemes
schemeManager.removeSchemesFromProject(jiraProjectTargetObj)
schemeManager.addSchemeToProject(jiraProjectTargetObj, newWorkflowSchemeObj)

}

 

catch(Exception e) {
//noop
sb.append(" || Error: " + e + "\n");
}


return sb.toString()

mesa sisa June 22, 2020

works great.

if you dont want to create new workflows then you can just comment out following lines:

//adjusting the name of the workflow to suit our naming convention
newWorkflowName = jiraWorkflowObj.name.replaceAll("Template", jiraProjectTargetObj.key)
jiraWorkflowCloneObj = workflowManager.copyWorkflow(adminUser, newWorkflowName, newWorkflowDescription, jiraWorkflowObj)

and then use this line:

schemeManager.addWorkflowToScheme(newWorkflowSchemeGenericObj, jiraWorkflowObj.name, it.id)

Like Guy Geron likes this
Kevin Ketchum July 17, 2020

Guy, I have to say - THANK YOU!

This has helped me immeasurably.

Again, thanks.

Like Guy Geron likes this
David Meyer August 10, 2020

Do they have this working for Jira Cloud yet?  I see other threads about it only for Jira Server, but they were last year.    Specifically for Service Desk...

Invir BILALOVIC
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 19, 2020

Hello good work,

I'm searching this to create a complete Project for duplicating all scheme and objects (Screen, Fieds Customisation...) from a template project.

 

I try your script and I got this answer when i try :

|| Error: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.workflow.DefaultWorkflowSchemeManager.addWorkflowToScheme() is applicable for argument types: (com.atlassian.jira.scheme.Scheme, String, String) values: [Scheme{id=10205, type=WorkflowScheme, name=TESDUP_WS, description=To be used only in TESDUP project, entities=[0]}, ...] Possible solutions: addWorkflowToScheme(org.ofbiz.core.entity.GenericValue, java.lang.String, java.lang.String
It seems to not appreciate this :  schemeManager.addWorkflowToScheme(newWorkflowSchemeGenericObj, jiraWorkflowCloneObj.name, it.id)
0 votes
Guy Geron January 7, 2020

Hi @C_ Derek Fields ,

 

I'd like each project to have their own workflow instance. So that each delivery team can make changes to their flow without any impact on the other delivery team.

 

Cheers,

 

Guy

C_ Derek Fields
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 7, 2020

Creating such a script isn't that difficult, but it is fair amount of work. For me, it would take about 2-3 hours to write and test. Maybe someone else can do it faster.

0 votes
C_ Derek Fields
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 7, 2020

Why not just use the "Create with Shared Confiuration" option in the Project Creation screen? That will copy all of the schemes associated with the template project to the new project. 

Rafał Żydek May 9, 2024

Create with Shared Confiuration make configuration shared, instead of copy.

Guy Geron May 28, 2024

In our Jira design, we wanted to provide Jira projects with the flexibility to modify it for their own needs. But I needed a starting point to set up a project.
When you copy/clone a workflow, any changes you make are dedicated to its Jira project and don't impact others.

 

With shared configuration, that doesn't work. Every change you make to the workflow will be reflected in other projects that will use the workflow.

Suggest an answer

Log in or Sign up to answer