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

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
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,325 views 12 19
Read article

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