Skip to main content
Skip table of contents

How to import a table into Lookup Manager via ScriptRunner

Scenario

The user can use ScriptRunner to import a table into the Lookup Manager.

How-to

Step 1:Go to ScriptRunner Console

Step 2: Copy and paste the code below into the console

CODE
import groovy.json.JsonOutput;
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*
import groovy.json.JsonSlurper
import net.sf.json.groovy.JsonSlurper
import groovy.json.JsonOutput
import org.apache.log4j.Logger
def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")

log.warn("Importing table for Lookup Manager...")

//Step 1:  Change this to your file path, e.g. <path_to_jira_home>/files/test.csv
File file = new File("YOUR_FILE_PATH_TO_CSV_FILEE")

if (file.exists() && file.isFile()) {
        
    //Step 2: Set table name
    def tableName = "YOUR_LOOKUP_TABLE_NAME";
    
    //Step 3: Set base URL
    def baseUrl = "YOUR_JIRA_BASE_URL";
        
    def http = new HTTPBuilder(baseUrl + "/rest/lookuprestresource/1.0/lookup" + "/table/" + tableName.replaceAll(" " ,"%20"))
    
    String[] lines = file.text.split('\n')
    List<String[]> rows = lines.collect {
        it.split(',')
    }
    
    //Sample: {name: Lookup Table Two, columns: [Reporter, Desccription, Priority], mode: newTable, result: [[Reporter, Desccription, Priority], [admin, This is a message, Medium]]}
    // If the mode is newTable, Lookup Manager will create a new table and append data to it.
    // If the mode is overwrite, Lookup Manager will overwrite the table
    // If the mode is append, Lookup Manager will append the new data to the table
    def dataObj = "{name: " + tableName + ", columns: " + rows[0] + ", mode: overwrite, result: " + rows + "}";    
    
    //Step 4: Set username and password
    def userName = "YOUR_USERNAME"
    def password = "YOUR_PASSWORD"
    
    http.request(PUT) {
                
        headers.'Authorization' = "Basic " + ((userName + ":" + password).bytes.encodeBase64().toString())
        headers.'X-Atlassian-Token' = 'nocheck';
        requestContentType = URLENC
        body = dataObj
        
    	response.success = { resp, data ->
        	log.warn("SUCCESS" + resp.status)
            log.warn(data)
    	}

    	response.failure = { resp, data ->
        	log.warn("ERROR " + resp.status)
            log.warn(data)
    	}
	}
} else {
    "No file found at that location..."
}

Sample CSV:

CODE
Reporter,Desccription,Priority
admin,This is a message,Medium

Learn More

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.