How to use custom picker

James Lebron
Contributor
September 23, 2022

hello community
I have a very tricky problem.
I want to use the custom picker field provided by ScriptRunner.
I hope this field, by calling the jira rest api interface, returns a certain type of issue, and finally the summary of these issues is used as a list for me to choose.
I saw an example provided by the documentation: country picker. But I don't know how to change it, the following is my code?

Remark: Regarding why I want to use rest api, because I use the normal issue picker field, I can't select the issue that I don't have permission to see, so I want to select the issue that I don't have permission to see through the current method

 
import com.onresolve.scriptrunner.canned.jira.fields.model.PickerOption

import groovyx.net.http.ContentType

import groovyx.net.http.HTTPBuilder

import groovyx.net.http.Method

HTTPBuilder getHttpBuilder() {

new HTTPBuilder("http://localhost")

}

search = { String inputValue ->

httpBuilder.request(Method.GET, ContentType.JSON) {

uri.path = inputValue ?

"/rest/api/2/issue/search?jql=reporter=${inputValue}" :

"/rest/api/2/issue/search?jql=reporter=admin"


uri.query = [fields:'key;summary']

response.failure = { null }

}

}

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

new PickerOption(

value: map.summary,

label: map.key,

html: highlight(map.key, false),

)

}

renderItemViewHtml = { Map country ->

"${country.key} (${country.summary})"

}

 

1 answer

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 7, 2022

Hi @James Lebron

You will need to use the REST Endpoint along with the Behaviour for your requirement.

For the REST Endpoint, you can try something like this:-

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.RESTClient

import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate
getIssuesFromProject { MultivaluedMap queryParams, body, HttpServletRequest request ->
def applicationProperties = ComponentAccessor.applicationProperties
def projectManager = ComponentAccessor.projectManager
def issueManager = ComponentAccessor.issueManager

def hostUrl = applicationProperties.getString('jira.baseurl')
def projectKey = queryParams.getFirst('projectKey')

def username = 'admin'
def password = 'q'
def path = "/rest/api/2/project/${projectKey}"

final def headers = ['Authorization': "Basic ${"${username}:${password}".bytes.encodeBase64()}", 'Accept': 'application/json'] as Map
def http = new RESTClient(hostUrl)

http.setHeaders(headers)
def resp = http.get(path: path) as HttpResponseDecorator

if (resp.status != 200) {
log.warn 'Commander did not respond with 200 for retrieving project list'
}

def issueJson = resp.data as Map
def project = projectManager.getProjectByCurrentKey(issueJson['key'].toString())
def issues = issueManager.getIssueObjects(issueManager.getIssueIdsForProject(project.id))

def rt = [
items : issues.collect { issue ->
[
value: issue.summary,
html : issue.summary,
label: issue.summary,
]

},
total : issues.size(),
footer: "",
]

Response.ok(new JsonBuilder(rt).toPrettyString()).build()
}

Below is a screenshot of the REST Endpoint configuration:-

rest_endpoint_config.png

And for the Behaviour, you will need to convert a text field to either a Single Select List or a Multi Select list, i.e. something like:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def sampleTextField = getFieldByName('Sample Text Field')

sampleTextField.convertToMultiSelect([
ajaxOptions: [
url : "${baseUrl}/rest/scriptrunner/latest/custom/getIssuesFromProject?projectKey=MOCK", // <2>
query : true,
formatResponse: "general"
]
])

 Below is a screenshot of the Behaviour configuration:-

behaviour_config.png

Please note that the sample codes provided are not 100% exact to your environment. Hence, you will need to make the required modifications.

Below are a few test screenshots for your reference:-

1. When a new issue is being created, the Summary of the previous issues can now be selected from the List as shown below:-

test1.png

2. When the Summary options are selected in the List, they are populated as shown below. If the Text Field is converted to a single select list, only one option can be selected at a time.

test2.png

3. Once the issue is created, the selected options are displayed in the issue as shown below:-

test3.png

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

James Lebron
Contributor
October 8, 2022

Thank you for explaining so much to me and listing the practical methods. I'll try them out and report back

Suggest an answer

Log in or Sign up to answer