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

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.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!

Managing JIRA at Scale White Paper

See More From These Topics