Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

scriptrunner rest endpoint to run jql and return issues as json

scott.lepech
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 25, 2018

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.

1 answer

2 votes
Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 25, 2018 edited

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()
}
scott.lepech
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 25, 2018

Thanks this is great.  works for me but how do i list all issue content as json like /rest/api/2/search would?

Darin Hafer
Contributor
April 24, 2019

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.

Dan27
Contributor
November 24, 2020

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!

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.

Register Now
AUG Leaders

Upcoming Jira Events