How to get ApplicationUser object for parseQuery using Listener

We are trying to write a script that parses a string into a JQL query, but it's not working:

parseQuery(ApplicationUser searcher, String query)

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.util.ImportUtils;
import com.atlassian.jira.issue.search.*;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.onresolve.scriptrunner.runner.util.UserMessageUtil;
import com.atlassian.query.Query;
import com.atlassian.jira.util.MessageSet;
import com.atlassian.jira.user.ApplicationUser;


def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser);
def searchProvider = ComponentAccessor.getComponent(SearchProvider);
def issueManager = ComponentAccessor.getIssueManager();
SearchService searchService = ComponentAccessor.getComponent(SearchService.class);

UserUtil userUtil = ComponentAccessor.getUserUtil();
ApplicationUser user = ComponentAccessor.getUserManager().getUserByKey("jira-admin");


def query = "project=HELP and created >= -5m";
def query2 = "project=TAG";


SearchService.ParseResult parseResult = SearchService.parseQuery(user, query);
SearchService.ParseResult parseResult2 = SearchService.parseQuery(user, query2);

SearchResults helpTicketList = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter());
SearchResults tagTicketList = searchService.search(user, parseResult2.getQuery(), PagerFilter.getUnlimitedFilter());

 

1 answer

Hi Angela,

I have simplified your script (I optimised your imports, removed unused variables and moved it down to getting just one query working).

package examples

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter

SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
ApplicationUser user = ComponentAccessor.getUserManager().getUserByName("admin")


def query = "project=HELP and created >= -5m"

SearchService.ParseResult parseResult = searchService.parseQuery(user, query)
SearchResults helpTicketList = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())

One major issue I corrected was on the line where you call parseQuery. You were making a static call with SearchService.parseQuery when you should have been calling it on your implementation searchService.parseQuery.

Tip: In Groovy you don't need semi-colons to end a line.

The script I have provided will run and return a result.

Thank you that helps. I noticed this doesn't work in Jira 6.4.3 and Scriptrunner 4.1.3.24 but it works with Jira 7

I'm glad that I was able to help :-) Do you need this to work for Jira 6.4.3? If so are you getting any errors??

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

167 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you