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.PagerFilter
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
@BaseScript CustomEndpointDelegate delegate
getCRQtemplates(httpMethod: "GET") { MultivaluedMap queryParams ->
def query = queryParams.getFirst("query") as String
def 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.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
@BaseScript CustomEndpointDelegate delegate
pickRemoteIssue() { MultivaluedMap queryParams ->
def query = queryParams.getFirst("query") as String
def 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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
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.