Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,502,081
Community Members
 
Community Events
180
Community Groups

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

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 Mar 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 Mar 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 Mar 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.

0 votes
Tarun Sapra Community Leader Mar 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. 

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 Mar 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.

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 Mar 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/

 

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