Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,366,270
Community Members
 
Community Events
168
Community Groups

Create project by scriptrunner

Hello ,
I would like to be able to create jira projects with existing templates through a script.
Procedure:
1. Through the transition of a card on the board, you must create a project.
2. Projects must be created using pre-created templates in JIra.
3. For example, from a card with the id, name, type of project and scrum master the project must be created.
4. There must be a condition to compare if the card has a waterfall type field, for example, a project must be created through the waterfall template.
5. If it is Agile, a project must be created using the Agile template.

Can you help me with this?
Thank in advance,
BR

2 answers

Hi Martin,

The script did not create any projects.
As you can see in the image, it didn't generate any logs either.
Am I doing something wrong?
Thank you
Emanuel

scrip_log.JPG

Hi @Emanuel Paquetelima I guess I have the script you need:

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

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


//change this to your template source project
def srcProjectKey = 'SPK' // source project key
def getSrcProjectObject = projectManager.getProjectByCurrentKey(srcProjectKey)

def targetKey = "SP" // you must adjust for your purpose
def targetProjectName = "Some project" // you must adjust for your purpose

if(getSrcProjectObject && targetKey && targetProjectName) {


//You have to wrap the thread in JiraThreadLocalUtils in Jira 8 or no issues will be cloned
Thread executorThread = new Thread(JiraThreadLocalUtils.wrap {
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) : true,
(CopyProject.FIELD_COPY_COMPONENTS) : true,
(CopyProject.FIELD_COPY_ISSUES) : true,
(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.warn("Couldn't create project: $errorCollection")
} else {
def util = ComponentAccessor.getUserUtil()
def adminsGroup = ComponentAccessor.getGroupManager().getGroup("jira-administrators")
assert adminsGroup // must have jira-administrators group defined
def admins = util.getAllUsersInGroups([adminsGroup])
assert admins // must have at least one admin

ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(util.getUserByName(admins.first().name))
copyProject.doScript(inputs)
}

})

//must call start Not run or the Post functions will throw an error because it tries to access a dead thread
executorThread.start()

}else{
log.error("Cannot Clone Project as source Project, target key or target project names were Not found or Not specified")
}

this script copies also issues, versions and components from source project

Good Day Martin Bayer,
Thank you very much for your feedback, and I apologize for the late response.
I adapted your code according to my needs, but when I run it, it returns null.
Am I doing something wrong?

 

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

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


//change this to your template source project
def srcProjectKey = 'BCKUATOUT' // source project key
def getSrcProjectObject = projectManager.getProjectByCurrentKey(srcProjectKey)

def targetKey = "CPYBCK" // you must adjust for your purpose
def targetProjectName = "CPYBCKUATOUT" // you must adjust for your purpose

if(getSrcProjectObject && targetKey && targetProjectName) {


//You have to wrap the thread in JiraThreadLocalUtils in Jira 8 or no issues will be cloned
Thread executorThread = new Thread(JiraThreadLocalUtils.wrap {
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) : true,
(CopyProject.FIELD_COPY_COMPONENTS) : true,
(CopyProject.FIELD_COPY_ISSUES) : true,
(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.warn("Couldn't create project: $errorCollection")
} else {
def util = ComponentAccessor.getUserUtil()
def adminsGroup = ComponentAccessor.getGroupManager().getGroup("jira-administrators")
assert adminsGroup // must have jira-administrators group defined
def admins = util.getAllUsersInGroups([adminsGroup])
assert admins // must have at least one admin

ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(util.getUserByName(admins.first().name))
copyProject.doScript(inputs)
}

})

//must call start Not run or the Post functions will throw an error because it tries to access a dead thread
executorThread.start()

}else{
log.error("Cannot Clone Project as source Project, target key or target project names were Not found or Not specified")
}

Hi @Fidelidade JIRA Admin it will return null because you are starting a new thread. But is the project created? Can you see any log messages in your atlassian-jira.log file?

Hi Martin,

The script did not create any projects.
As you can see in the image, it didn't generate any logs either.
Am I doing something wrong?
Thank you
Emanuel

scrip_log.JPG

Hi Martin,

Any news regarding this post?

Thank you

BR

Hi @Fidelidade JIRA Admin you need to check your server log, not only the script runner console log

Hi Martin,

How are you?

I got these logs, but I can't find anything that helps me understand why the script doesn't create project. 

Can you help?

Thank you

Hi Martin,

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.

 

Emanuel Lima

Hi @Fidelidade JIRA Admin it is difficult for me to help you without access to your environment but I always suggest (and I use it in this way) to log as much log.error messages as necessary. If you put your log entries and check your server log it is helpful because you can find out what part of code is OK and what part of it is wrong. For example you can change

//(CopyProject.FIELD_ORDER_BY) : "Rank", <-- no longer a valid option so I am commenting this out
]

def errorCollection = copyProject.doValidate(inputs, false)

to

//(CopyProject.FIELD_ORDER_BY) : "Rank", <-- no longer a valid option so I am commenting this out
]
log.error "going to get an error collection"
def errorCollection = copyProject.doValidate(inputs, false)


Do you know what I mean?

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events