It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Cloning an issue with Scriptrunner

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

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. 

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Opsgenie

DevOps Just Got a Whole Lot Easier with Opsgenie and AWS CloudFormation Registry and CLI

...esponders": [{ "type": "team", "name": { "Fn::GetAtt": [ "NemoSearchParty", "Name" ] } }], "Enabled": true }, "DependsOn": [ "TeamA" ] }, "C...

890 views 0 10
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you