I can't find an answer to this question in the Adaptavist documentation: I want my transition to fail if my custom post-function script fails. Is there a return code that will cause the transition to fail?
Running a very simple test:
assert 1 == 0
causes the transition to fail with an ugly assertion message. Not what I want.
throw new Exception("Failure message")
does not cause the transition to fail.
Still looking for a way to cause my transition to fail.
The only way to "fail" a transition in a post-function is to crash it completely. It certainly is possible to write code that will always crash, but you really really do not want to be doing this.
Post-functions are not expected to break like this, they are expected to do things as part of a validated and successful transition.
If you crash out a post-function, you can easily corrupt your data, because part of the status change will have happened and other bits will not. Crashed post-functions are the number-one reason that people end up with issues they can't transition any more because they're in two different states and with corrupted data.
Never write a post-function script to fail. Write a validator script that checks if your post-function might fail, and fail there instead.
Yes, I've seen this a few times, mostly where the post-function needs to update a remote system and that can fail.
In the cases where you can't validate before acting, the usual resort is "flagging" - add some data to the issue that indicates "I tried to do this, but it failed". Almost always followed up with a service that searches for the flag being set and tries the remote update again, over and over, until it works (and then clears the flag of course). Similar to your looped transition function.
I will have to think about the validator function. I could test whether the actual step would succeed and notify the user in the transition if not.
@Nic I understand what you are saying about not have scripted post-functions that fail and leave the ticket in a unstable state. However, it would be possible for a decent workflow designer to come up with backout solutions. In my case, the post-function would be first in the list so that if it fails, nothing else would be done.
The only "back out solution" I can see is even more complex - you'd need to write "this failed" on to the issue somewhere, then write a listener to catch every change and look for the flag after the fact, then work out how to transition back to the previous state and overwrite all the other changes made during the transition back on to the issue based on what the original value was.
This is because, as I didn't emphasise enough earlier, a post-function makes changes based on the fact that a transition has succeeded. They have no other way to work. They happen after the decision to proceed has been made. You can't cancel or revert the "hitting the ground" post-function after you've let go of the weight you are dropping off the tower of Pisa.
I'm not a groovy developer, so I can only think of an ugly way to do this... but maybe the ugly way would work, and I can't think of an alternative.
Have your transition with the post function actually be circular, so it points back to the same status.
Then, have a fast-track transition that executes after your circular one. The condition would be that the post function had to do whatever it did. If the post function failed, then the fast-track transition wouldn't execute.
The fast-track transition could also be setup with a condition so it's not visible to users and can only be executed through the automation.
Not knowing exactly what you're trying to accomplish with the post function, it's hard to know if the above would work, but this is one way you might be able to accomplish what you're trying to do.
Connect with like-minded Atlassian users at free events near you!Find an event
Connect with like-minded Atlassian users at free events near you!
Unfortunately there are no Community Events near you at the moment.Host an event
You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events