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 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 } } }
Community moderators have prevented the ability to post new answers.
@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
Thanks @Jamie Echlin [Adaptavist] for the precise code, I meant this closure feature is a bit "non intuitive" (bug is an stronger word)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
Screen Shot 2017-03-27 at 11.45.34 AM.png
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Carol,
Good to hear that you solved the problem. But I have found what exactly was going wrong in your original code . 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/3049790/break-from-groovy-each-closure
https://kousenit.org/2014/04/16/the-closure-of-no-return/
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
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.