First of all, please remember How to create and invoke a new REST Endpoint in Jira:

In this exercise we will create a REST Endpoint in our Jira using Scriptrunner plugin to clone an existing project. The REST endpoint in this case accept two params: project_key and project_name.

Here the code, just replace ‘SOURCE_PROJECT_KEY‘ by the Key of an existing project in your Jira (to be cloned by the REST method), also change “admin” by an admin user in your installation, also in groups: [“administrators”] you can use other group or groups.


import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

import com.atlassian.jira.component.ComponentAccessor
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)

@BaseScript CustomEndpointDelegate delegate

newClonedProject(httpMethod: "GET", groups: ["administrators"]) { MultivaluedMap queryParams, String body ->
def project_key = queryParams.getFirst("project_key") as String
def project_name = queryParams.getFirst("project_name") as String
if (!project_key) return Response.ok(new JsonBuilder([project_key: "", error: "Error in param project_key"]).toString()).build();
if (!project_name) return Response.ok(new JsonBuilder([project_key: "", error: "Error in param project_name"]).toString()).build();

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

def destProjetKey = project_key
def destProjectName = project_name

def srcProjectKey = 'SOURCE_PROJECT_KEY'
def getSrcProjectObject = projectManager.getProjectByCurrentKey(srcProjectKey)

def targetKey = destProjetKey
def targetProjectName = destProjectName

if(getSrcProjectObject && targetKey && targetProjectName) {
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) : false,
(CopyProject.FIELD_COPY_COMPONENTS) : false,
(CopyProject.FIELD_COPY_ISSUES) : false,
(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.debug("Couldn't create project: $errorCollection")
return Response.ok(new JsonBuilder([project_key: "", error: "Couldn't create project: $errorCollection"]).toString()).build();
} else {
def userManager = ComponentAccessor.getUserManager();
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(userManager.getUserByName("admin"))
copyProject.doScript(inputs)
}

}else{
log.error("Cannot Clone Project as source Project, target key or target project names were Not found or Not specified")
return Response.ok(new JsonBuilder([project_key: "", error: "Cannot Clone Project as source Project, target key or target project names were Not found or Not specified"]).toString()).build();
}

return Response.ok(new JsonBuilder([project_key: destProjetKey, error: ""]).toString()).build();
}

Invokation example:

http://jira.example.com/rest/scriptrunner/latest/custom/newClonedProject?project_key=XYXY&project_name=XYXY

By MrAddon

Posted by:.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s