Auto create subtasks based on transitions

Joey Klein April 28, 2020

I have three statuses in a workflow with 6 subtasks each.  i want to create the subtasks upon transition.  i found this https://library.adaptavist.com/entity/create-sub-tasks-when-an-issue-is-created but its marked as cloud.  will this work on server?  my plan is to add the script separately to each transition.  

3 answers

1 accepted

0 votes
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 3, 2020

There's a lot of code there, I'm struggling to work through what it is trying to do.

If I were to cut it down to the bare bones though, all you really need in a post-function is below.  You don't need to set the status, that happens automatically on creation, and you should be able to add setDescription("") to this as well.

def issueInputParameters = issueService.newIssueInputParameters().with {
setProjectId(issue.projectObject.id)
setIssueTypeId(subTaskIssueType.id)
setReporterId(reporter.key)
setSummary(summary)

}

def validationResult = issueService.validateSubTaskCreate(loggedInUser, issue.id, issueInputParameters)
assert validationResult.valid : validationResult.errorCollection

def issueResult = issueService.create(loggedInUser, validationResult)
assert issueResult.valid : issueResult.errorCollection
Joey Klein May 4, 2020

I'm close.... any thoughts on why this isnt working?

 

 

import com.atlassian.jira.component.ComponentAccessor

def issueInputParameters = issueService.newIssueInputParameters().with

{
setProjectId(issue.projectObject.id)
setIssueTypeId(subTaskIssueType.id)
setReporterId(reporter.key)
setsummary('SPS Project team recruitment', 'SPS Project team introductory meeting', 'Location selection', 'Introductory Location meeting', 'SPS Project team review of interviews')
}

def validationResult = issueService.validateSubTaskCreate(loggedInUser, issue.id, issueInputParameters)
assert validationResult.valid : validationResult.errorCollection

def issueResult = issueService.create(loggedInUser, validationResult)
assert issueResult.valid : issueResult.errorCollection

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 4, 2020

Er, you can't set five summaries for a single issue. 

You need to look through this 5 times, changing the summary on each iteration

Joey Klein May 4, 2020

in the original script above (with all the extra stuff) we defined a list of summaries and that worked.  trying to figure out what i can cut out of that one to make it work but NOT have it run for epics. 

 

def listOfsummaries = ['Subtask summary 1', 'Subtask summary 2'] // The summaries to use for
def issueType = "Sub-task" // The Sub Task Issue Type to Use

// Get the sub task issue type to use
//def issueTypeId = issueTypes.find { it.subtask && it.name == issueType }?.id
//assert issueTypeId: "No subtasks issue type found called '${issueType}'"

// Get the project to create the subtask in
//def project = (issue.fields as Map).project

listOfsummaries.forEach { summary ->
// Create the subtask

IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
issueInputParameters.setProjectId(destinationProjectId)
.setIssueTypeId(subTaskIssueTypeId)
.setSummary(summary)
//.setReporterId(reporterId)

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 4, 2020

Yes, your original script had a loop in it, which you have re-implemented here, so it'll go through several creations.

But you've commented out all the stuff that works out other parameters you need in the create.

To avoid this running for Epics, look at the issue type for the current issue first, and exit if it's Epic

Joey Klein May 4, 2020

ok thanks. I'll keep plugging

0 votes
Joey Klein April 30, 2020

we got this far but are still getting errors:

 

import com.onresolve.scriptrunner.runner.util.UserMessageUtil
//import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import groovy.json.JsonSlurper
import groovy.json.*
import org.apache.http.client.methods.*
import org.apache.http.entity.*
import org.apache.http.impl.client.*
import org.apache.xpath.operations.String
import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.atlassian.jira.component.ComponentAccessor
//import com.atlassian.jira.ComponentManager - deprecated https://scriptrunner.adaptavist.com/5.4.49-jira8/jira/releases/UpgradingToJira8.html#_componentmanager_has_been_moved
//import com.atlassian.jira.component.pico.ComponentManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.CreateValidationResult
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.link.DefaultIssueLinkManager
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.event.type.EventDispatchOption

def subTaskIssueTypeId = 5
//get current user
//def currentUserObject = ComponentAccessor.getJiraAuthenticationContext().getUser()
//def currentUser = currentUserObject.username
//Set CommentManager object
//def commentManager = ComponentAccessor.getCommentManager()
//Create IssueManager object
//def issueManager = ComponentAccessor.getIssueManager()
//Create Custom Field Manager object
//def customFieldManager = ComponentAccessor.getCustomFieldManager()
//Create Issue Service Manager Object
//IssueService issueService = ComponentAccessor.getIssueService();
//def projectManager = ComponentAccessor.getProjectManager()
//def defaultStatusId = "1" //Open
//set issue variable
//Issue issue = issue;
def communityFieldId = "customfield_14000" //Community
//def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def issueService = ComponentAccessor.getIssueService()
def projectManager = ComponentAccessor.getProjectManager()
def CommentManager commentMgr = ComponentAccessor.getCommentManager();
//def issue = ComponentAccessor.getIssueManager().getIssueObject(parentId)
//Issue issue = event.getIssue()
//Issue issue = issue
log.warn("issue: " + issue)
//Get destination project key
def communityField = customFieldManager.getCustomFieldObject(communityFieldId)
def destinationProjectKey = issue.getCustomFieldValue(communityField).toString()

def originalIssueKey = issueManager.getIssueByCurrentKey(issue.key)


//get destination project info
def destinationProjectObj = projectManager.getProjectObjByKeyIgnoreCase(destinationProjectKey.replace('Project: ',''))
def destinationProjectId = destinationProjectObj.getId()
def destinationProjectLead = destinationProjectObj.getProjectLead()
def destinationProjectLeadObj = ComponentAccessor.getUserUtil().getUserByKey(destinationProjectLead.username)


//get current user
def currentUserObject = ComponentAccessor.getJiraAuthenticationContext().getUser()
//def currentUser = currentUserObject.username

//TODO: get project lead of assigned community
def reporterId = issue.getReporterId()

//If issue is a subtask, script is not executed
//if (issue.fields.issuetype.subtask) {
// return
//}
log.warn("issue type: " + issue.issueType.name)
if(issue.issueType.name == "Subtask"){

return


}
log.warn("Current User: " + currentUser)
log.warn("Source Issue Key: " + originalIssueKey)
log.warn("Source Issue Reporter: " + reporterId)
log.warn("Destination Project Key: " + destinationProjectKey)
log.warn("Destination Project Id: " + destinationProjectId.toString())

//get destination project info

// New created issue
def issueKey = issue.key

// Get the issue
//def issueResp = get("/rest/api/2/issue/${issueKey}").asObject(Map)
//assert issueResp.status == 200
//def issue = issueResp.body as Map

// Get the issue types for the instance
//def typeResp = get('/rest/api/2/issuetype').asObject(List)
//assert typeResp.status == 200
//def issueTypes = typeResp.body as List<Map>

def listOfsummaries = ['Subtask summary 1', 'Subtask summary 2'] // The summaries to use for
def issueType = "Sub-task" // The Sub Task Issue Type to Use

// Get the sub task issue type to use
//def issueTypeId = issueTypes.find { it.subtask && it.name == issueType }?.id
//assert issueTypeId: "No subtasks issue type found called '${issueType}'"

// Get the project to create the subtask in
//def project = (issue.fields as Map).project

listOfsummaries.forEach { summary ->
// Create the subtask

IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
issueInputParameters.setProjectId(destinationProjectId)
.setIssueTypeId(subTaskIssueTypeId)
.setSummary(summary)
//.setReporterId(reporterId)
.setDescription("teset")
.setStatusId(defaultStatusId)
//.setAssigneeId(destinationProjectLead.username)
//.addCustomFieldValue(timeCategoryFieldId, defaultTimeCategory)
//.addCustomFieldValue(epicNameFieldId, currentIssueSummary)
//.addCustomFieldValue(parentLinkFieldId, parentLinkDefaultValue)

IssueService.UpdateValidationResult updateValidationResult = issueService.validateUpdate(currentUserObject, issue.getId(), issueInputParameters);
if(updateValidationResult.isValid()) {
IssueResult updateResult = issueService.update(currentUserObject, updateValidationResult);

}

// Get and validate the newly created subtask

}

0 votes
Narendra Kumar April 28, 2020

Hi @Joey Klein  yes script runner add-on is available for Jira cloud as well for Jira server version.

You can write separate script to create sub-task at particular transition for your workflow.

You can search more about it on https://marketplace.atlassian.com/apps/6820/scriptrunner-for-jira?hosting=cloud&tab=overview

Thanks!

Best,

Narendra

Joey Klein April 29, 2020

Thanks @Narendra Kumar , appreciate the fast response.  I have the app for server.  I was referring specifically to the script i pasted which says its for cloud.  

Like Narendra Kumar likes this

Suggest an answer

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

Atlassian Community Events