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

maxxrdrgz June 13, 2017

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'

 

2 answers

1 accepted

2 votes
Answer accepted
Daniel Yelamos [Adaptavist]
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.
June 14, 2017

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

 

maxxrdrgz June 14, 2017

How do you instantiate projectService?

Daniel Yelamos [Adaptavist]
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.
June 15, 2017

Apologies:

You get it like this

def static projectService = ComponentAccessor.getComponent(ProjectService)

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

maxxrdrgz June 15, 2017

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

Abyakta Lenka July 13, 2017

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)

Daniel Yelamos [Adaptavist]
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 13, 2017

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

0 votes
Mahdi Challouf September 5, 2019

Hello All, 

when creating the new project can I set to it another existing workflow because the builder takes the existing one in the default workflow scheme.

best regards  

Suggest an answer

Log in or Sign up to answer