Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Cloning an issue with Scriptrunner

Tayyab Bashir
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.
September 9, 2017

Hi, 

I am running this code in the console of scriptrunner for testing puropses. 
The clone operation clones a given issue and creates a clone for it? Right? 

I am looking into this API, and using validateClone() and clone() operations as in the code below.
It retuns an AsyncronousTaskResult stamp but no issue is created in jira? 
Am I wrong in assuming that this would clone and create the issue? 

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()
def issueService = ComponentAccessor.getIssueService()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueObject = issueManager.getIssueObject("BAT-11")

//Cloning issue
def customFieldObjects = customFieldManager.getCustomFieldObjects(issueObject)
def array = customFieldObjects.toArray()

HashMap map = new HashMap();
map.put(array[0], false)
map.put(array[1], false)
map.put(array[2], false)
map.put(array[3], false)

def validateClone = issueService.validateClone(user, issueObject, issueObject.summary, false, false, false, map)
def clone = issueService.clone(user, validateClone)

 
Could you please clarify if I am misunderstanding something? 

Regards,

Tayyab

1 answer

Suggest an answer

Log in or Sign up to answer
3 votes
Thanos Batagiannis _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.
September 11, 2017

Hi Tayyab,

If you want to follw the Jira API then the following script will do the trick 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFactory

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.getIssueManager()
def issueObject = issueManager.getIssueByCurrentKey("ATG-1")
def clonedIssue = ComponentAccessor.getComponent(IssueFactory).cloneIssue(issueObject)

Map<String,Object> newIssueParams = ["issue" : clonedIssue] as Map<String,Object>
issueManager.createIssueObject(user, newIssueParams)
log.info "Issue with summary ${clonedIssue.key} created"

But this will not clone custom fields. So if you want to create an exact clone of the original issue you can use the mthod thet the Clone Issue Listener does and will clone everythinig. For example in your script console run something like 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.Project
import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue

def issueManager = ComponentAccessor.getIssueManager()
def originalIssueKey = issueManager.getIssueByCurrentKey("ATG-1")
def destinationProjectKey = ComponentAccessor.projectManager.getProjectObjByKey("ATG")

cloneIssue(originalIssueKey, destinationProjectKey)

void cloneIssue (MutableIssue issue, Project project) {
def params = [
issue : issue,
(CloneIssue.FIELD_TARGET_PROJECT) : project.key,
(CloneIssue.FIELD_SELECTED_FIELDS) : null, //clone all the fields
] as Map<String, Object>

new CloneIssue().doScript(params)
}

kind regards, Thanos

Tayyab Bashir
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.
September 11, 2017

Hi Thanos,

Thansks for the code. It works fine. 

Could you tell me why wasn't clone() and validateClone() functionsn weren't working from Issue Service. 
And can I create the same clone using create methods from the issue service api? 

Is using Issue Service API for clone opeation not correct?

Thanos Batagiannis _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.
September 12, 2017

Hi Tayyab,

Yes you are right (I was working with the 7.0.0 API), in 7.1.1 they introduced the clone. The only bit that is wrong with your script is that the second value of the map should be an Optional and not a boolean. 

Still tho I can see some limitations, like you cannot clone the issue into a new project or with a different issue type.

But if you want to clone an issue in the same project and with the same issue type that seems to be a good option.

So the script should be something like 

import com.atlassian.jira.component.ComponentAccessor

def issueService = ComponentAccessor.issueService

// user can also be null, indicating an anonymous user
def user = ComponentAccessor.userManager.getUserByKey("admin")
def issue = ComponentAccessor.issueManager.getIssueByCurrentKey("ATG-1")

def textFieldCF = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("TextFieldA")
def map = [:]

map.put(textFieldCF, Optional.of(false))

def validationResult = issueService.validateClone(user, issue, "Cloned from ${issue.key}".toString(), false, false, false, map)

if (! validationResult.isValid()) {
log.error validationResult.errorCollection.errorMessages
 return
}

issueService.clone(user, validationResult)

regards, Thanos. 

Vineela Durbha May 20, 2019

@Thanos Batagiannis _Adaptavist_ 

Thanks for your code.

Is their anyway we can set the field values during this clone? If any such please help me as it would be of great help.

I am actually trying to clone an issue with the change in the values of custom fields during cloning.

Like Andrei Chiriac likes this
Putri Nur Dayana Kamarudin September 22, 2022

Hi Vineela and to anyone looking for a solution to set a field after cloning, use issueFactory instead. A snippet of the code would be like below to set a summary and select list :

def issue = issueManager.getIssueByCurrentKey(issue.key)
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("select list")[0]
def cfVal = issue.getCustomFieldValue(selectListCustomField) as String[]
def cfConfig = cf.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {it.value == 'selection 1'}

def cloneIssueFactory = issueFactory.cloneIssueWithAllFields(issue)
cloneIssueFactory.setCreated( new Date(System.currentTimeMillis()).toTimestamp() )

cloneIssueFactory.setSummary("${issue.summary}")
cloneIssueFactory.setCustomFieldValue(cf, value)

def newIssueParams = ['issue': cloneIssueFactory] as Map<String, Object>
issueManager.createIssueObject(user, newIssueParams)

log.warn "Issue ${cloneIssueFactory?.key} cloned"

 Hope this helps!

TAGS
AUG Leaders

Atlassian Community Events