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.
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.errorCollectiondef issueResult = issueService.create(loggedInUser, validationResult)assert issueResult.valid : issueResult.errorCollection
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
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.
we got this far but are still getting errors:
import com.onresolve.scriptrunner.runner.util.UserMessageUtil//import com.atlassian.jira.ComponentManagerimport 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.IssueServiceimport com.atlassian.jira.issue.IssueInputParametersimport com.atlassian.jira.bc.issue.IssueService.IssueResultimport groovy.json.JsonSlurperimport groovy.json.*import org.apache.http.client.methods.*import org.apache.http.entity.*import org.apache.http.impl.client.*import org.apache.xpath.operations.Stringimport com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssueimport com.onresolve.scriptrunner.runner.ScriptRunnerImplimport 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.ComponentManagerimport com.atlassian.jira.project.Projectimport com.atlassian.jira.issue.Issueimport com.atlassian.jira.issue.IssueManagerimport com.atlassian.jira.issue.IssueInputParametersimport com.atlassian.jira.bc.issue.IssueServiceimport com.atlassian.jira.bc.issue.IssueService.CreateValidationResultimport com.atlassian.jira.bc.issue.IssueService.IssueResultimport com.atlassian.jira.issue.comments.CommentManagerimport com.atlassian.jira.issue.comments.Commentimport com.atlassian.jira.user.ApplicationUserimport com.atlassian.jira.issue.link.DefaultIssueLinkManagerimport com.atlassian.jira.issue.link.IssueLinkManagerimport 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 = issuelog.warn("issue: " + issue)//Get destination project keydef communityField = customFieldManager.getCustomFieldObject(communityFieldId)def destinationProjectKey = issue.getCustomFieldValue(communityField).toString()
def originalIssueKey = issueManager.getIssueByCurrentKey(issue.key)
//get destination project infodef destinationProjectObj = projectManager.getProjectObjByKeyIgnoreCase(destinationProjectKey.replace('Project: ',''))def destinationProjectId = destinationProjectObj.getId()def destinationProjectLead = destinationProjectObj.getProjectLead()def destinationProjectLeadObj = ComponentAccessor.getUserUtil().getUserByKey(destinationProjectLead.username)
//get current userdef currentUserObject = ComponentAccessor.getJiraAuthenticationContext().getUser()//def currentUser = currentUserObject.username
//TODO: get project lead of assigned communitydef 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 issuedef 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 fordef 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
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
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
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.
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();issueInputParameters.setProjectId(destinationProjectId).setIssueTypeId(subTaskIssueTypeId).setSummary(summary)//.setReporterId(reporterId)
It looks like you're new here. Sign in or register to get started.