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})"
}
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:-
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:-
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:-
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.
3. Once the issue is created, the selected options are displayed in the issue as shown below:-
I hope this helps to answer your question. :-)
Thank you and Kind regards,
Ram
Thank you for explaining so much to me and listing the practical methods. I'll try them out and report back
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.