Snippets

Matthew Clark (Adaptavist) Post Function Script - Clone Template Project as a POST function (JIRA 8 ONLY)

Created by Matthew Clark last modified
//Based on this example https://gist.github.com/jamieechlin/8240692

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.util.thread.JiraThreadLocalUtils
import com.onresolve.scriptrunner.canned.jira.admin.CopyProject
import org.apache.log4j.Logger
import org.apache.log4j.Level
def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def projectManager = ComponentAccessor.getProjectManager()

//These fields will need to be created as single line text fields so users can provide the new project key and project name
def destProjetKey = customFieldManager.getCustomFieldObjectsByName("New Project KEY")[0]
log.debug("New Project KEY: " + issue.getCustomFieldValue(destProjetKey))
def destProjectName = customFieldManager.getCustomFieldObjectsByName("New Project Name")[0]
log.debug("New Project Name: " + issue.getCustomFieldValue(destProjectName))

//change this to your template source project
def srcProjectKey = 'SSPA'
log.debug("Source Project Name: " + srcProjectKey)
def getSrcProjectObject = projectManager.getProjectByCurrentKey(srcProjectKey)
//Use getSrcProjectObject to check the source project exists

def targetKey = issue.getCustomFieldValue(destProjetKey)
def targetProjectName = issue.getCustomFieldValue(destProjectName)

if(getSrcProjectObject && targetKey && targetProjectName) {


//You have to wrap the thread in JiraThreadLocalUtils in Jira 8 or no issues will be cloned
    Thread executorThread = new Thread(JiraThreadLocalUtils.wrap {
            def copyProject = new CopyProject()
            def inputs = [
                (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
            ]

        def errorCollection = copyProject.doValidate(inputs, false)
            if (errorCollection.hasAnyErrors()) {
                log.warn("Couldn't create project: $errorCollection")
            } else {
                def util = ComponentAccessor.getUserUtil()
                def adminsGroup = ComponentAccessor.getGroupManager().getGroup("jira-administrators")
                assert adminsGroup // must have jira-administrators group defined
                def admins = util.getAllUsersInGroups([adminsGroup])
                assert admins // must have at least one admin

                ComponentAccessor.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 thread
    executorThread.start()

}else{
    log.error("Cannot Clone Project as source Project, target key or target project names were Not found or Not specified")
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.