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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,558,237
Community Members
 
Community Events
184
Community Groups

Simple script failing? Need help

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

1 vote
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jun 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();
}

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.

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)

 

 

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jun 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.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events