Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script Runner: Copy workflows and associate with a new project

Guy Geron
Contributor
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

4 answers

1 accepted

3 votes
Answer accepted
Guy Geron
Contributor
January 14, 2020

Thanks @Derek Fields _RightStar_  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
Contributor
July 17, 2020

Guy, I have to say - THANK YOU!

This has helped me immeasurably.

Again, thanks.

Like • Guy Geron likes this
David Meyer
Contributor
August 10, 2020 edited

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)
2 votes
Alper Firengiz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 26, 2025 edited

I have modified the script to create a new workflow schema and a workflow cloned from SOURCE PROJECT, and assign WF/WFS to the TARGET PROJECT while removing the default WF/WFS of the TARGET. 

It still assigning the WF for each issue type. 

Jira 9.12.15/Scriptrunner 8.42.0

 

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("SOURCE PROJECT")
Project jiraProjectTargetObj = projectManager.getProjectObjByName("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
// A - added a static string value for newWorkflowName here
def newWorkflowName = "New Workflow by Scriptrunner"
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 Created by Scriptrunner"
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
// A - commented out below line
// newWorkflowName = jiraWorkflowObj.name.replaceAll("Template", jiraProjectTargetObj.key)


jiraWorkflowCloneObj = workflowManager.copyWorkflow(adminUser, newWorkflowName, newWorkflowDescription, jiraWorkflowObj)

newWorkflowSchemeGenericObj = schemeManager.getScheme(newWorkflowSchemeObj.id)

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


// A - commented out below line to avoid resetting newWorkflowName, every time the copyWorkflow works it finds the previously created workflow and updates it.
//newWorkflowName = ""
}

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

}


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

 

I am sharing as an example and as is, you can modify it for your needs. 

0 votes
Guy Geron
Contributor
January 7, 2020

Hi @Derek Fields _RightStar_ ,

 

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

Derek Fields _RightStar_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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
Derek Fields _RightStar_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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
Contributor
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