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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Can ScriptRunner custom post-function cause transition to fail?

Derek Fields Community Leader Jul 09, 2021

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?

3 answers

0 votes
Derek Fields Community Leader Jul 09, 2021

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.

0 votes

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.

I suspect the problem with the validator approach is that validators check to see if something is there, but the "something" in this case isn't "there" yet because the post function is what's doing the needful.

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.

Derek Fields Community Leader Jul 12, 2021

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.

Derek Fields Community Leader Jul 12, 2021

This is an interesting idea and I will have to think about it a little more. Thanks for suggesting it.

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

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

Events near you