Creating Configurations for Jira Groovy Scripts

Tyler Reid February 14, 2018

Hey All,

 

For some post scripts on Jira, I have a set of groovy scripts that I have deployed on the server where jira is located.  These groovy scripts are run on the execution of certain transitions.  Several locations and other values are hardcoded into the script, but realistically the values should be configuration values.  Is there a way to create a configuration file that these groovy scripts will get their values from rather than having to hardcode these values into my scripts and change when I move from development to production?

 

For example, I have something along the lines of the following:

def url = new URL("http://some_dev_url:29000/")

and in production I will need the following

def url = new URL("http://some_prod_url:19000/")

but I would love to have something like this in both dev and productions

def url = new URL("http://" + config.url + ":" + config.port)

Thanks for the help!

 

2 answers

1 accepted

1 vote
Answer accepted
Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 16, 2018

How about externalizing the configuration values to property files and then simply:

Properties properties = new Properties()
File propertiesFile = new File('test.properties')propertiesFile.withInputStream {    properties.load(it)
}

def runtimeString = 'a'
assert properties."$runtimeString" == '1'
assert properties.b == '2'

 Or you could JVM variables to eliminate the file, but that is more painful from other aspects.

Tyler Reid February 16, 2018

This worked.  I had to do some further googling and used what I found here (http://www.baeldung.com/java-properties), but I got it.  Thanks

1 vote
Ivan Tovbin
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.
February 14, 2018

Hi Tyler,

You can utilise GStrings to cast a variable into a string like so:

def url = new URL("http://${configUrl}:${configPort}".toString())

As for using code from a remote script file check out this article.

Hope this helps. 

Tyler Reid February 14, 2018

Yes, you can do that Ivan.  I more or less typed pseudo-code and I think you missed the point of the question.

I could very easily do something like

def configUrl = "Blah"
def configPort = 19000
def url = new URL("http://${configUrl}:${configPort}".toString()
)

 But I am still forced to define my variables withing the file which isn't great design.  I would prefer to do something along the lines of

def configUrl = configFile.url
def configPort = configFile.port
def url = new URL("http://${configUrl}:${configPort}".toString()
)

I am mainly a python developer so sorry if my code is rather pythonic. 

Ivan Tovbin
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.
February 14, 2018

I see. Well I suppose you could store a file in your Jira home directory, which will contain something like:

www.someurl.com:12345

And then call that file in your script:

def scanner = new Scanner(new File("path to your file"))
String urlFromFile
while (scanner.hasNextLine()){
urlFromFile = scanner.nextLine()
}
scanner.close()
def url = new URL("http://${urlFromFile}".toString())

Naturally, file contents can be more complex but that'll require a bit of parsing to be done within your script. 

Sorry if this is not the most elegant solution (I'm not a full fledged developer) but it gets the job done.

Tyler Reid February 14, 2018

It would work I suppose, but I was hoping for a better way to load a configuration value from a file.  Preferably would load from xml or json.

 

I appreciate the help Ivan.

Suggest an answer

Log in or Sign up to answer