Simple script failing? Need help

Wesley Stewart June 28, 2021

I am trying to figure out what I think is a simple issue.  I am trying to create a restAPI using scriptrunner, and simply dump the data from a JQL.  I almost have it working I believe...

 

However, this returns a blank page...

def jql= "project = Project1"

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)

def query = jqlQueryParser.parseQuery(jql)
def results = searchService.search(user,query, PagerFilter.getUnlimitedFilter())
helpdeskActiveAgent(httpMethod: "GET", groups: ["Scriptrunner Access Group"]) { MultivaluedMap queryParams, String body ->
return Response.ok(
results.results
).build();
}

 

However, instead of using results.results or results.getResults(), and I use results.total, I get a valid answer [314].  However when trying to figure the JQL results, I get null.

 

Ultimately just trying to learn.  Any help appreciated!

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
PD Sheehan
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.
June 28, 2021

I think you'll want to curate your response body a little more than just providing a collection object like "results.results".

And depending on what you want to return, you may want to manually generate a JSON object. The default "toString()" method might be okay too.

Also, I am not sure it's a good idea to have your jql search outside of the endpoint definition.

Here is what I used for testing:

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.issue.search.SearchQuery
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.bc.issue.search.SearchService
import groovy.json.JsonBuilder
import groovy.transform.BaseScript

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

def jql= "project = xxx"

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)


helpdeskActiveAgent(httpMethod: "GET", groups: ["Scriptrunner Access Group"]) { MultivaluedMap queryParams, String body ->
def query = jqlQueryParser.parseQuery(jql)
def results = searchService.search(user,query, PagerFilter.getUnlimitedFilter())
return Response.ok(
new JsonBuilder( results.results.take(10).collect{it.key}).toString()
).build();
}

Wesley Stewart June 28, 2021

Thanks for this!  Will try in the morning.

 

Ultimate I want to make a rest api that will give an open ticket count for each agent in a project.  However currently I am just trying to get the api to output something.

 

I appreciate your pointers!  I'm definitely trying to learn more Java and groovey.

Wesley Stewart June 29, 2021

Peter, that is giving me output!

I am curious, why do you think my initial attempts were only outputting null?  Was it the collect method that I was missing?

 

Also, this is dumping the "it.assignee" instead.  My goal is to be able to count issues for each agent so I can display in a grafana dashboard.  However I am curious how I could only output "username"?

Looking at the JSON output, the object should just be:

it.assignee.username

But I receive:

Cannot get property 'username' on null object

Any advise?  Thanks again!

 

*EDIT*

Solved - the collect method requires a field, and I was trying to pass the field and a specific object.  This Worked:

collect{it.assignee}.username)

 

 

PD Sheehan
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.
June 29, 2021

The Response.ok() method will take a simple object and automatically attempt to convert it to a JSON string. When you simply pass results.results, it appears to be failing (silently) in that conversion.

I use the collect method to convert the more complex object into a simpler ArrayList of string which I then manually convert to JSON string using JsonBuilder (so that I can see the error if there is one)

If you want to get a count of issues per assignee, try this:

helpdeskActiveAgent(httpMethod: "GET", groups: ["Scriptrunner Access Group"]) { MultivaluedMap queryParams, String body ->
def query = jqlQueryParser.parseQuery(jql)
def results = searchService.search(user,query, PagerFilter.getUnlimitedFilter())
def assignees = results.results.collect{it.assignee}.unique()
def assigneeMap = assignees.collectEntries{assignee->
[(assignee.username) : results.results.findAll{it.assignee==assignee}.size()]
}
return Response.ok(
new JsonBuilder(assigneeMap).toString()
).build();
}

First I extract a unique list of assignees from the search results

Then I create a Map object using the collectEntries method over the list of unique assignees. Each entry in the map has the assignee.username as the key and the found of issues for that assignee in the query results.

TAGS
AUG Leaders

Atlassian Community Events