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

Conor McGmail July 26, 2016

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

2 votes
Thanos Batagiannis _Adaptavist_
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.
July 28, 2016

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 

Conor McGmail July 28, 2016

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'"}}}'
		}
Thanos Batagiannis _Adaptavist_
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 1, 2016

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 test system first.

regards

Thanos 

Thanos Batagiannis _Adaptavist_
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.
September 28, 2016

UPDATE:

Link to the updated documentation After Create actions

Andreas Schad October 11, 2016

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

Thanos Batagiannis _Adaptavist_
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.
October 11, 2016

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

Andreas Schad October 11, 2016

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!

 

Thanos Batagiannis _Adaptavist_
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.
October 11, 2016

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

Artemy Matvienko
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.
February 22, 2018

Logs don't seem to work within the doAfterCreate closure (ScriptRunner 5.1.6). Is there any way to output values into the Execution Information logs on the workflow transition page?

Suggest an answer

Log in or Sign up to answer