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

Regular expression validator to "Fix Version" value

NogaE July 20, 2016

Hi,

I want to restrict the Fix Version value to a few specific patterns (only numbers) so the users won't be able to insert any value they want. For example:

x.x.xx

x.x.xx.xxx

x.x.xx.xxxx

 

The "regular expression check" validator doesn't help because fix version doesn't show in the fields list.

Is there a way to use groovy script for that purpose?

Thanks,

Noga.

 

7 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
adammarkham
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.
July 27, 2016

The tricky part is newly created versions because they are not created yet so are not proper Version objects.

You should use the following behaviours script on the Fix Version/s field:

import com.atlassian.jira.project.version.Version
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def formField = getFieldById(getFieldChanged())

def fixVersions = formField.getValue() as List<Version>

// This handles newly created versions by the user (single and multiple)
def rawValues = []
rawValues.addAll(formField.getRawValue())
def createdFixVersions = rawValues.findAll { version ->
    version.startsWith("nv_")
}.collect { version ->
    version.replaceAll("nv_", "")
}

def allFixVersions = fixVersions*.name + createdFixVersions as List<String>

if (allFixVersions && ! allFixVersions.every { it ==~ /^(\d+\.)?(\d+\.)?(\*|\d+)$/ }) {
    formField.setError("Fix version invalid!")
} else {
    // no versions or fix version is valid
    formField.clearError()
}

The regex matches the following version format from: http://stackoverflow.com/questions/82064/a-regex-for-version-number-parsing

You will need to adjust it for your use case. This will help you get started.

Hope this helps,
Adam

NogaE August 1, 2016

Does "nv_" is the start of any new version? or I need to adjust it too?

def createdFixVersions = formField.getRawValue().findAll { version ->
    version.startsWith("nv_")
}.collect { version ->
    version.replaceAll("nv_", "")
}

Noga.

adammarkham
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.
August 1, 2016

Its the start of any new version that the user enters through the create new version in the dropdown. These are not actually created till you submit the issue form, so are prefixed with "nv_".

The code I gave you should work for all these cases. You just to adjust the regular expression.

NogaE August 1, 2016

Hi Adam,

It is still not working..

I changed the regex and used this expression:

/^([0-9]{1,2}+)\.([0-9]{1,2}+)\.([0-9]{1,2}+)\.([0-9]{0,4})$/

I don't get the error message ("Fix version invalid!") when I add a new unmatched fix version and I can submit the changes.

 When I enter the edit screen in the second time, I get the error and it prevent me to click "edit" and update the issue.

Attached screenshots

.first time - edit screen.PNGsecond time - edit screen.PNG

 

I need it to display the error when I try changing the fix version on the first time.

Thanks.

adammarkham
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.
August 15, 2016

Is this JIRA 6 or 7? For some reason I only got an email just now that you had added this comment.

NogaE August 15, 2016

JIRA 7

adammarkham
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.
August 15, 2016

 Hi Noga, I have updated the script in my answer. For some reason my script didn't handle single versions only multiples for the new versions. Hope this helps.

NogaE August 15, 2016

WORKS !! Thank you very much smile

adammarkham
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.
August 15, 2016

No problem, glad we got there in the end.

0 votes
NogaE July 26, 2016

Still not working. The behaviour is mapped to all projects and all issue types:

FixVersion.PNG

0 votes
adammarkham
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.
July 26, 2016

You should add the script on the fix version field and then replace the second line with:

def fixVersionField = getFieldById(getFieldChanged());

That should work.

0 votes
NogaE July 26, 2016

Hi Adam,

I tried using the behaviours and added a serverside script on Fix Version field.

But it doesn't do anything.

import com.atlassian.jira.project.version.Version;
def FixVersionField = getFieldById("fixVersion");
def versions = FixVersionField.getValue() as List<Version>
versions.any {it.getName().matches(/(\d{1,2})\.\d\.\d{2,2}\.\d{0,4}\)/)}

Is this how it should look ? 

Noga.

0 votes
NogaE July 23, 2016

Hi Adam,

I want to restrict the format of fix version always - during a transition , in edit screen and when in every other option you have to add a new version.

 

Noga.

0 votes
JamieA
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.
July 21, 2016
NogaE July 23, 2016

Thats applies only to transition. I need it also on edit screen ect'.

Thanks.

0 votes
adammarkham
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.
July 21, 2016

Is this for when you edit an issue, you want to restrict the fix version added? Rather than editing on a screen during a transition? Once I have more information I will give an example but behaviours will be useful for this: https://scriptrunner.adaptavist.com/4.3.2/jira/behaviours-overview.html

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events