Using ScriptRunner, how do I prevent a story from being closed when sub-tasks are open?

Jeramy
Contributor
November 21, 2024

I have a script that I've added to the Close transition as a validator. 

 

Here's the script:

 

 

package Workflows.Validators

import com.onresolve.scriptrunner.runner.util.UserMessageUtil

import com.atlassian.jira.issue.Issue

import org.apache.log4j.Logger

import org.apache.log4j.Level

import Log****.*

import Log****.Helpers.*

Logger jiraLog = Logger.getLogger(this as String)

jiraLog.setLevel(Log****Locale.logLevel)

// Get the current issue

Issue parentIssue = issue

jiraLog.debug('Source Issue: ' + parentIssue.getKey())

if (parentIssue.getSubTaskObjects().size() > 0) {

    def subTasks = parentIssue.getSubTaskObjects()

    jiraLog.debug('Associated sub-tasks: ' + subTasks)

    def allSubTasksDone = subTasks.every { it.status.name in ['Closed'] }

    jiraLog.debug(allSubTasksDone)

    if (!allSubTasksDone) {

        jiraLog.debug('Got to the part where all sub-tasks are not closed.')

        UserMessageUtil.info('Cannot close issue ' + parentIssue + ' as it has open sub-tasks')

        return false

    }else {

        jiraLog.debug('All sub-tasks are closed.')

        return true

    }

}

if (parentIssue.getSubTaskObjects().size() == 0) {

    jiraLog.debug('No sub-tasks associated with issue: ' + parentIssue)

    return true

}
Can anyone help me figure out why the transition still set the status to Closed? When I close the issue with open sub-tasks, I get the Util message. So I know it's getting there, but the status still changes to closed.
Thank you.

2024-11-21_14-34-08.jpg

1 answer

1 accepted

4 votes
Answer accepted
Salih Tuç
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 21, 2024

Hi @Jeramy ,

If you are using Custom script validator, then you need to throw an InvalidInputException instead of returning false.

So, just change

return false

to

throw new InvalidInputException("...")

Jeramy
Contributor
November 22, 2024

@Salih Tuç - Perfect! That worked great. Thank you so much!

Suggest an answer

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

Atlassian Community Events