I am trying to Automate A project creation using the a Script

G Sunil Kumar January 3, 2021

Hello,

 

I have enabled a post function to run a script get value from Custom Fields and create project automatically. 

 

Script :- 

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 org.apache.log4j.Logger
import org.apache.log4j.Level

def logg = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
logg.setLevel(Level.INFO)

// the key for the new project from custom field
def projectKey = "Project Key"
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def projectKeyV = customFieldManager.getCustomFieldObjectByName(projectKey)
def getKeyV = issue.getCustomFieldValue(projectKeyV)
log.info("The Key is" + getKeyV )

// the name of the new project
def projectName = "Project Name"
def projectNameV = customFieldManager.getCustomFieldObjectByName(projectName)
def getNameV = issue.getCustomFieldValue(projectNameV)
log.info("The Key is" + getNameV )

def projectService = ComponentAccessor.getComponent(ProjectService)
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

// available project type keys: business | software | service_desk
def projectTypeKey = new ProjectTypeKey("software")

def creationData = new ProjectCreationData.Builder().with {
withName(getNameV)
withKey(getKeyV)
withLead(loggedInUser)
withUrl(null)
withAssigneeType(AssigneeTypes.PROJECT_LEAD)
withType(projectTypeKey)
}.build()

final ProjectService.CreateProjectValidationResult projectValidationResult = projectService.validateCreateProject(loggedInUser, creationData)
assert projectValidationResult.isValid() : projectValidationResult.errorCollection

projectService.createProject(projectValidationResult)

 

I am getting an error when I am running this script

Error:-

ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue JIRA-1731 for user 'Sunilkumar'. workflowMode=live&workflowName=New+JS+Workflow&descriptorTab=postfunctions&workflowTransition=481&highlight=1
java.lang.AssertionError: Errors: {projectType=An invalid project type was specified. Make sure the project type is available in your Jira instance. Please talk to your Jira administrator if you need more help.}
Error Messages: []. Expression: projectValidationResult.isValid()
	at Script111.run(Script111.groovy:44)
	at com.onresolve.scriptrunner.runner.AbstractScriptRunner.runScriptAndGetContext(AbstractScriptRunner.groovy:174)
	at com.onresolve.scriptrunner.runner.AbstractScriptRunner$runScriptAndGetContext$3.callCurrent(Unknown Source)
	at com.onresolve.scriptrunner.runner.AbstractScriptRunner.runScriptAndGetContext(AbstractScriptRunner.groovy:293)
	at com.onresolve.scriptrunner.runner.AbstractScriptRunner$runScriptAndGetContext$2.callCurrent(Unknown Source)
	at com.onresolve.scriptrunner.runner.AbstractScriptRunner.runScript(AbstractScriptRunner.groovy:305)
	at com.onresolve.scriptrunner.runner.ScriptRunner$runScript$10.call(Unknown Source)
	at com.onresolve.scriptrunner.canned.jira.utils.TypedCustomScriptDelegate.execute(TypedCustomScriptDelegate.groovy:19)
	at com.onresolve.scriptrunner.canned.jira.utils.TypedCustomScriptDelegate$execute$0.call(Unknown Source)
	at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CustomScriptFunction.execute(CustomScriptFunction.groovy:53)
	at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CustomScriptFunction$execute.callCurrent(Unknown Source)
	at com.onresolve.scriptrunner.canned.jira.workflow.AbstractWorkflowCannedScript.execute(AbstractWorkflowCannedScript.groovy:20)
	at com.onresolve.scriptrunner.canned.jira.workflow.AbstractWorkflowCannedScript$execute$1.call(Unknown Source)
	at com.onresolve.scriptrunner.jira.workflow.AbstractScriptWorkflowFunction$_run_closure2.doCall(AbstractScriptWorkflowFunction.groovy:89)
	at com.onresolve.scriptrunner.jira.workflow.AbstractScriptWorkflowFunction$_run_closure2.doCall(AbstractScriptWorkflowFunction.groovy)
	at com.onresolve.scriptrunner.runner.diag.DiagnosticsManagerImpl$DiagnosticsExecutionHandlerImpl$_execute_closure1.doCall(DiagnosticsManagerImpl.groovy:350)
	at com.onresolve.scriptrunner.runner.diag.DiagnosticsManagerImpl$DiagnosticsExecutionHandlerImpl$_execute_closure1.doCall(DiagnosticsManagerImpl.groovy)
	at com.onresolve.scriptrunner.runner.ScriptExecutionRecorder.withRecording(ScriptExecutionRecorder.groovy:13)
	at com.onresolve.scriptrunner.runner.ScriptExecutionRecorder$withRecording.call(Unknown Source)
	at com.onresolve.scriptrunner.runner.diag.DiagnosticsManagerImpl$DiagnosticsExecutionHandlerImpl.execute(DiagnosticsManagerImpl.groovy:348)
	at com.onresolve.scriptrunner.runner.diag.DiagnosticsExecutionHandler$execute$3.call(Unknown Source)
	at com.onresolve.scriptrunner.jira.workflow.AbstractScriptWorkflowFunction.run(AbstractScriptWorkflowFunction.groovy:82)

 Regards,

Sunil

2 answers

0 votes
Vignesh Singh September 29, 2021

Hi @G Sunil Kumar 

I tried with the same script in my instance. Working perfectly. Thanks a ton

-Vignesh

0 votes
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.
January 3, 2021

That's quite a specific error:

projectType=An invalid project type was specified

 

Before you build the ProjectCreationData - can you log all of the variables so we can see what is being built?

Can you also check the ApplicationManager whether it thinks that Jira Software is in fact installed? You can use this:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.application.api.Application;
import com.atlassian.application.api.ApplicationManager;
import com.atlassian.jira.application.ApplicationKeys;
import io.atlassian.fugue.Option;

isJiraSoftwareInstalled();

boolean isJiraSoftwareInstalled() {
ApplicationManager applicationManager = ComponentAccessor.getComponent(ApplicationManager.class);
Option<Application> jiraSoftwareApplication = applicationManager.getApplication(ApplicationKeys.SOFTWARE);
return jiraSoftwareApplication != null;
}

 

Another thing you could try is sending a String instead of ProjectTypeKey variable to the 'withType( .. )' method, although your implementation certainly looks correct to me.

G Sunil Kumar January 4, 2021

Hi Radek,

 

Thank You for the update, it was an issue on our end on JIRA software and it is working fine.

But, on the same note, can i use the above script to copy the configuration of a Project and create it?

 

Regards,

Sunil

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.
January 4, 2021

Hi,

 

Similar to it yes, you would want to use the following 2 instead:

Validation based on "fromExistingProject":

https://docs.atlassian.com/software/jira/docs/api/8.5.8/com/atlassian/jira/bc/project/ProjectCreationData.Builder.html#fromExistingProject-com.atlassian.jira.project.Project-com.atlassian.jira.bc.project.ProjectCreationData-

 

And then get a CreateProjectValidationResult from the "based on existing project" method:

https://docs.atlassian.com/software/jira/docs/api/8.5.8/com/atlassian/jira/bc/project/ProjectService.html#validateCreateProjectBasedOnExistingProject-com.atlassian.jira.user.ApplicationUser-java.lang.Long-com.atlassian.jira.bc.project.ProjectCreationData-

 

This should get you the same result as if you went in the UI and used a "Create project based on existing template" feature.

 

However I must say I'm not familiar with Cloud or whether above is valid there (it is on Server), I did not verify nor have tried this with Cloud, I was however able to get this to work on Server in one of my assignments.

 

Note - creating based on existing project does what the UI does (from my experience, and again on Server). You still have to figure out how you want to handle project role actors and custom field contexts, but the above will get you a working project with the same schemes as the source project.

 

Regards,

Radek

G Sunil Kumar January 4, 2021

Hi Radek,

 

Thank You for the update, will give it a try. 

 

Regards,

Sunil

Suggest an answer

Log in or Sign up to answer