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

Using JQL Queries within Groovy

Atlassian, Jira

Here 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.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
//your query goes here
def query = jqlQueryParser.parseQuery("project = TES and assignee = amanda.kirk")
//gets results of query
def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())
log.debug("Total issues returned: ${results.total}")
//iterate over each returned issue
results.getIssues().each {retrievedIssue ->
log.debug("Issue Key: " + retrievedIssue.key)
//do stuff here

As you can see, we use the JqlQueryParser object 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 SearchProvider object to gather the results of this filter query. You will then have access to the results of the query, allowing you to view the total number of returned issues, for example, using results.total. From there, you can iterate over the results, going through the issues one by one using results.getIssues().  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!

Managing JIRA at Scale White Paper

TAGS: Atlassian, Jira

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Subscribe to Our Newsletter

Recent Blog Posts