I have several groovy scripts used with ScriptRunner - some as validators and some as Behaviours. I have l code lines like this (which are repeated for 10 custom fields:
if (is_a) {
field_a.setHidden(!is_a)
field_b.setHidden(is_a)
// more fields here
} elif (is_b) {
field_a.setHidden(is_b)
field_b.setHidden(!is_b)
// more fields here
} elif (is_c) ...
and it can be converted into a method:
showOneFieldHideAllOthers(field_a, is_a)
showOneFieldHideAllOthers(field_b, is_b)
// more calls here
However, I do not want to use the script root mechanism of ScriptRunner - I want all the code to be self-contained in the project.
Questions:
1. Is this possible? Can one use methods in those scripts?
2. If yes, how do I define the signature of the showOneFieldHideAllOthers() - 1st param is a custom field type, 2nd is boolean.
Thanks
You define it like this
def ret = showOneFieldHideAllOthers(cs, b)
def showOneFieldHideAllOthers(CustomField cs, boolean b) {
return "my return value";
}
Thanks. Followup questions:
1. What import do I need to add to get the CustomField type?
2. Can the function be placed anywhere in the script? Best practice (e.g. in Python) would be to put it in the beginning, right after all the import statements, right?
3. Is the ';' in the return really needed?
4. Is it not better to type the function:
String showOneFieldHideAllOthers(...) {
return "some string"
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Alexey Matveev Just to clarify, in my case, the function does not return anything, but questions 3 and 4 are related to your example.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
not needed and not better. it's up to you what standard you want to use for your coding style.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
1. You need to import com.atlassian.jira.issue.fields.CustomField
2. This function can be put anywhere in the script. It does not matter where to put it. Some people put it in the end because in this case you can begin with reading your script not the defined functions.
3. ";" is not needed.
4. I prefer to type my variables and functions, but the Groovy language allows you not to do it, and some people find it more convenient. So it is up to you how you want it to be.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, within the script:
void showOneFieldHideAllOthers(def field, def fields[]) {
// hide fields[]
// show field
}
def fields = [field_a, field_b, field_c];
showOneFieldHideAllOthers(field_a, fields);
showOneFieldHideAllOthers(field_b, fields);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.