1. Place a file with name credentials.properties Under <script_root> :

app.url=http://example.com
app.user=myuser
app.password=secret

2. Create 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 = 'credentials.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 user = props['app.user'];
def password = props['app.password'];
def url  = props['app.url'];
...

Font: Arnold Müller/Atlassian Community

MrAddon

.

Posted by:.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s