ScriptRunner groovy code - is it possible to use methods?

Amir Katz (Outseer)
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.
April 30, 2018

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

2 answers

1 accepted

1 vote
Answer accepted
Alexey Matveev
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.
April 30, 2018

You define it like this

def ret = showOneFieldHideAllOthers(cs, b) 

def showOneFieldHideAllOthers(CustomField cs, boolean b) {

 return "my return value";

}
Amir Katz (Outseer)
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.
April 30, 2018

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"
}
Amir Katz (Outseer)
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.
April 30, 2018

@Alexey Matveev   Just to clarify, in my case, the function does not return anything, but questions 3 and 4 are related to your example.

Randy
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.
April 30, 2018

not needed and not better.  it's up to you what standard you want to use for your coding style.

Alexey Matveev
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.
April 30, 2018

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.

1 vote
Randy
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.
April 30, 2018

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);  

Suggest an answer

Log in or Sign up to answer