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,699
Community Members
 
Community Events
184
Community Groups

JQL restAPI frequently returns null values

I am running a fairly simple JQL, counting to results and returning them in a JSON format:

It works for a few days, and then it stops and I get this:

[{"target":"jira Open Tickets","datapoints":[[0]]},{"target":"jira Open Procurement","datapoints":[[0]]}]

 

I simply disabled and re-enable and boom!

[{"target":"jira Open Tickets","datapoints":[[16]]},{"target":"jira Open Procurement","datapoints":[[10]]}]

 

So it's strange it seems happy and it works and then later returns null.  I was curious if anyone could see any issues.  I am very very new to Java.

 

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

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter

@BaseScript CustomEndpointDelegate delegate

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser

def jql= "project = PROJ and resolution = Unresolved ORDER BY 'Time to resolution' ASC"
def jqlProcurement = "project = PROJ AND status = 'Awaiting Delivery'"


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

def query = jqlQueryParser.parseQuery(jql)
def queryProcurement = jqlQueryParser.parseQuery(jqlProcurement)

long[] results
results = new long[1]
results[0] = searchService.searchCount(user,query)

long[] resultsProcurement
resultsProcurement = new long[1]
resultsProcurement[0] = searchService.searchCount(user,queryProcurement)

helpdeskOpen(httpMethod: "GET") { MultivaluedMap queryParams, String body ->
return Response.ok(new JsonBuilder([
["target": "jira Open Tickets","datapoints": [results]],
["target": "jira Open Procurement","datapoints": [resultsProcurement]]
]).toString()).build();
}

 

 

1 answer

0 votes
Radek Dostál
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.
Dec 17, 2020 • edited

Do I follow this right that this is your registered REST Endpoint in ScriptRunner (https://scriptrunner.adaptavist.com/latest/jira/rest-endpoints.html) and you are requesting the URL remotely to get the json?

 

Can you show the script how you are implementing the GET calls? My first hunch says this could be that you run this for a while, your authentication cookie expires, but because your instance allows anonymous access, the search will actually finish correctly without errors, but because it does the search now anonymously it will not find any relevant issues, hence, will think that 0 is correct for anonymous users.

 

You could also add logging to the script via

import org.apache.log4j.Level

log.setLevel(Level.DEBUG)

log.debug("Currently processing search for user $user, jql is $jql and results are $results")
// or similar 

 

Edit:

I'm also looking at the way you are getting the current user via JiraAuthenticationContext.getLoggedInUser()

 

The documentation says to use a different approach - https://scriptrunner.adaptavist.com/latest/jira/rest-endpoints.html#_get_the_user_making_the_request

Not sure if there would be a difference between them but caught my eye.

Thank you for the help! I think it is definitely an authorization issue.

 

However, I am confused on why I even need a user at all, if I am just trying to make an unauthenticated API.

I am accessing via a simple curl/wget:

https://jira.server.dns.org/rest/scriptrunner/latest/custom/helpdeskOpen

 

In the example you linked, it is just getting the user making the request and returning it in a json format.  But is it not possible to simply not require a user to authenticate?

 

Also, after I authenticate, or disable/renable, my grafana server can call the API without issue, which would be a completely separate HTTP session?  I am confused on how authentication is working, or why it is even needed.

 

But thank you for the reply! I dont really know enough about java, groovey, or scriptrunner and I am trying to learn.

Radek Dostál
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.
Dec 18, 2020

Depends how the 

searchService.searchCount(user,query)

method treats the user I think, because you are trying to provide the value in the script after all.

 

Now what I don't know is how the SeachService.searchCount(ApplicationUser, Query) handles the applicationUser object - because you are still trying to provide a value to it, which you are getting from a potentially wrong interface (so I don't know what can happen to the variable over time, or how SearchService reacts to it when it becomes null). You might want to test it with some debug logging to be totally sure what happens in your environment. But I think we can be pretty sure it just stops finding issues when that variable becomes null, there's nothing else in the script that could affecting the results.

 

I would personally just hard-code some functional account to it and you should theoretically be fine with that, so long as that user has full browse access to those issues.

There is a UserManager interface with .getUserByName(String) method to supply the user.

import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.ApplicationUser

UserManager userManager = ComponentAccessor.getUserManager()
ApplicationUser userToSearchWith = userManager.getUserByName("automation-account")

All in all hard-coding some users is never a good practice but hey, gets the job done to verify.

 

I am a quite baffled with one thing though, if you have an active session in your browser, and send some http requests with curl, then that should not be using any cookies or whatever unless you specifically supply them in that curl. I can only assume it must be the getLoggedInUser() thing.

 

Let me know in case I've missed something and/or should backtrack :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events