Script runner post function to create/copy a project

Hi

Is it possible to create (or copy) a project on workflow post function?

I see there is Built in script for this action, but how can I use it in workflow.

My idea is create simple workflow that selects specific project template (created before) and copy this template to new project. Unfortunately I didn't find any info about it. Is it possible?

Thanks,

Fyodor

5 answers

1 accepted

Hi Fyodor,

I haven't ever actually tried this, and I don't necessarily recommend running this script as a post-function, but you should be able to get the actual content of the script by clicking ''view source'' after having selected the script in the builtin script field.

Put the copied script into your WEB-INF/classes/com/onresolve/jira/groovy/canned/workflow/postfunctions folder and change the

package com.onresolve.jira.groovy.canned.admin

for

package com.onresolve.jira.groovy.canned.workflow.postfunctions

Might require some hacking around but it's a start, depending on how familiar you are with groovy coding:

You can find a bit more info on how this would work here:https://jamieechlin.atlassian.net/wiki/display/GRV/Built-In+Scripts#Built-InScripts-Workflowfunctions-Post-functions

1 vote

You can use this post-function to do this: https://gist.github.com/jamieechlin/8240692

Obviously various params would be replaced by custom field values etc.

You could copy the source, but I don't really recommend it - multiple maintenance problems.

Hi Jamie,

thanks for this snippet. Is there an actual version because I was not able to resolve import of packages.

Best regards

Christof

Hi Christof,

I believe you will be having issues with the import of the CopyProject class. Try changing:


import com.onresolve.jira.groovy.canned.admin.CopyProject

to:

import com.onresolve.scriptrunner.canned.jira.admin.CopyProject

Hi, I already use this class.

This is my code:

package examples

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.canned.jira.admin.CopyProject
import com.onresolve.jira.groovy.user.FieldBehaviours
//import com.onresolve.jira.groovy.user.FormField
import org.apache.log4j.Logger
import org.apache.log4j.Level

Logger.getLogger("com.onresolve").setLevel(Level.DEBUG)
def log = Logger.getLogger("com.onresolve.jira.groovy.MyCopyProjectScript")
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def destProjetKey = customFieldManager.getCustomFieldObjectByName("New Project KEY")
log.debug("New Project KEY: " + issue.getCustomFieldValue(destProjetKey))
def destProjectName = customFieldManager.getCustomFieldObjectByName("New Project Name")
log.debug("New Project Name: " + issue.getCustomFieldValue(destProjectName))

def srcProjectKey = 'TEMPKAMP'
log.debug("Source Project Name: " + srcProjectKey)

Thread executorThread = new Thread(new Runnable() {
void run() {
def copyProject = new CopyProject()
def inputs = [
(CopyProject.FIELD_SOURCE_PROJECT) : srcProjectKey,
(CopyProject.FIELD_TARGET_PROJECT) : issue.getCustomFieldValue(destProjetKey),
(CopyProject.FIELD_TARGET_PROJECT_NAME) : issue.getCustomFieldValue(destProjectName),
(CopyProject.FIELD_COPY_VERSIONS) : true,
(CopyProject.FIELD_COPY_COMPONENTS) : true,
(CopyProject.FIELD_COPY_ISSUES) : true,
//(CopyProject.FIELD_COPY_GREENHOPPER) : false,
(CopyProject.FIELD_COPY_DASH_AND_FILTERS) : false,
(CopyProject.FIELD_ORDER_BY) : "Rank",
]

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

def executingAdmin = ComponentAccessor.getUserManager().getUserByName(admins.first().name)
log.debug("Executing Admin: " + executingAdmin.getName())

ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(executingAdmin)
copyProject.doScript(inputs)
}
}
})
executorThread.run()

Hi Fyodor,

I haven't ever actually tried this, and I don't necessarily recommend running this script as a post-function, but you should be able to get the actual content of the script by clicking ''view source'' after having selected the script in the builtin script field.

Put the copied script into your WEB-INF/classes/com/onresolve/jira/groovy/canned/workflow/postfunctions folder and change the

package com.onresolve.jira.groovy.canned.admin

for

package com.onresolve.jira.groovy.canned.workflow.postfunctions

Might require some hacking around but it's a start, depending on how familiar you are with groovy coding:

You can find a bit more info on how this would work here:https://jamieechlin.atlassian.net/wiki/display/GRV/Built-In+Scripts#Built-InScripts-Workflowfunctions-Post-functions

You can use the CLI Plugin for JIRA and it's JIRA post function to run cloneProject, createProject, or similar actions.

Hi all

Thank you for advices, I will try scenarios you offered.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

46 views 0 4
Read article

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