Forums

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

Scriptrunner Postfunction: clone subtask

Duane Cronkite
Contributor
July 7, 2023

Is there a way to use postfunctions to clone a subtask when a subtask transitions to a certain status.

For some reason scriptrunner doesn't allow for the selection of subtasks when using the clone postfunction.

Use Case:

When a subtask enters a failed status, we want a bug subtask created under the same parent.

2 answers

1 accepted

1 vote
Answer accepted
Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 7, 2023

Hi, @Duane Cronkite 
Maybe Jira Automation? :)

Ok, ok, just joking, I remember, you use server )

As for your answer, it's not a simple task, because script needs to know, what fields must be set. Simple version can create basic sub-task with needed issue-type, summary, description and assignee, but copying other custom fields is very tricky case.

Duane Cronkite
Contributor
July 7, 2023

lol!

would it be easy if we only copied over:

  • Description
  • Summary (nice to have would be BUG: preface)
  • reporter = current user
Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 7, 2023

Well, I think, it will help

/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.ConstantsManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser

IssueManager issueManager = ComponentAccessor.getIssueManager()
ConstantsManager constantsManager = ComponentAccessor.getConstantsManager()

MutableIssue issue = issue as MutableIssue
MutableIssue parentIssue = issueManager.getIssueObject(issue.key)
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// Set exact name of sub task you're trying to create
String issueType = "Sub-Bug"

MutableIssue newIssue = ComponentAccessor.getIssueFactory().getIssue()
newIssue.setProjectObject(issue.getProjectObject())
newIssue.setIssueType(constantsManager.allIssueTypeObjects.findByName(issueType))
newIssue.setSummary("BUG: preface")
newIssue.setDescription(issue.description)
newIssue.setAssignee(issue.assignee)
newIssue.setReporter(user)
newIssue.setLabels(null)
newIssue.setComponent(null)
newIssue.setPriorityId('3')
newIssue.setDueDate(null)
ComponentAccessor.issueManager.createIssueObject(user, newIssue)

ComponentAccessor.subTaskManager.createSubTaskIssueLink(parentIssue, newIssue, user)
Duane Cronkite
Contributor
July 7, 2023

okay it looked like it successfully created a bug but the bug is somehow being listed as a subtask of a subtask. It is not appearing as a subtask on the parent.

TP-168 test 2 is a subtask which is strange that its appearing like its the bug subtask parent.

image.png

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 7, 2023

It's crazy. I'll check it. In script it must have been linked to parent.

Duane Cronkite
Contributor
July 7, 2023

and for the summary what I meant was Bug: (clone summary of the source ticket).

So Bug: is the preface before the source summary if that makes sense.

I didn't check if description copied over yet. 

 

and I appreciate all the help!

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 7, 2023

Found problem. 

This is working (at least on my instance):

/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/

package Examples.Postfunctions

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.ConstantsManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser

IssueManager issueManager = ComponentAccessor.getIssueManager()
ConstantsManager constantsManager = ComponentAccessor.getConstantsManager()

MutableIssue issue = issue as MutableIssue
MutableIssue parentIssue = issueManager.getIssueObject(issue.parentObject.key)
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// Set exact name of sub task you're trying to create
String issueType = "Sub-Bug"

MutableIssue newIssue = ComponentAccessor.getIssueFactory().getIssue()
newIssue.setProjectObject(issue.getProjectObject())
newIssue.setIssueType(constantsManager.allIssueTypeObjects.findByName(issueType))
newIssue.setSummary("BUG: ${issue.summary}")
newIssue.setDescription(issue.description)
newIssue.setAssignee(issue.assignee)
newIssue.setReporter(user)
newIssue.setLabels(null)
newIssue.setComponent(null)
newIssue.setPriorityId('3')
newIssue.setDueDate(null)
ComponentAccessor.issueManager.createIssueObject(user, newIssue)

ComponentAccessor.subTaskManager.createSubTaskIssueLink(parentIssue, newIssue, user)
Duane Cronkite
Contributor
July 7, 2023

once again you made my day! I'll send you a connect on Linkedin! A++++

Like Evgenii likes this
0 votes
Duane Cronkite
Contributor
July 25, 2023

@Evgenii : Is there a way to add a "link" between the source and created subtask to the code.

"Relates to" link will work.

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 25, 2023

Hi, @Duane Cronkite 

It depends from your environment, IDs of link types can differ from mine.

You can use such code (adopted to your environment, of course):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser

// Set user, that will link issues
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
// Set inward or outward link
Long sequence = 1L
// Set link type ID
Long issueLinkTypeId = 10000L

ComponentAccessor.issueLinkManager.createIssueLink(fromIssue.id, toIssue.id, issueLinkTypeId, sequence, user)
Duane Cronkite
Contributor
August 15, 2023

received this error: 

The script could not be compiled:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script668.groovy: 45: The current scope already contains a variable of the name user
 @ line 45, column 17.
   ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
                   ^

 I added this link code directly to the bottom of the other code above. I tried removing the user piece etc but couldn't figure out how to get it to work.

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 16, 2023

Hi, @Duane Cronkite 

What's the sense in it? This are 2 subtasks for one issue.

Anyway, it can be made with something like this:

/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.ConstantsManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser

IssueManager issueManager = ComponentAccessor.getIssueManager()
ConstantsManager constantsManager = ComponentAccessor.getConstantsManager()

MutableIssue issue = issue as MutableIssue
MutableIssue parentIssue = issueManager.getIssueObject(issue.parentObject.key)
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// Set exact name of sub task you're trying to create
String issueType = "Sub-Bug"
// Set inward or outward link
Long sequence = 1L
// Set link type ID
Long issueLinkTypeId = 10000L

MutableIssue newIssue = ComponentAccessor.getIssueFactory().getIssue()
newIssue.setProjectObject(issue.getProjectObject())
newIssue.setIssueType(constantsManager.allIssueTypeObjects.findByName(issueType))
newIssue.setSummary("BUG: ${issue.summary}")
newIssue.setDescription(issue.description)
newIssue.setAssignee(issue.assignee)
newIssue.setReporter(user)
newIssue.setLabels(null)
newIssue.setComponent(null)
newIssue.setPriorityId('3')
newIssue.setDueDate(null)
ComponentAccessor.issueManager.createIssueObject(user, newIssue)

ComponentAccessor.subTaskManager.createSubTaskIssueLink(parentIssue, newIssue, user)
ComponentAccessor.issueLinkManager.createIssueLink(issue.id, newIssue.id, issueLinkTypeId, sequence, user)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events