ScriptRunner for forcing attachment

Hadas Hamerovv August 21, 2021

Hi,

I've installed ScriptRunner and now wish to force attachment (PRD) whenever a new feature is moved to development.

since validators won't perform best I thought the way to perform this is by adding post function that makes the attachment mandatory when moving the item to the 'Requirements' status.

If I understand the way things work this will force from this stage on to add an attachment. So in the next step (moving to development) the Product team will have to attach the file.

Hopefully once the file is there the system will not force another attachment again and again.

The problem is I have no clue for how to write this script in the post functions.

Can someone help or advise a better way to force adding the PRD document before transitioning the issue to the developers?

 

1 answer

1 accepted

0 votes
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 21, 2021

Ok, take a step back:

This is something you should be doing with a validator.  A post-function is completely and utterly the wrong place to do validation.  Post-functions do things when a transition is confirmed and have no way to go back to a user and say "no, this is wrong".  It's too late to go back by the time you reach the post-functions.  The only way they can stop the transition is to throw a horrible error and possibly leave your issue in an inconsistent state.

This sort of checking is exactly what validators are for.   What makes you think "validators won't perform best"?  (Not that it really matters how they perform, as they are the only place you can actually do this)

Hadas Hamerovv August 24, 2021

Everything you said is correct but notice that I am adding a post function on a prior transition.

So when the issue is in the new status and onwards the field is mandatory. 

I don’t see why won’t that work. My problem though is I don’t yet understand how to write the scriptrunner code. 

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 24, 2021

Again, do not do that.

A post-function is the wrong place to try to do this.  Do it in a validator.

It won't work because post-functions are not the place to do validation and they do not support it.

Hadas Hamerovv August 24, 2021

But I am not doing validation Nic. I just make the field mandatory from a certain point onwards.

I don't see why this approach won't work.

Here are the steps for example:

1. issue is open.

2. Issue is moved to 'in progress' (A post function on this transition makes the attachment field mandatory)

3. Issue needs to be moved to done and now the field is mandatory so cannot be done unless there is an attachment.

Hadas Hamerovv August 24, 2021

I am not arguing with the fact that probably validation is a better approach. I just wish to understand why this approach won't work and maybe for certain cases might be favorited.

Either case, I just watched some tutorials yesterday and will try to implement a code. Wish me luck.  :)

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 24, 2021

Validators are not "better", they're the right way to do it.

Post-functions are not the place to do validation.  

Like İbrahim Yasak likes this
Hadas Hamerovv August 24, 2021

But again. I am not doing validation. I am changing the behavior of an issue from on point in the flow onwards.

Why is it wrong to set certain fields mandatory from one point in the flow onwards?

I can see many usages for it.

What am I missing?

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 24, 2021

There is nothing wrong with what you are trying to do, just the way you are trying to do it is utterly wrong.

Let's go back to the original statement (I have shortened this to simplify it: 

  1. [I] wish to force attachment whenever a new feature is moved to development 
  2. (since validators won't perform best)
  3. Add [something to] makes the attachment mandatory when moving the item to the 'Requirements' status.

Point 3 there reads to me as validation.  The overall aim is to check that an attachment has been added to the issue as it goes (transitions) into the Requirements status.  "Is it valid to perform this transition?".  If yes, continue, if no, then do not do it, and ideally, tell the user why you're not letting it happen.

Assuming that is broadly correct, then you need to understand that a post-function can not do this.  It's too late to "change the behaviour of the transition", post-functions run as part of the committal of the change.

You could do something in a post-function, but it would need to finish the transition, do the check, write something back to the issue, fire an issue event back up to another piece of scripting or automation that can push the issue back through the workflow to its old status, and tell the user "hey, I'm trashing your change" (this of course is clunky and annoying for the user and litters your issue history with junk)

Or, you could use a validator, which is a couple of lines of code, doesn't trash your history and asks the user nicely to provide more information, all within off-the-shelf Jira functionality.  (See why I keep questioning "since validators won't perform best" - where did you get that from?)

Hadas Hamerovv September 3, 2021

Yes you are correct. I did it with validators. I was confuse a little between validators and conditions. (too much staff to learn in few weeks. Never dealt with Jira before)

Thanks man.

Suggest an answer

Log in or Sign up to answer