Clone subtasks from other issue (using Groovy)

Andrew Striletskyi June 21, 2018

Hello everyone. How to clone subtasks from other issue? (using Groovy)  

2 answers

1 accepted

1 vote
Answer accepted
Mark Markov
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.
June 21, 2018
Andrew Striletskyi June 22, 2018

Yes , I know about. I have a situation when I want to create another issue. For example 

 

def artifactIssue = issueManager.getIssueObject("TEST-2999")

def newArtifactIssue = issueFactory.cloneIssue(artifactIssue)

 

But It's not cloning an issue

Mark Markov
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.
June 22, 2018

clone issue just clone issue object, you need to create it after.

And for some reason it dont clone info about parent object, so you need to do that too.

Here the script:

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def subtaskManager = ComponentAccessor.getSubTaskManager()
def issue = issueManager.getIssueObject("TEST-1")
log.error("SOURCE ISSUE KEY: ${issue.getKey()}")

def toclone = issueFactory.cloneIssueWithAllFields(issue)
def cloned = issueManager.createIssueObject(issue.reporter, toclone)
def parent = issueManager.getIssueObject(subtaskManager.getParentIssueId(issue))
subtaskManager.createSubTaskIssueLink(parent, cloned, issue.reporter)
log.error("CLONED ISSUE KEY: ${cloned.getKey()}")
Andrew Striletskyi June 25, 2018

Yes , It's OK for me. But last question: how to clone all subtasks of this issue?

Mark Markov
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.
June 25, 2018

Like this

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def subtaskManager = ComponentAccessor.getSubTaskManager()
def issue = issueManager.getIssueObject("IM-474172") // parent issue
log.error("SOURCE ISSUE KEY: ${issue.getKey()}")
def subtasks = issue.getSubTaskObjects()
if (subtasks)
{
subtasks.each {subtask ->
def toclone = issueFactory.cloneIssueWithAllFields(subtask)
def cloned = issueManager.createIssueObject(issue.reporter, toclone)
subtaskManager.createSubTaskIssueLink(issue, cloned, issue.reporter)
log.error("CLONED ISSUE KEY: ${cloned.getKey()}")
}
}
Like # people like this
Andrew Striletskyi June 25, 2018

Oh, thank you!

Mark Markov
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.
June 25, 2018

You re welcome!

If it helps you, please mark answer as accepted :)

Vineela Durbha
Contributor
May 20, 2019

@Mark Markov 

I have created a new ticket using the code above and it got created successfully. But the issue is that new request is getting created with the created date of parent ticket. Is there any way the created date can be changed to current date?

0 votes
Michael Spoonauer
Contributor
May 15, 2020

I use the above code as well.  But is there a way to return the sub-tasks in the order in which they're placed in the to-be-cloned issue?  Because for us, order matters in terms of the order in which you execute sub-tasks, and that order is lost when cloning sub-tasks.

It looks like they're returned to the caller in the order in which they were created, not the order in which they were placed.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events