scriptrunner validation if remaining estimate on all subtasks have been zeroed out

Carol Jones March 24, 2017

I'm attempting to write a scripted validator to only allow a parent ticket to transition if all subtasks have zero remaining time.  I thought the below would work and have tried several different variations of it.  But am at the point where I think it's time to ask for help smile  Anyone have experience putting something like this together already that could lend a helping hand?  TIA!

Collection subTasks = issue.getSubTaskObjects()
if (!subTasks.empty) {
    subTasks.each {
        if (it.getEstimate() != 0) {
            return false
        }
    }
}

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
JamieA
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.
March 28, 2017

@Tarun Sapra is right, but this is not a bug... return gets you to the next loop of that closure.

A more groovy way to do it is:

issue.getSubTaskObjects().every {
    it.getEstimate() == 0
}

that should be all you need.

Have a look at http://groovy-lang.org/groovy-dev-kit.html#List-Filtering

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 28, 2017

Thanks @Jamie Echlin [Adaptavist] for the precise code, I meant this closure feature is a bit "non intuitive" (bug is an stronger word) smile

JamieA
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.
March 28, 2017

yeah I agree it's a gotcha... I think my point is if you find you need to return from simple code, maybe there is a better GDK method, like .every, .any, .sum, .countBy etc etc.

Carol Jones March 29, 2017

Thanks!!

0 votes
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 27, 2017

Hi Carol,

The code looks ok, I have one small doubt.

You have mentioned in your question " if all subtasks have zero remaining time" Now there could be a scenario wherein the subtask has no estimate at all.

Also, have you executed this script on the "Script console" with proper logging for a certain issue key which has subtasks with remaining estimation. 

Carol Jones March 27, 2017

Thanks, always good to know I'm on the right track!  I was also thinking about adding a || it.getEstimate() == null type of thing, but was trying to get this part working first.  I've been testing first with just a basic setup of one subtask that has the original estimate entered and time recorded against it, but not zeroed out.  See attached (sub-task time recorded).  The validator shows successful when the parent ticket moves through the workflow transition, however it still allows the parent ticket to transition which is not the desired end state. sad

Screen Shot 2017-03-27 at 11.45.34 AM.png

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 27, 2017

In the "Script console" can you try to print the value of 

if (!subTasks.empty) {
    subTasks.each {
        if (it.getEstimate()) {
            println(it.getEstimate()) //print this value
        }
    }
}

Try to print the value and see what it's printing , this would give you immediate insight on where you are going wrong.

Carol Jones March 27, 2017

I feel like this can be simplified, but I was able to get it working with the below.  Thanks for all your help guiding me in the right direction!

Collection subTasks = issue.getSubTaskObjects()
def total = 0

if (!subTasks.empty) { 
    subTasks.each { 
        if (it.getEstimate()){ 
            total += it.getEstimate()
        }
    }
}

total == 0
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 27, 2017

Hi Carol,

Good to hear that you solved the problem. But I have found what exactly was going wrong in your original code smile . Your original code is absolutely fine, but because we are using "closures" in groovy thus it's not working the way it should be working !!

Please read these links

http://stackoverflow.com/questions/765605/how-does-one-return-from-a-groovy-closure-and-stop-its-execution

http://stackoverflow.com/questions/3049790/break-from-groovy-each-closure

https://kousenit.org/2014/04/16/the-closure-of-no-return/

 

Carol Jones March 28, 2017

oh interesting, I'll have to test this out.  From these articles, it looks like if I updated the subTasks.each to subTasks.find we might have a more simplified solution.  Awesome find, thank you!

TAGS
AUG Leaders

Atlassian Community Events