Cloning an issue with Scriptrunner

Tayyab Bashir Community Champion Sep 09, 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

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 Community Champion Sep 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?

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. 

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 Thursday 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...

106 views 0 5
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