Searching all Jira Filters for a Specific String

Atlassian, Jira

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!

There may be times where you need to search over all of the filters being used in your Jira instance to check for a specific query, such as a custom field, that is being references within the JQL. Maybe you want to find all the areas where a specific project or function is being used, or want to identify how many filters reference a specific user. The script I provide in this blog will allow you to quickly search over all filters to find all instances where a string of your choice is referenced......But first, a use case!

The Use Case

As Jira Admins, we often need to evaluate the impact of changes, one being removing a custom field from the system. When doing so, it is important to work with end users to understand the implications and also make sure that you take the proper steps to ensure nothing gets broken in the process. This could include any of the following:

  • Checking for scripts and conditions/validators that use the field, removing or modifying these steps.
  • Checking if the data stored in the field needs to be salvaged. Will the loss of data effect anything?
  • Bulk updating fields as needed.
  • Re-indexing the system.
  • Running an Integrity Check to scan your configuration and check for possible degradation/issues.
  • Ensuring filters aren't referencing the field so that they still function

In order to evaluate how many and which filters are currently referencing a custom field we are planning to delete, we will need to search all of the filter's JQL for that custom field name. This would typically be a complex and time consuming task, but luckily, I have created a simple and effective groovy script that will do this for you!

Again, the problem statement is to Find all (JQL) Filters referencing a specific string. In the use case above, this string would be a custom field name.

In the script below, we are searching for any references to an "Account" field in our instance's filters. To use the script for yourself, all you need to do is substitute your string in the CUSTOM_FIELD_NAME variable.


import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.filter.SearchRequestService
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.bc.user.search.UserSearchService
// Change this constant to the string you want to search for >String CUSTOM_FIELD_NAME = 'Account'

SearchRequestService searchRequestService = ComponentAccessor.getComponent(SearchRequestService.class)
UserSearchService userSearchService = ComponentAccessor.getComponent(UserSearchService)
def sb = new StringBuffer()
UserSearchParams userSearchParams = new UserSearchParams.Builder()
.allowEmptyQuery(true)
.includeInactive(false)
.ignorePermissionCheck(true)
.build()
//iterate over each user's filters
userSearchService.findUsers("", userSearchParams).each{ApplicationUser filter_owner ->
try {
searchRequestService.getOwnedFilters(filter_owner).each{SearchRequest filter->
String jql = filter.getQuery().toString()
//for each filter, get JQL and check if it contains our string
if (jql.contains(CUSTOM_FIELD_NAME)) {
sb.append("Found: ${filter_owner.displayName}, ${filter.name}, ${filter.getPermissions().isPrivate() ? 'Private' : 'Shared'}, ${jql}\n")
}
}
} catch (Exception e) {
//if filter is private
sb.append("Unable to get filters for ${filter_owner.displayName} due to ${e}")
}
}
//output results
return sb.toString()
Here is some sample output for the above script:
 
Found: Amanda Kirk, Filter for Isos Support, Shared, {Account = "Isos Support"} AND ( {resolution = "Unresolved"} OR {resolutiondate >= "-2w"} ) order by Rank ASC
Found: Amanda Kirk, Filter for X Made Support, Shared, {Account = 154} AND {createdDate >= startOfYear()} order by Rank ASC

As you can see, this is a simple yet powerful script that can get you access to data that is typically buried deep within a huge list of filters. So, next time you find yourself needing to get some data on where specific items are referenced in JQL across your instance, feel free to hop back over to my blog and save yourself some time by using the script I have provided here. And as always, 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 Blog

Recent Blog Posts