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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Script Runner: Copy workflows and associate with a new project

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

1 vote
Answer accepted

Thanks @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()

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

Guy, I have to say - THANK YOU!

This has helped me immeasurably.

Again, thanks.

Like Guy Geron likes this

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...

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
Derek Fields Community Leader Jan 07, 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. 

Hi @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

Derek Fields Community Leader Jan 07, 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.

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you