Scriptrunner Select list picker custom scripted field from csv file

Antoine _Klee Group_ February 12, 2024

Hello,

I am trying to code a select list picker scripted field using scriptrunner (jira dc v9.12.2 and scriptrunner v 8.19.0).

My tests are working fine but it looks like the values proposed in the list are not kept up to date with the csv file located on my jira server

Here is my code:

 

import com.onresolve.scriptrunner.canned.jira.fields.model.PickerOption
import groovy.json.*

String filePath = '/pathtomyfile/monfichiercsv.csv'

//Reading the csv file as an inputstream
List csvMapList = []

new File(filePath).withInputStream { file ->
    log.info(file)
    file.eachLine { line ->
        def tmpMap = [:]
        tmpMap.putAt("value", line.toString().toLowerCase())
        csvMapList.add(tmpMap)
    }
}

search = { String inputValue ->
    csvMapList.findAll {it ->
        it.'label'.toLowerCase().contains(inputValue.toLowerCase())
    }
}

toOption = { Map<String, String> map, Closure<String> highlight ->

    new PickerOption(
        value: map.value.toString(),
        label: map.label.toString(),
        html: highlight(map.label, false),
    )
}

getItemFromId = { String id ->
    def selectedOption = csvMapList.find {it.value.toString() == id }
    if (selectedOption) {
        selectedOption
    } else {
        null
    }
}

renderItemTextOnlyValue = { Map item ->
item.'label'.toString()
}
This works more or less fine, but since I basically want to maintain the values from the list up to date according to the csv file, I tried updating it, expecting that the "inputsteam" will open and read the csv each time the field is opened for selection.
Well it turns out it is not the case and the values only appears to be updated whenever I "edit" the scripted field and do an update.
How to explain this and how to solve my issue?

1 answer

0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 6, 2024

I couldn't get your example to work.

But I think what's likely happening is that the scriptrunner/groovy framework is compiling the script into some java class and the csvMapList is instantiated just once when the class is compiled or your application starts.

That's why when you change the script, the file is reloaded (the class is re-compiled).

If you move the loading into a method or closure, you should be able to have this happen dynamically each time the script is called.

Here is your example modified to work in my environment:

import com.onresolve.scriptrunner.canned.jira.fields.model.PickerOption
import groovy.json.*


//Reading the csv file. You don't need an inputstream. Groovy add the eachLine to the file object
List<String> getTermsFromFile() {
List<String> termList = []
String filePath = '/path/to/file.csv'
new File(filePath).eachLine { line ->
termList << line
}
termList
}

search = { String inputValue ->
getTermsFromFile().findAll { it ->
it.toLowerCase().contains(inputValue?.toLowerCase())
}
}

toOption = { String term, Closure<String> highlight ->
new PickerOption(
value: term.toLowerCase(), //here, I'm using the lowercase of the term as the id. That's what will get stored in the DB
label: term,
html: highlight(term, false),
)
}

getItemFromId = { String id ->
def selectedTerm = getTermsFromFile().find { it.toLowerCase() == id }
if (selectedTerm ) {
selectedTerm
} else {
null
}
}

Suggest an answer

Log in or Sign up to answer