scriptrunner - clone and link additional action to clone all subtasks too

Hi,

Is there a way to have the scriptrunner plugin clone all the subtasks when using "clone issue and link" post-function?

Regards

Conor

 

1 answer

Hi Conor,

It is possible, writing your own script in the Additional Issue actions.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import groovy.transform.Field
@Field ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
@Field MutableIssue cloneIssue = issue
Issue originalIssue = sourceIssue

def subtasks = originalIssue.getSubTaskObjects()
// if there are no subtasks to clone, do nothing
if (!subtasks)
    return

doAfterCreate = { 
	subtasks.each {subtask ->
    	def summary = subtask.getSummary()
    	def assignee = subtask.getAssignee()
    	def descr = subtask.getDescription()
    	def textFieldA = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("TextFieldA")
    	def textFieldAValue = subtask.getCustomFieldValue(textFieldA)
    	createSubTask(summary, assignee, descr, textFieldAValue)
	}
}

def createSubTask(String summary, ApplicationUser assignee, String description, def customFieldValue) {
    def issueFactory = ComponentAccessor.getIssueFactory()
    def constantManager = ComponentAccessor.getConstantsManager()
    def issueManager = ComponentAccessor.getIssueManager()
    def subTaskManager = ComponentAccessor.getSubTaskManager()
    def textFieldA = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("TextFieldA")
    MutableIssue newSubTask = issueFactory.getIssue()
    newSubTask.setSummary(summary)
    newSubTask.setAssignee(assignee)
    newSubTask.setDescription(description)
    newSubTask.setCustomFieldValue(textFieldA, customFieldValue)
    newSubTask.setParentObject(cloneIssue)
    newSubTask.setProjectObject(cloneIssue.getProjectObject())
    newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find {
        it.getName() == "Sub-task"
    }.id)
    Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
    issueManager.createIssueObject(user, newIssueParams)
    subTaskManager.createSubTaskIssueLink(cloneIssue, newSubTask, user)
    log.info "Subtask ${newSubTask.getKey()} created"
}

 

regards,

Thanos 

Hi Thanos, this is what I have in the Additional issue actions so far, but I'm struggling. Could you please assist? many thanks

 

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger

# Get subtasks of ORIGINAL parent issue 
def subTaskList = issue.getSubTaskObjects()

// Iterate through the subtasks
	for (subTask in subTaskList) {
        def subtaskAssignee = subTask.assignee.displayName
        def subtaskSummary = subTask.summary
		def subtaskDescription = subTask.description
		
		# How do I get the newly cloned parent issue to attach these new subtasks to?
		# Once I get this new parent issue can I run a REST API curl command like below to create the new subtasks for the newly cloned parent?
		# How do i get current username|password to run command
		
		curl -D- -u $USER:$PASSWORD -X POST -H "Content-Type: application/json"  https://ourjira/rest/api/2/issue --data '{"fields":{"project":{"key": "DEMO"},"parent":{"key": "'$NEW_PARENT'"},"summary": "'subtaskSummary'","description": "'$subtaskDescription'","issuetype":{"id": "44"},"assignee":{"name":"'$subtaskAssignee'"}}}'
		}

Hi Conor,

Sorry I was not clear enough and just realised that the way is not documented. Therefore I edited my answer above adding a script. Is not thoroughly tested therefore recommend you to try it in a production system first.

regards

Thanos 

UPDATE:

Link to the updated documentation After Create actions

Hi guys,

I tried this script but I do get an error notification which says "Property 'newSubTask' not found". Any suggestions about the reason and solutions?

regards

Andy

Hi Andreas,

JIRA and SR version ? Even though I will take an educated guess and i will say that what cause this issue is

log.info "Subtask ${newSubTask.getKey()} created"

so try to replace it with 

log.info "Subtask " + newSubTask.getKey() + " created"

please let me know if this does the trick

Hi Thanos,

thx for this quick reply! Yes, it solves the problem. I've got JIRA 5.2.7 and SR 2.1.15 at hand.

Upcoming error now: No such property 'sourceIssue'. Can you guess on that one, too?

Thanks!

 

Hi Andrea,

SR 2.1.15 is a very old version, please try ti update in a later one, I think the 3.1.4 is the latest free one but still I do not think (I will test it later) the doAfterCreate closure is available even in this version. I also doubt that the sourceIssue will be available but you can get it via transientVars.

Still you are not loosing anything to try (use the cloneIssue instead of the sourceIssue). 

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,755 views 11 18
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot