<img height="1" width="1" style="display:none;" alt="" src="https://px.ads.linkedin.com/collect/?pid=299788&amp;fmt=gif">
Go Back

Using JQL Queries within Groovy

Amanda Kirk Amanda Kirk | June 30, 2023 | 3 MIN READ
Using JQL Queries within Groovy

Untitled-81Here at Isos Technology, implementing custom Groovy scripts for Jira is commonplace. Whether it be for cleanup, complex post functions in workflows, or scripted behaviors for fields, most Jira Admins become very familiar with utilizing Groovy for more complex requirements. As it turns out, Groovy allows us to provide some pretty elegant and optimal solutions to even the most complicated of client needs.

For whatever reason, you may find yourself needing the results of a JQL query within one of your Groovy scripts. You might need to manipulate or gather data from many issues as a part of your larger script, for example. I wanted to share this snippet of code with you to show you how easy it is to run a JQL query within Groovy.

You ready? Let's jump into it!

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

def searchService = ComponentAccessor.getComponent(SearchService)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def queryParser = ComponentAccessor.getComponent(JqlQueryParser)
def sb = new StringBuffer()

//your query goes here
def query = queryParser.parseQuery("assignee = amanda.kirk")

//gets results of query
def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())

//iterate over each returned issue
search.results.each

{ retrievedIssue -> sb.append("Issue Key: " + retrievedIssue.key + "\n") //do stuff here }

return sb.toString()
 

As you can see, we use the JqlQueryParser to parse our JQL query. You can paste in any query here, as long as it is valid (I always test mine within Jira as a start). Next, we use the SearchService to do the search and gather the results of this filter query. You will then have access to the results of the query, allowing you to  iterate over the results, going through the issues one by one using results.each.  As you can see, you will then be able to retrieve data from the individual issues returned by your query.

That's it! Simple, no?

Of course, you could just run queries within Jira and get the same results. But remember, Jira has limits on how many rows you can view at one time. You may also have to possibly export the data and then parse through those results yourself in order to effectively use them in your script. The results within Jira will not necessarily aggregate data the way you may want. There are also some built in reporting mechanisms within Jira that can provide metrics and calculations on issues returned by a filter, though these often have specific use cases and may be too clunky for your needs. Sometimes it just makes sense and is more efficient to run the JQL query from within your script. For more information on the classes and methods used in the script above, I suggest making the Jira API documentation a common reference.

Happy Automating!

New call-to-action

Recent Articles

Searching all Jira Filters for a Specific String
In this blog I will be providing yet another useful Groovy snippet to help you get the data you need from your Jira instance in a jiffy!
Amanda Kirk Amanda Kirk 4 MIN READ
Read More
atlassian, jira, groovy
A How-to on Pulling Jira Active User Lists with Groovy
Here at Isos Technology, implementing custom Groovy scripts for Jira is commonplace. Whether it's for cleanup, complex post functions in workflows, or other needed administration tasks, most Jira...
Amanda Kirk Amanda Kirk 4 MIN READ
Read More
Triggering a Script When a Custom Field Changes
Triggering a Script When a Custom Field Changes
At Isos, we encounter many situations where either clients or we would like to trigger a custom action or script when a custom field changes. With no out-of-the-box Jira solution, we have had to come...
Amanda Kirk Amanda Kirk 3 MIN READ
Read More