Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner: Automate cloning an issue with sub-tasks

Paul Martin August 1, 2022

Hello,

Is it possible to automate a cloning process (inc. all sub-tasks) when an issue is resolved?

  


Current Process:

On a daily basis my team work through an issue which has 14 sub-tasks associated to it. We currently have a template ticket which is cloned then the summary field is updated with todays date and work commences.

Desired outcome:

Once the existing issue is complete, a new clone is automatically generated with all sub-tasks ready to go. 


 

So far, I have been able to clone a task when a custom field is equal to a particular value after a resolution event.

cfValues['cfExample']?.value == 'Example'

I have also added an additional issue action to update the summary on the newly cloned issue to todays date

def SummaryName = String.format('%tF', java.time.LocalDateTime.now())
issue.summary = SummaryName

 

The issue is that the above only clones the parent task and does not clone any of the sub-tasks. How would I go about cloning the sub-tasks over to the newly created issue?

Thank you. 

1 answer

0 votes
Aditya Sastry
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.
August 2, 2022

Hi Paul,

How are you doing the cloning of tasks? Is it through listeners or through workflow post function? 

You can use the below piece of code in cloning the sub-tasks.

def subtasks = issues.subTaskObjects
if (subtasks) {
subtasks.each { subtask ->
def subtasktoClone = issueFactory.cloneIssueWithAllFields(subtask)
def subtaskcloned = issueManager.createIssueObject(reporter, subtasktoClone)
subtaskManager.createSubTaskIssueLink(newissue, subtaskcloned, newissue.reporter)

}

}

 Thanks,
Aditya

Paul Martin August 3, 2022

Many thanks for your response Aditya.

I'm looking to use listeners.

When I attempt to use the above it throws back some errors. You'll have to forgive me as I am new to Groovy script. I did try and resolve these by adding the below but it still throws back some errors due to undeclared variables. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.issue.IssueManager

def issueFactory = ComponentAccessor.getIssueFactory()
def subtaskmanager = ComponentAccessor.getSubTaskManager()
def issueManager = ComponentAccessor.getIssueManager()

def subtasks = issue.subTaskObjects
if (subtasks) {
subtasks.each { subtask ->
def subtasktoClone = issueFactory.cloneIssueWithAllFields(subtask)
def subtaskcloned = issueManager.createIssueObject(reporter, subtasktoClone)
subtaskmanager.createSubTaskIssueLink(newissue, subtaskcloned, newissue.reporter)

}

}
Aditya Sastry
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.
August 3, 2022

Hi Paul,

I've just shared a piece of code. Can you share your complete code and the error to look at it?

Thanks,
Aditya

Paul Martin August 4, 2022

So I'm using the following script and it executes successfully without an error however the sub-tasks are not being cloned. 


The cloning of sub-tasks should be based on the original parent ticket which was moved to complete. Is this code doing that, or is it based on the new cloned ticket which doesn't have any sub-tasks? 

 

import java.time.LocalDate
import com.atlassian.jira.component.ComponentAccessor

def SummaryName = LocalDate.now().plusDays(1).toString()

def subtaskManager = ComponentAccessor.getSubTaskManager()
def issueManager = ComponentAccessor.getIssueManager()
def issueFactory = ComponentAccessor.getIssueFactory()
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()}")
}
}

issue.summary = SummaryName

 

Suggest an answer

Log in or Sign up to answer