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
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.ComponentAccessorimport com.atlassian.jira.project.Project
def workflowManager = ComponentAccessor.workflowManagerdef schemeManager = ComponentAccessor.workflowSchemeManagerdef projectManager = ComponentAccessor.projectManagerdef issueManager = ComponentAccessor.issueManagerdef issueTypeScheme = ComponentAccessor.issueTypeSchemeManagerdef 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 Schemedef newWorkflowSchemeGenericObjdef newWorkflowSchemeObjdef newWorkflowSchemeNamedef newWorkflowSchemeDescription
// Create Workflows propertiesdef newWorkflowName = ""def newWorkflowDescription = "To be used with this project"def sourceWorkflowNamedef jiraWorkflowObjdef jiraWorkflowCloneObj
//Create a issue type scheme object
def sb = new StringBuffer()
try{//Creating a Workflow Scheme ObjectnewWorkflowSchemeName = 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 conventionnewWorkflowName = 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 schemenewWorkflowSchemeGenericObj = schemeManager.getScheme(newWorkflowSchemeObj.id)schemeManager.addWorkflowToScheme(newWorkflowSchemeGenericObj, jiraWorkflowCloneObj.name, it.id)newWorkflowName = ""}//Switching Workflow SchemesschemeManager.removeSchemesFromProject(jiraProjectTargetObj)schemeManager.addSchemeToProject(jiraProjectTargetObj, newWorkflowSchemeObj)
}
catch(Exception e) {//noopsb.append(" || Error: " + e + "\n");}
return sb.toString()
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.
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.
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.
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 conventionnewWorkflowName = 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)
Guy, I have to say - THANK YOU!
This has helped me immeasurably.
Again, thanks.
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...
Create with Shared Confiuration make configuration shared, instead of copy.
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.
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.
It looks like you're new here. Sign in or register to get started.