I'm writing a script runner rest endpoint. Would like to know the best way to run JQL and return the issues found as json.
Hello @scott.lepech
Htre is example that collect issue summaries in project test.
import com.atlassian.jira.web.bean.PagerFilterimport com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.search.SearchProviderimport com.atlassian.jira.jql.parser.JqlQueryParserimport com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegateimport groovy.json.JsonBuilderimport groovy.transform.BaseScriptimport javax.ws.rs.core.MultivaluedMapimport javax.ws.rs.core.Response@BaseScript CustomEndpointDelegate delegategetCRQtemplates(httpMethod: "GET") { MultivaluedMap queryParams ->def query = queryParams.getFirst("query") as Stringdef user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()def rt = [:]def jqlQuery = "project = \"TEST\"" def issueManager = ComponentAccessor.issueManager def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)def query1 = jqlQueryParser.parseQuery(jqlQuery)def results = searchProvider.search(query1, user, PagerFilter.unlimitedFilter)def summaryList = results.issues.collect { issue-> issueManager.getIssueObject(issue.id).getSummary() } rt = [items : summaryList.collect { String row -> [value: row,html: row.replaceAll(/(?i)$query/) { "<b>${it}</b>" },label: row, ] },total: summaryList.size(),footer: "Choose template... " ]return Response.ok(new JsonBuilder(rt).toString()).build();}
And another one from scriptrunner documentation
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegateimport groovy.json.JsonBuilderimport groovy.transform.BaseScriptimport groovyx.net.http.ContentTypeimport groovyx.net.http.HTTPBuilderimport groovyx.net.http.Methodimport javax.ws.rs.core.MultivaluedMapimport javax.ws.rs.core.Response@BaseScript CustomEndpointDelegate delegatepickRemoteIssue() { MultivaluedMap queryParams ->def query = queryParams.getFirst("query") as Stringdef jqlQuery = "project = BSERV and component = Enterprise" def httpBuilder = new HTTPBuilder("https://jira.atlassian.com")def response = httpBuilder.request(Method.GET, ContentType.JSON) {uri.path = "/rest/api/2/issue/picker" uri.query = [currentJQL: jqlQuery, query: query, showSubTasks: true, showSubTaskParent:true]response.failure = { resp, reader ->log.warn("Failed to query JIRA API: " + reader.errorMessages)return } } response.sections.each { section -> section.issues.each {// delete the image tag, because the issue picker is hard-coded // to prepend the current instance base URL. it.remove("img") } }return Response.ok(new JsonBuilder(response).toString()).build()}
Thanks this is great. works for me but how do i list all issue content as json like /rest/api/2/search would?
In my endpoint for collecting details from an issue changelog, I wrote my own JSON output within the endpoint itself, and passed it back in the Response. You can also check out JSONslurper http://groovy-lang.org/json.html.
Hello @Mark Markov ,
I tried to use your script, and I got error 'the variable [uri] is undeclared'
Can you help me with it please?
Thank you!
It looks like you're new here. Sign in or register to get started.