Incorrect JQL results via Java Api

Hi CDAC,

We have run into an issue in our plugin where a correct jql search query returns no result.
This issue happens for several of our customers and unfortunately we can’t reproduce it in our test environment
Here is the code we’re using:

logger.debug("AdminUser: " + actorUser.getDisplayName() + "(" + actorUser.getKey() + ")");
issues = findIssuesFromJQL(actorUser, jql);

    public List<Issue> findIssuesFromJQL(ApplicationUser user, String jql) {
        try {
            Query query = new DefaultJqlQueryParser().parseQuery(jql);
            logger.debug("query: " + query.toString());
            /* Get JQL-result */
            jiraThreadLocalUtil.preCall();
            
            SearchResults searchResults = searchService.search(user, query, PagerFilter.getUnlimitedFilter());
            List<Issue> issues = searchResultsHelper.getResults(searchResults);
            logger.debug("JQL result size: " + issues.size());
            return issues;
        } catch (Exception ee) {
            logger.error("JQL is invalid: " + jql, ee);
        } finally {
            jiraThreadLocalUtil.postCall(APACHE_LOGGER);
        }
        return new ArrayList<>();
    }

public class SearchResultsHelper {
  private final BuildUtilsInfoImpl build = new BuildUtilsInfoImpl();

  private Method getMethod() {
    String method = build.getVersionNumbers()[0] < 8 ? "getIssues" : "getResults";
    try {
      return SearchResults.class.getMethod(method);
    } catch (NoSuchMethodException e) {
      throw new RuntimeException("Unable to find method: " + method + " in class: SearchResults", e);
    }
  }

  public List<Issue> getResults(SearchResults searchResults) {
    Method method = getMethod();

    try {
      return (List<Issue>) method.invoke(searchResults);
    } catch(IllegalAccessException | InvocationTargetException e) {
      throw new RuntimeException("Error in invoking method: " + method.getName() + " from class: SearchResults", e);
    }
  }
}

As far we can see from debug logs jql is valid and valid ApplicationUser is provided. But jql query returns zero results.
We tested similar query through Jira user interface and REST API - and the users get the right results.

This code is called from within our com.atlassian.scheduler.JobRunner implementation.

Every impacted customer upgraded from Jira 7.x to 8.4.1.

2 Likes

Are you sure that actorUser in this case has BROWSE permissions to the correct projects? It seems as though you should not be relying on a user-provided User object.

1 Like

Good question, yes this is something we have checked. We had the customer log in as this user (confirmed via logs) and browse to the test issue. We also confirmed that they were able to get the test issue via the REST api while authenticating as the mentioned user.

1 Like

Is the JQL query defined by users? Do they use 3rd party functions in that query?
It might be possible they’re using JQL functions that check permissions against authenticationContext instead of user that’s provided in search call (which isn’t correct, but it’s possible).

1 Like

Thanks for the suggestion. We’re having the customer test with the simplest of JQL

key = ABC-123

And are able to reproduce this issue.

What sort of context does this run in? Is this in a user-facing thread or as a Service or Event Handler?

We’re running this in a Scheduler / Job Runner context. We’re seeing the correct JQL and user being logged when this code runs.

Maybe a stupid question, but is it possible that when you invoke the method getResults() which returns List<T>, that it returns a list of objects that cannot be cast into Issues for some reason? Or is it returning an empty list of results as well?

You don’t need to call jiraThreadLocalUtil from scheduler job runners, I’m wondering if this might cause the issue.

We built a custom version without this, and it’s still not working in the client environment.

The page at https://ecosystem.atlassian.net/servicedesk/customer/portal/14 says they no longer answer dev questions, does anyone know if this is the case? We’re completely stuck here.

Did you also try setting logged in user in authentication context? There might be a bug in Jira somewhere, you never know.

1 Like

Hi @boris were you able to find a solution for this problem? We also have some customers experiencing this problem on their instances with our apps.