Using script runner custom field, I’m trying to create a specific subtask type if such doesn’t exists yet for the parent issue and if another custom filed is set with a value.
I’m getting into an endless run where it subtask are getting created endlessly.
Seems that there is a recursive event due to some multi-threading behavior that I’m not aware of.
My script is bellow, could you give it a look and let me know what I’m doing wrong?
import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.ComponentManager import com.atlassian.jira.bc.issue.IssueService import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.util.DefaultIssueChangeHolder import com.atlassian.jira.issue.util.IssueChangeHolder import com.atlassian.jira.issue.ModifiedValue //import java.util.Map.Entry import com.atlassian.jira.config.SubTaskManager import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.event.type.EventDispatchOption import com.atlassian.jira.issue.Issue //import com.atlassian.jira.security.Permissions import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.util.ImportUtils; //def projectManager = ComponentManager.getInstance().getProjectManager(); SubTaskManager subTaskManager = ComponentManager.getInstance().getSubTaskManager(); if(subTaskManager.subTasksEnabled){ def mutableIssue = ComponentManager.getInstance().getIssueManager().getIssueObject(issue.id); if (mutableIssue.issueTypeObject.name == "Story"){ def TaskType; def AuserName ="ron.grinfeld"; def issueManager = ComponentManager.getInstance().getIssueManager(); def issueService = ComponentManager.getInstance().getIssueService(); def userManager = ComponentAccessor.getUserManager(); def AdUser = userManager.getUserObject(AuserName); def issueLinkManager = ComponentManager.getInstance().getIssueLinkManager(); def issueFactory = ComponentAccessor.getIssueFactory(); def userUtil= componentManager.getUserUtil(); def authenticationContext = ComponentManager.getInstance().getJiraAuthenticationContext(); def Collection subTasks; def mutableIssue1; def cfDocReq = componentManager.getCustomFieldManager().getCustomFieldObjectByName("Documentation required"); if(cfDocReq != null){ def Docreq_Val = mutableIssue.getCustomFieldValue(cfDocReq); subTasks = mutableIssue.getSubTaskObjects() if(Docreq_Val == null){ // documentation not requiered - delete any exisiting documentation subtasks if (!subTasks.empty){ subTasks.each { mutableIssue1 = ComponentManager.getInstance().getIssueManager().getIssueObject(it.id); TaskType = mutableIssue1.issueTypeObject.name; if (TaskType.equals("Documentation")) issueManager.deleteIssue(AdUser, mutableIssue1,EventDispatchOption.ISSUE_DELETED, false); }// end subTasks.each }// end if (!subTasks.empty) } // end if(Docreq_Val==null) else{ // doccumentation required subTasks = mutableIssue.getSubTaskObjects(); def Docfound=0; // documentation requiered iterate through the subtasks and create a documentation task if one doesnt exist if(subTasks.size()) for(i in 0..subTasks.size()-1){ mutableIssue1 = subTasks[i]; if(mutableIssue1) if(mutableIssue1.issueTypeObject){ TaskType = mutableIssue1.issueTypeObject.name; if (TaskType.equals("Documentation")) Docfound=1; } i++; } if(!Docfound){ // didnt find and documentation task - create one def usera = userManager.getUserObject("Rachelle.Cohen"); def issueObject = issueFactory.getIssue(); issueObject.setProject(mutableIssue.getProject()) def newIssueType = ComponentAccessor.issueTypeSchemeManager.getIssueTypesForProject(issue.projectObject).find{it.name=="Documentation"} issueObject.setIssueTypeId(newIssueType.id); issueObject.setProject(mutableIssue.getProject()); issueObject.setParentId(mutableIssue.getId()); // set subtask attributes issueObject.setSummary("Needs to be documented !!!"); issueObject.setAssignee(usera); issueObject.setDescription(mutableIssue.getDescription().toString()); def subTask = issueManager.createIssue(authenticationContext.getLoggedInUser(), issueObject); subTaskManager.createSubTaskIssueLink(mutableIssue.getGenericValue(), subTask, AdUser); issueLinkManager.createIssueLink(mutableIssue.getId(),subTask.id,10100,1,AdUser); // Update search indexes ImportUtils.setIndexIssues(true); ComponentManager.getInstance().getIndexManager().reIndex(subTask); ImportUtils.setIndexIssues(false); subTask.store(); mutableIssue.store(); }// end if(!Docfound) } // end else }// end if(cfDocReq != null) } // end if (mutableIssue.issueTypeObject.name == "Story") } // end if(subTaskManager.subTasksEnabled) return null;
Community moderators have prevented the ability to post new answers.
You didn't say how you were running this, but the problem could be:
def mutableIssue = ComponentManager.getInstance().getIssueManager().getIssueObject(issue.id);
You should use the issue given to you in the script binding, eg
def mutableIssue = issue
(although that's redundant, you could just replace mutableIssue with issue).
The problem might be that it doesn't "see" the subtasks unless the transaction is finished, as you are looking up the issue from the db, so it endless thinks there are no matching subtasks. When you modify it by eg adding a link, it's kicked off again (I'm guessing this is a listener?).
I'm using this script in a script runner custom field and not a workflow, so the issue is given. How can I tackle the fact that it doesn't "see" the sub task? Basically I'm trying to have a Yes/No filed that based on its value a "Documentation" sub-task will be added/removed.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You just can't update the current issue in a field... you need a listener.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
OK, so on the same topic, how do I set a custom list field to a specific list option value ? Ii have a field named DocReq which have the values 'None' (the default system one) and "Required"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.