//Based on this example https://gist.github.com/jamieechlin/8240692importcom.atlassian.jira.component.ComponentAccessorimportcom.atlassian.jira.util.thread.JiraThreadLocalUtilsimportcom.onresolve.scriptrunner.canned.jira.admin.CopyProjectimportorg.apache.log4j.Loggerimportorg.apache.log4j.Leveldeflog=Logger.getLogger(getClass())log.setLevel(Level.DEBUG)defcustomFieldManager=ComponentAccessor.getCustomFieldManager()defprojectManager=ComponentAccessor.getProjectManager()//These fields will need to be created as single line text fields so users can provide the new project key and project namedefdestProjetKey=customFieldManager.getCustomFieldObjectsByName("New Project KEY")[0]log.debug("New Project KEY: "+issue.getCustomFieldValue(destProjetKey))defdestProjectName=customFieldManager.getCustomFieldObjectsByName("New Project Name")[0]log.debug("New Project Name: "+issue.getCustomFieldValue(destProjectName))//change this to your template source projectdefsrcProjectKey='SSPA'log.debug("Source Project Name: "+srcProjectKey)defgetSrcProjectObject=projectManager.getProjectByCurrentKey(srcProjectKey)//Use getSrcProjectObject to check the source project existsdeftargetKey=issue.getCustomFieldValue(destProjetKey)deftargetProjectName=issue.getCustomFieldValue(destProjectName)if(getSrcProjectObject&&targetKey&&targetProjectName){//You have to wrap the thread in JiraThreadLocalUtils in Jira 8 or no issues will be clonedThreadexecutorThread=newThread(JiraThreadLocalUtils.wrap{defcopyProject=newCopyProject()definputs=[(CopyProject.FIELD_SOURCE_PROJECT):srcProjectKey,(CopyProject.FIELD_TARGET_PROJECT):targetKey,(CopyProject.FIELD_TARGET_PROJECT_NAME):targetProjectName,(CopyProject.FIELD_COPY_VERSIONS):true,(CopyProject.FIELD_COPY_COMPONENTS):true,(CopyProject.FIELD_COPY_ISSUES):true,(CopyProject.FIELD_COPY_DASH_AND_FILTERS):false,//(CopyProject.FIELD_ORDER_BY) : "Rank", <-- no longer a valid option so I am commenting this out]deferrorCollection=copyProject.doValidate(inputs,false)if(errorCollection.hasAnyErrors()){log.warn("Couldn't create project: $errorCollection")}else{defutil=ComponentAccessor.getUserUtil()defadminsGroup=ComponentAccessor.getGroupManager().getGroup("jira-administrators")assertadminsGroup// must have jira-administrators group defineddefadmins=util.getAllUsersInGroups([adminsGroup])assertadmins// must have at least one adminComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(util.getUserByName(admins.first().name))copyProject.doScript(inputs)}})//must call start Not run or the Post functions will throw an error because it tries to access a dead threadexecutorThread.start()}else{log.error("Cannot Clone Project as source Project, target key or target project names were Not found or Not specified")}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.