Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage
Highlighted

Best practices for writing scripts Edited

Hello.

Scripting for jira is my first major programming experience, so if this discussion is too amateurish - please bear with me.

The situation is the following - whenever I write a script with several functions in it and with a need to access let's say customFieldManager in some of them, I have to define this customFieldManager in every function:

def customFieldManager = ComponentAccessor.customFieldManager

It's not that big of a problem, but I feel like there's a better way.

So far I've found three (second while writing this):
    1. dummy Constants class:

import static Constants.*
class Constants {
static final MYCONSTANT = "foobar"
}

does what it is - creates a dummy class where you can put your global scoped variables

    2. @Field annotation:

import groovy.transform.Field
@Field List awe = [1, 2, 3]
def awesum() { awe.sum() }
assert awesum() == 6

does pretty much the same as method one, just more elegantly I think

    3. Own helper class:
much like method one, writing a dummy class, but making it in a separate file with the help of Scriptrunner's Script Roots functionality:

package util.managers

import com.atlassian.jira.component.ComponentAccessor

class managers {
static final customFieldManager = ComponentAccessor.customFieldManager
//all the other components I use frequently
}

so I can access customFieldManager in any of my scripts by just importing this class:

import static util.managers.*

customFieldManager.getCustomFieldObject(12345)

  So, the question is, am I inventing a bicycle here, and do you have any tricks or hint to making your script better?

 

Thanks.

2 comments

I was hoping to see what others would say about this.

But I don't bother to store managers in different scripts. I try to only declare them if and where needed.

Only if I create a function or a group of related functions that I'd like to use in multiple instances do I group them together into a separate class. Like I have a util.WorkflowHelper class that has a few functions I want to be able to access quickly

If I have a script with multiple functions/methods, I'll either use the @Field annotation to define those managers at the top of the script so that they are available in each script methods. Or sometimes, I'll just declare them without the def prefix (which does something similar ... I'm not 100% clear on the distinction).

The difference is very slight:

If you want a variable to become a field of the class without going into the Binding, you can use the @Field annotation.

Variable without type declaration becomes available in script binding (where, for example, "issue" variable live in SR post-functions, validators, etc.)

As for me, i prefer writing helpers as plugins in Java. It is easy to use them on different instances and it is protected from accidental changing by anybody who has access to file storage. But if you use script plugin for ScriptRunner no problem exists of course.

Like Ilya Turov likes this

Comment

Log in or Sign up to comment
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you