Script runner post function to create/copy a project

fjodors
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 2, 2014

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

0 votes
Answer accepted
Ubisoft
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 2, 2014

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
JamieA
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 2, 2014

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.

Christof Hurst May 9, 2018

Hi Jamie,

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

Best regards

Christof

James Elder May 21, 2018

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

Christof Hurst May 22, 2018

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()
0 votes
fjodors
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 19, 2014

Hi all

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

0 votes
Bob Swift OSS (Bob Swift Atlassian Apps)
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 2, 2014

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

0 votes
Martin Poirier January 2, 2014

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events