How to prevent closing the parent issue before closing all sub tasks in Jira script runner using Groovy script?

2 minute read

The developers sometimes forget to close their sub tasks, and someone closes the parent issue without checking the sub task status. You can prevent closing the parent issue unless all the sub tasks are closed by adding the validator to your workflow transition.

Steps:

  1. Go to your workflow.
  2. Select the diagram and edit the workflow.
  3. Select the close/done transition and choose the validator.
  4. Click “Add” and select the “Simple scripted validator [ScriptRunner]”.
  5. Enter the below groovy script under the condition section.
  6. Enter the error message, something like “Sub tasks should be closed before closing the parent issue”.
  7. Save the changes.

Groovy script:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor

// declare variable to assign the flag of sub task status
def IsAllSubTaskClosed = true;
// Get the subtask manager
def subTaskManager = ComponentAccessor.getSubTaskManager();
// Get the list of sub task objects from the current issue
Collection<Issue> subTasks = issue.getSubTaskObjects();

// Check whether the current issue is sub task. If yes then ignore proceed to further code.
if(issue.getIssueType().getName() == "Sub-task"){
    return true;
}

// Check for sub task(s) exists and enabled.
if(subTaskManager.subTasksEnabled && !subTasks.empty){
    // Loop through the sub tasks
    for(def subtask in subTasks) { 
        // If current task type is sub task
        if(subtask.issueType.name == "Sub-task"){
            // Check the sub task current status is done or not
            if(subtask.status.name != "Done"){
                // Set the sub task not closed status
                IsAllSubTaskClosed = false;
            }
        }
    }
}


return IsAllSubTaskClosed;

Leave a comment