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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,463,375
Community Members
 
Community Events
176
Community Groups

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.

Like Andrei Chiriac likes this

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!

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events