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

Create project Jira using Script

Fidelidade JIRA Admin July 7, 2022

Hi All,

As I have several types of projects in my instance, I created several templates.
So, I created a project in Jira, with a Kanban board and a workflow with three states (Backlog, Rifinement, Done).

From this project, in the transition from the "Rifinement" to "Done" state, I intend to run the script to clone one of the templates.
and I have this script that in Jira says it ran, but it doesn't generate any clones. And in the log I don't see anything that helps me understand why.
Can anyone help me?
It is very important for me because I create many projects manually.

Here is the script I'm using:

 

import com.atlassian.jira.bc.project.ProjectCreationData
import com.atlassian.jira.bc.project.ProjectService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.AssigneeTypes
import com.atlassian.jira.project.type.ProjectTypeKey

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: ["jira-administrators"]) { MultivaluedMap queryParams, String body ->
def project_key = queryParams.getFirst("CPBS ProjectKey") as String
def project_name = queryParams.getFirst("CPBS ProjectName") 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 = 'FXSPT'
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();

 

Thank you

Emanuel Lima

1 comment

Comment

Log in or Sign up to comment
Radek Dostál
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 7, 2022

Peter-Dave posted a pretty nice response here https://community.atlassian.com/t5/Jira-questions/Can-I-run-the-built-in-scripts-externally/qaq-p/1149898

 

The snippet you have seems either incomplete or wrongly copied. In the last part I only see

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();
I assume that the actual "do create that project I want" should be inside the else block, so this depends whether it's just a bad copy paste, but in any case, Peter-Dave's comment includes the entire snippet in REST endpoint as well.
Although, I suspect that recreating all of it manually would only be harder to maintain. If you are a global Jira admin (and you are restricting that endpoint to it in any case), then it might be easier to simply call the built-in script directly (it's some parameterized http post I would guess?), rather than creating and maintaining an extra endpoint which effectively would do the same thing.
TAGS
AUG Leaders

Atlassian Community Events