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

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

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.

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
Community showcase
Published in Trello

📹 Favorite Trello templates

Hello Community!  My name is Brittany Joiner and I am a Trello enthusiast and Atlassian Community Leader. In this video, I'll share my favorite Trello templates. Templates mentioned in ...

75 views 4 5
Read article

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