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.
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
Hi David,
That works exactly as expected.
Thanks a lot!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Have you try to replace
&& !issue.get("labels").size() == 1
By
&& (issue.get("labels").size() != 1)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.