Good Day,
I am trying to find a way to use scriptrunner for Jira Server to set the assignee of a scriptrunner built in script for creating a sub-task to the reporter of the parent issue. Some pointers would be very much appreciated.
Thank you
Hi
This might be overkill but hopefully useful.
This is set up as a class stored in the Scriptrunner script editor.
It called from a workflow post function and gets its parameters from the workflow step attributes.
import com.blah.Constantsimport com.blah.SubTaskHandlerimport org.apache.log4j.Loggerdef logit = Logger.getLogger("com.blah.logging")SubTaskHandler handler = new SubTaskHandler(issue)logit.info(handler)handler.createSubTasks()
Flag is set to prevent the sub tasks being created twice by the workflow. Sub tasks are labelled to they can be groups together for handling
package com.blah/*sub-task scriptdepends on transition properties being set"flag.field" = single select field to indicate step sub-tasks have been processed"subtask.label" = label to identify sub-tasks belongint to this transition"sub-tasks" = pipe | separated list of sub-task summaries*/import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.CustomFieldManagerimport com.atlassian.jira.issue.fields.CustomFieldimport org.apache.log4j.Loggerimport com.atlassian.jira.issue.MutableIssueimport com.atlassian.jira.issue.issuetype.IssueTypeimport com.atlassian.jira.bc.issue.comment.CommentServiceimport java.sql.Timestampimport com.atlassian.jira.issue.label.Labelimport com.atlassian.jira.issue.status.Statusimport com.opensymphony.workflow.loader.StepDescriptorimport com.atlassian.jira.issue.Issueimport com.atlassian.jira.user.ApplicationUserimport com.atlassian.jira.workflow.JiraWorkflowimport com.atlassian.jira.security.JiraAuthenticationContextimport com.atlassian.jira.project.Projectimport com.atlassian.jira.issue.IssueFactoryimport com.atlassian.jira.issue.IssueManagerimport com.atlassian.jira.config.SubTaskManagerimport com.atlassian.jira.config.ConstantsManagerimport com.atlassian.jira.user.util.UserUtilimport com.atlassian.jira.issue.fields.config.FieldConfigimport com.atlassian.jira.issue.customfields.option.Optionimport com.blah.Constantsclass SubTaskHandler {Logger logit = Logger.getLogger("com.blah.logging")CustomFieldManager customFieldManager //= ComponentAccessor.getCustomFieldManager()UserUtil userUtil //= ComponentAccessor.getUserUtil()ConstantsManager constantsManager //= ComponentAccessor.constantsManagerIssueManager issueManager //= ComponentAccessor.issueManagerIssueFactory issueFactory //= ComponentAccessor.issueFactorySubTaskManager subTaskManager //= ComponentAccessor.subTaskManagerJiraAuthenticationContext authenticationContext //= ComponentAccessor.jiraAuthenticationContextApplicationUser cwdUser //= authenticationContext.getLoggedInUser()JiraWorkflow workflow //= ComponentAccessor.getWorkflowManager().getWorkflow(issue) MutableIssue issue public SubTaskHandler(MutableIssue issue) { this.issue = issue logit = Logger.getLogger("com.cheil.eu.logging")customFieldManager = ComponentAccessor.getCustomFieldManager()userUtil = ComponentAccessor.getUserUtil()constantsManager = ComponentAccessor.constantsManagerissueManager = ComponentAccessor.issueManagerissueFactory = ComponentAccessor.issueFactorysubTaskManager = ComponentAccessor.subTaskManagerauthenticationContext = ComponentAccessor.jiraAuthenticationContext cwdUser = authenticationContext.getLoggedInUser() workflow = ComponentAccessor.getWorkflowManager().getWorkflow(issue) logit.info("SubTaskHandler") } public String toString() { return "SubTaskHandler " + cwdUser} public void createSubTasks() { logit.info("SubTaskHandler.createSubTasks")StepDescriptor destStepDescriptor = workflow.getLinkedStep(issue.getStatus())def currentAttributes = destStepDescriptor.getMetaAttributes()String FIELD = currentAttributes[Constants.FIELD] // get the flag field nameString LABEL = currentAttributes[Constants.LABEL] // get the sub-task label valuelogit.debug("" + Constants.FIELD + " : " + FIELD)logit.debug("" + Constants.LABEL + " : " + LABEL)def subTaskList = currentAttributes[Constants.SUBTASKS].toString().tokenize("|") // get the sub-task summary listlogit.debug("" + Constants.SUBTASKS + " : " + subTaskList)Collection<CustomField> done = customFieldManager.getCustomFieldObjectsByName(FIELD) // get current flag field valueCustomField flagField = done.first()def doneVal = issue.getCustomFieldValue(flagField)logit.info('done: ' + doneVal) // check is the step is not flagged as already processed (no value or not set to 'Y')if(doneVal == null || !doneVal == 'Y') {//You need a target projectProject targetProject = issue.getProjectObject()IssueType[] type = new IssueType[1]IssueType[] issueTypes = targetProject.getIssueTypes().toArray(type)IssueType issueTypeissueTypes.each() { IssueType iss = (IssueType)it if (iss.name == 'Sub-task') { issueType = iss }}logit.debug('sub-task issueType: ' + issueType) subTaskList.each(){ //You need a summary String issueSummary = it logit.debug("sub : " + it) //This creates an empty subtask Issue subtask = issueFactory.issue //Set the summary and the target project def errorText = "" subtask.setSummary(issueSummary) subtask.setDescription(issueSummary) subtask.setPriority(issue.getPriority()) subtask.setParentObject(issue) subtask.setReporter(issue.reporter) subtask.setProjectObject(targetProject) //subtask.assignee = issue.getAssignee() if (issue.dueDate) { subtask.setDueDate(new Timestamp(issue.dueDate.getTime())) } subtask.setLabels([new Label(null,null, LABEL)] as Set) // def issueType = constantsManager.getIssueType("Sub-task") if (!issueType) { logit.info( "Your subtask type does not exist, you need a subtask type to create a subtask!") return "Your subtask type does not exist, you need a subtask type to create a subtask!" } subtask.setIssueTypeId(issueType.getId()) //You need to parse all of this as a subtask params Map<String, Object> newIssueParams = ["issue": subtask] as Map<String, Object> issueManager.createIssueObject(cwdUser, newIssueParams) //After the issue is created, you need to create the link. subTaskManager.createSubTaskIssueLink(issue, subtask, cwdUser) //labelManager.addLabel(cwdUser, subtask.getId(), FIELD, false)} // when sub-tasks completed update issue flag field for statuaFieldConfig fieldConfig = flagField.getRelevantConfig(issue)Option value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Y' }issue.setCustomFieldValue(flagField, value)}}}
It looks like you're new here. Sign in or register to get started.