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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

scriptrunner rest endpoint to run jql and return issues as json

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

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?

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!

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you