Hi.
I am trying to create a scriptrunner post-function based on the presence of a certain label in a *custom* label field.
This is the condition that I have come up with based on other answers:
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.customfields.option.Option;
CustomFieldManager customFieldManager = (CustomFieldManager)ComponentAccessor.getCustomFieldManager()
CustomField custom_field = customFieldManager.getCustomFieldObjectByName( "myCustomLabelField" );
def my_labels = (List<Option>) issue.getCustomFieldValue(custom_field);
if (my_labels != null && my_labels.contains("myTriggeringValue")) {
return true
} else {
return false
}
... but it does not seem to trigger (but also does not throw an error). Has anyone ever done this before?
Hi,
What is "stream_labels"?
in your first "if" you use it, but you never declared it.
From your code, seems to me like you always get "return false" that's is why it's not working for you and why you also not getting errors.
I'm sorry, I anonymized the script, but forgot about that one. Corrected the source.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
my_labels is a collection of labels (Object), not Strings, so you can't check "contains" on it.
This complete code will work for you, i tested it myself:
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
CustomFieldManager customFieldManager = (CustomFieldManager)ComponentAccessor.getCustomFieldManager()
CustomField custom_field = customFieldManager.getCustomFieldObjectByName("myCustomLabelField");
def my_labels = issue.getCustomFieldValue(custom_field);
List<String> labelsList = new ArrayList<>(my_labels.size());
for (Object object : my_labels) {
labelsList.add(Objects.toString(object, null));
}
if (my_labels != null && labelsList.contains("myTriggeringValue")) {
return true
} else {
return false
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That seems a lot saner and actually helps me understand the basic problem - but there still seems to be an issue with this code.
Apparently, this line does not work:
List<String> labelsList = new ArrayList<>(my_labels.size());
It seems like ArrayList cannot handle Objects?
JIRA version is 7.4.2, if this is relevant.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
With the line you mentioned, and the for i wrote right after, the collection of objects (my_labels) is converted to the List<String>
for (Object object : my_labels) {
labelsList.add(Objects.toString(object, null));
}
it is 100% work because i use the same thing and it works for me
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Nir Haimov when you debug,
for (Object object : my_labels) {
labelsList.add(Objects.toString(object, null));
display result for log.error("Please show ---> $labelsList") this give collection of string, and when you get the CustomFieldValue for labels, this is already a collection(List) so you dont need the code above which will make the code not to even work. I guess this this is why @Martin Hohenberg is complaining.
Kind regards,
Moses
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You could simplify this a little by using a find:
testIssue.getLabels().find( label -> label.getLabel() == 'A-Label')
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.