How to create a project in JIRA using java api and scriptrunner Edited

Hello All, 

For the life of me I cannot find a simple example on how to create a project using JIRA's java api. I am getting my feet wet into the api and groovy so I don't know much. I am very familiar with the REST api however I do not want to use that because it is limited in terms of setting schemes.

import com.atlassian.jira.user.ApplicationUser
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.Project
import com.atlassian.jira.util.ErrorCollection


//def user = ComponentAccessor.getUserManager().getUserByName('admin')
ApplicationUser user = ComponentAccessor.getUserManager().getUserByName('admin')


ProjectCreationData.Builder builder = new ProjectCreationData.Builder()

builder.withKey('KEY123').withName('KEY123').withLead(user)

ProjectCreationData data = builder.build()

Project validatedProject = ComponentAccessor.getProjectManager().createProject(user,data);

The error I get is:

java.lang.IllegalStateException
	at com.google.common.base.Preconditions.checkState(Preconditions.java:158)
	at com.atlassian.jira.project.DefaultProjectManager.createProject(DefaultProjectManager.java:177)
	at com.atlassian.jira.project.CachingProjectManager.createProject(CachingProjectManager.java:143)
	at com.atlassian.jira.project.ProjectManager$createProject.call(Unknown Source)
	at test.run(test.groovy:19)

Will using scriptrunner and the java api allow me to set more complicated schemes like issue, screen, workflow etc..?

 

Thanks,

Maxx

EDIT:

This is the correct answer thanks to Daniel, at the time of this post, this answer is compliant with api 7.3.6

import com.atlassian.jira.bc.project.ProjectCreationData.Builder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.bc.project.ProjectService


def user = ComponentAccessor.getUserManager().getUserByName('admin')
def projectService = ComponentAccessor.getComponent(ProjectService)


Builder builder = new Builder()

builder.withKey('MAXX23').withName('MAXX23').withLead(user).withDescription('helloworld').withType('business')

def data = builder.build()

def projectResult = projectService.validateCreateProject(user, data)
Project prj = projectService.createProject(projectResult)

return 'finished'

 

1 answer

1 accepted

Hi Maxxrdrgz.

To create a project in Jira you firrst need to validate the creation of the project. Then you need to create the project using the validation results of that first step. 

This is how you would create a project with scriptrunner:

import com.atlassian.jira.bc.project.ProjectService
def static
projectService = ComponentAccessor.getComponent(ProjectService)

def createProjectValidationResult = projectService.validateCreateProject(currentUser, projectName, key, null, currentUser.name, null, AssigneeTypes.PROJECT_LEAD) project = projectService.createProject(createProjectValidationResult)

The variabels passed onto the first function come from another script, so do not take the values literally, you can have a read on the official documentation here.

If I can help you any further, please let me know.

Cheers!

DYelamos

 

How do you instantiate projectService?

Apologies:

You get it like this

def static projectService = ComponentAccessor.getComponent(ProjectService)

 I updated my answer to better reflect what you need to do.

Awesome! Thank you so much! I finally figured it out!

Hello Daniel , Maxxrdrgz

 

When i tried the above code it is giving me an error :

 

java.lang.IllegalStateException: You can not create a project with an invalid validation result. at com.atlassian.jira.bc.project.DefaultProjectService.createProjectInternal(DefaultProjectService.java:382) at com.atlassian.jira.bc.project.DefaultProjectService.createProject(DefaultProjectService.java:373) at com.atlassian.jira.bc.project.ProjectService$createProject$0.call(Unknown Source) at Script6.run(Script6.groovy:18)

That means that the validation is getting a fail. Which means that you are feeding the wrong data into the creation of the validation.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,958 views 12 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot