Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Validator to only allow one of 3 specific labels with groovyscript

Jakob KN
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 3, 2022

I'm trying to script a validator for the create-transition which only allows one label to be chosen, and this label needs to be one of three specific labels depending on the issue type. 

Several issuetypes are using the same workflow, and this validator should only affect two of the issue types (Google Analytics and Marketing), the two issuetypes each have 3 labels the user should be able to choose one of. 

The reason I'm not using custom fields for this, is to prevent the creation of too many unnecessary custom fields, and that it's not possible to assign different context to different issuetypes within the same project so that each issuetype can have different options while using the same custom field. 

Google Analytics allowed labels: Tracking, Analysis and Issue
Marketing allowed labels: Tracking, New_Feature and Issue
All other issuetypes won't be using labels.

I'm using the JMWE scripted validator with a groovyscript. 

The script I've got so far is:

if (issue.getAsString("issuetype") == ("Google Analytics") && (!issue.get("labels")?.any{it.label == "Tracking" || it.label == "Analysis" || it.label == "Issue"} && !issue.get("labels").size() == 1)) {
return false
}

else if (issue.getAsString("issuetype") == ("Marketing") && (!issue.get("labels")?.any{it.label == "Tracking" || it.label == "New_Feature" || it.label == "Issue"} && !issue.get("labels").size() == 1)) {
return false
}

else return true

The validation of issuetypes and labels seem to work, but when i add function for counting the number of labels it doesn't work as expected. 

0 labels returns true
1 allowed label returns true
1 not allowed label returns true
2+ allowed labels returns true
2+ not allowed labels return true

Any help is much appreciated.

2 answers

1 accepted

0 votes
Answer accepted
David Fischer
Community Champion
August 4, 2022

Hi @Jakob KN ,

I believe this is what you're looking for:

if (issue.get("issuetype").name == "Google Analytics")
return issue.get("labels")?.size() == 1 && issue.get("labels").first().label in ["Tracking","Analysis","Issue"]
if (issue.get("issuetype").name == "Marketing")
return issue.get("labels")?.size() == 1 && issue.get("labels").first().label in ["Tracking","New_Feature","Issue"]
return true
Jakob KN
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 8, 2022

Hi David, 

That works exactly as expected. 

Thanks a lot!

0 votes
Florian Bonniec
Community Champion
August 3, 2022

Hi @Jakob KN 

It seems that it do not match any condition and use the last return.

issue.getAsString("issuetype") seems wrong to me, issue.issuetype.name should be used instead.

Also use issue.getLabels() instead of issue.get("labels")

 

Try to build your condition step by step, first make it work by issuetype then add the complexity of checking the labels.

 

regards

Jakob KN
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 4, 2022

Hi Florian, thanks for your reply!

I tested out the script without "!issue.get("labels").size() == 1", and it performs as expected.

If the issue i'm testing has at least one allowed label, it returns true.
If the issue has only not allowed labels, or no labels at all, it returns false. 
For other issuetypes than Marketing and Google Analytics it returns true regardless of which labels the issue has.

It seems that both my original way of getting issuetypes and labels and your suggestion works, since both return the same result when testing.

As i see it, everything points towards the error being with counting the number of labels. 

Any ideas as to how this can be done correctly, so only one label is allowed?

Florian Bonniec
Community Champion
August 4, 2022

Have you try to replace

&& !issue.get("labels").size() == 1

By

&& (issue.get("labels").size() != 1)
Jakob KN
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 8, 2022

Hi Florian, 

I tried that earlier on in my testing, didn't work either unfortunately. 
David posted a working script in case you're interested in seeing the resolution. 

Thanks for your help on this!

Suggest an answer

Log in or Sign up to answer