Parsing JQL queries in plugins
In the previous recipe, we saw how to build a query to search within JIRA. In this recipe, we will see searching again, but without building a query using the APIs. We will instead use the JQL query as it is written in the Issue Navigator in advanced mode and search using the same.
How to do it…
Suppose we know the query that we want to execute. Let us assume it is the same as we saw in the previous recipe:
project = "DEMO" AND assignee = currentUser()
The following is how we can use the JQL query for searching:
Parse the JQL query:
String jqlQuery = "project = \"DEMO\" and assignee = currentUser()"; SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlQuery);
Check if the parsed result is valid or not:
if (parseResult.isValid()){ // Carry On } else { // Log the error and exit! }
If the result is valid, get the
Query
object from the parsed result:Query query = parseResult.getQuery()
Search for the issues and retrieve the search results...