ScriptRunner How to store and access configuration data for groovy scripts?

Arnold Müller May 17, 2018

Hi ereryone,

I'm using Adaptavist ScriptRunner 5.3.6 for JIRA Server. I'm trying to setup up a couple of scripts, which need some configuration data, like JDBC URLs, usernames and passwords, which I don't want to hardwire into the scripts.

Where shall I store these data?

There are a two options imho:

  • use JNDI (but I'm having trouble creating an instance of javax.naming.InitialContext):
import javax.naming.InitialContext
def ic = new InitialContext()

// javax.naming.NoInitialContextException: Cannot instantiate class: org.apache.naming.java.javaURLContextFactory

Also, how would I tell JNDI the usernames etc.?

  • read a .properties file. This works:
Properties properties = new Properties()
File propertiesFile = new File('/absolute/path/to/script/root/test.properties')
propertiesFile.withInputStream {
    properties.load(it)
}
log.info properties['test.message'] // works

But I don't like the absolute path. Where should I locate the .properties file? I would guess in my <script_root>, but just using the test.properties without the path won't find the file.

Is there a way to get the script root's absolute path? Using System.getenv() or System.getProperties() does not yield the application_data path.

Thanks for your help!

Arnold

3 answers

1 accepted

3 votes
Answer accepted
Thanos Batagiannis _Adaptavist_
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.
May 17, 2018

If you want the  script root's absolute path then try something like 

import com.onresolve.scriptrunner.runner.ScriptRunner
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def scriptRoots = ScriptRunnerImpl.getPluginComponent(ScriptRunner).getRootsForDisplay()?.split(", ")?.toList()

Keep in mind that may have been more than one script roots configured. 

Arnold Müller May 17, 2018

Hey Thanos,

this works fine for me. That way I can rely on a known file structure beneath (one of) my script root(s).

Thank you!

Arnold

2 votes
Arnold Müller May 18, 2018

Ok thank you. Alexey, I had a look at SAL an I'll explore that, when and if we need a grown-up plugin. For my scripting I'll go with a .properties file.

This is how I've done it:

1. Placed myproject.properties Under <script_root> :

db.driverName = oracle.jdbc.OracleDriver
db.url = jdbc:oracle:thin:@//bla:123/blubb
db.user = myuser
db.password = secret

2. Created an accessor class in <script_root>/mycompany/MyCompanyProperties.groovy:

package mycompany

import com.onresolve.scriptrunner.runner.ScriptRunner
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import java.nio.file.Files
import java.io.File
import groovy.util.logging.Log4j

@Singleton
@Log4j
public class MyCompanyProperties {

    final String configFileName = 'myproject.properties'

    public Properties getProperties() {
        def scriptRoots = ScriptRunnerImpl.getPluginComponent(ScriptRunner).getRootsForDisplay()?.split(", ")?.toList()
        //def scriptRoots = ['a', 'b']
        File propertiesFile = null
        for (root in scriptRoots) {
            propertiesFile = new File("$root/$configFileName")
            if (Files.isReadable(propertiesFile.toPath())) {
                log.info "Found ${propertiesFile.toPath()}"
                break   
            }
        }
        // TODO: what if propertiesFile == null?
        Properties properties = new Properties()
        propertiesFile.withInputStream {
            properties.load(it)
        }    
        properties
    }
}

3. Use that class each time to access any configuration data in my scripts:

import mycompany.MyCompanyProperties 

def props = MyCompanyProperties.instance.properties

def driverName = props['db.driverName'];
def url        = props['db.url'];
Ankit Shrestha November 21, 2022

Hi, Arnold,

 

Have you been successfully able to roll this out?

We are using Data center version and didn't find the option to upload the file with ".properties" files. Only ".groovy" files are allowed to upload for the configuration.

We are trying to figure out how are you uploading ".properties" files? Our requirements are close to yours.

Any help will be appreciated.

 

Thanks

Arnold Müller November 22, 2022

Hello Ankit,

we're using the Server variant of JIRA (now discontinued to my knowledge). So there I could simply ssh into the server and upload the .properties.

I have not looked into DataCenter.

Regards

Arnold

2 votes
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.
May 17, 2018

You can also store your properties in sal. You can find more info here:

https://developer.atlassian.com/server/framework/atlassian-sdk/storing-plugin-settings/

Arnold Müller May 17, 2018

That looks interesting. I think this will be the orthodox solution for a grown-up plugin. Thanks for the link!

Arnold

Suggest an answer

Log in or Sign up to answer