Workflow Regex Validation For Duration/Estimate/Time tracking Custom Field

Adam Wood March 12, 2021

Objective

For my company, I have set up a Service Management HR project for employees to request holidays, time off in lieu etc.
This then clones the request into a Holidays Software project which has an epic for their allowance using original estimate & remaining estimates for how much holiday they are entitled to and how many days they have left.
It then adds a story under that epic for each request they put in with the start and end date.

They can then view this on an Advanced Roadmaps For Jira Plan to use as a holidays calendar and pull those Stories into their own plans so they know who's on holiday when planning work.

Issue

On the request form, I need the employee to fill out a duration field for how much time off they'd like, which could be 1d, 1h or even 30m etc, so I need them to specify.

requestForm.png

This is then used to log time again their holiday epic using the Jira Cloud API to reduce their remaining estimate field.

The problem is I have to monitor each request to make sure they put their duration in the correct format that Jira likes. e.g. 1w 5d 2h 30m with a space separating each time unit. If they don't put a space Jira doesn't like it.

So I want to add a validator on the "Create" transition of the workflow which uses a regex to check for the correct format, I think I'm almost there with the below regex but if anyone has done this before or has any ideas then please let me know!validator.png

^(?:(?<days>\d+)[d]\s+)?(?:(?<hours>\d+)[h]\s*)?(?:(?<minutes>\d+)[m]\s*)?$

I'm basically struggling to get the whitespace check to not match when there's no whitespace between 1d5h or 5h30m.

\s+ - is looking for 1 or more spaces

s* - is looking for 0 or more spaces

Obviously, there's only ever whitespace when another time unit is added so just 1d would still need to pass the check with no whitespace.

I may have gone down a rabbit hole trying to figure this out, and there could be an easy solution that I've missed, any help is appreciated.

Thanks,
Adam

 

2 answers

1 accepted

1 vote
Answer accepted
Mykenna Cepek
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 15, 2021

Bummer that there isn't a "duration" type for a custom field.

Be sure to cast a vote here for such a feature to be added to Jira.

As for your regex, keep in mind that Jira is typically happy with things like "4.5h" for a duration also, not just whole values for each time unit. Obviously this complicates the regex even more. But your users may be used to that.

If you are intent on a regex, then let me offer a couple of things:

  • If you don't have an easy way to test your regex efforts, try this. The "Test match" button is what you want, after filling in your regex and an example text to match against.
  • I don't think you need to be using capture groups in your regex, as the validator doesn't really care about them. It just checks for a match or not.
  • Here's a simple regex to consider:
^ *([0-9]+[WwDdHhMm] *)+$

Note that it is very forgiving about space characters (no need to worry about other whitespace). Decimals are not incorporated. It does not accept a number without a letter. So this is not a direct replacement for Jira built-in duration fields. I offer it as a starting point.

Adding decimals might look like this, but it doesn't support something like ".5d" yet:

^ *([0-9]+(.[0-9]+)*[dDwWmMhH] *)+$

Let us know here what you ended up with!

Adam Wood May 19, 2021

Thanks for your reply :D 

Also thank you for the issue link for the duration field, I have voted and now I'm watching it too. I really hope we get that soon!

Sadly I need to enforce that spaces are used because once their holiday is approved, I have a script runner script that takes the holiday duration value and passes it through to the holiday's board and also uses that value to log time to bring down their remaining time on their holiday epic.

When I pass "4d1h" for example with no spaces via Scriptrunner, the value isn't handled properly, off the top of my head I think it would log 4d and ignore the 1h.

That's why I originally went for the capture group route.

Sadly I've not had time to revisit this regex yet so I'm just manually checking each holiday request before it's approved and changing the spacing or format if I need to.

If I get round to sorting it, I'll definitely update this thread.

Mykenna Cepek
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 19, 2021

To enforce spaces between values,  you can use this regex:

^ *([0-9]+[WwDdHhMm])( +[0-9]+[WwDdHhMm])* *$

For those playing along at home, here's how to decode this:

  • "^" = enforce start-of-line match.
  • " *" = any number of space characters.
  • "([0-9]+[WwDdHhMm])" = one required "Jira duration element" consisting of one or more digits, followed by one Jira duration unit letter.
  • " +" = requires one or more spaces between multiple duration elements.
  • "()*" = this match grouping allows for zero or more subsquent duration elements after the first one (space delimited).
  • "$" = enforce end-of-line match.

All my years as a unix developer really helped develop my "regex fu".  :-)

Adam Wood May 20, 2021

Thanks for that, is there a way to optionally allow decimals and enforce only 1 space?

Also not allow spaces at the start or end?

Ideally, I'd like to match exactly how the Jira time tracking UI handles manual additions of time tracking

Mykenna Cepek
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 20, 2021

Here's the next iteration:

^([0-9]+([.][0-9]+)?[WwDdHhMm])([ ][0-9]+([.][0-9]+)?[WwDdHhMm])*$

This allows decimal points and only a single space between elements, and no spaces at the beginning or end.

Important notes:

  • This is almost certainly NOT exactly what Jira does. However, it's likely close enough to not frustrate users very much.
  • It does NOT support "naked decimals" -- so "0.5h" must be used instead of ".5h"
  • It does not support unit-less values -- so "2h" must be used instead of "2". Jira has a global configuration for the default time unit (typically Hours); more here.
  • This regex isn't optimized, and is intended to be as easy to understand as possible (for future maintenance).

Additional tweaks will be left as an exercise for the reader.

Adam Wood May 21, 2021

Amazing! Thank you so much @Mykenna Cepek I really appreciate your help and skills with this. 🙌

This is the final version I have implemented as a workflow validator on the create transition (just ensured letters were lowercase):


^([0-9]+([.][0-9]+)?[wdhm])([ ][0-9]+([.][0-9]+)?[wdhm])*$

 

Like # people like this
quy_pv3
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 28, 2021

Thank you so much @Adam Wood 

Like Adam Wood likes this
0 votes
Simon H
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 29, 2021

@Adam Wood another solution you might find helpful is ProForma Forms for Jira. Full disclosure I’m one of the Product Managers of ProForma and we recently joined Atlassian team; however, you can use ProForma to build dynamic forms which you can publish on your JSM portal. There are also a greater number of field validation options than normal JSM, including Regex. This means you can apply the regex check when the team member is attempting to create the issue, rather than once it has been created.

Just a thought. 

Kudos to @Mykenna Cepek for your regex pattern too.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events