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!
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:
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 fiilter, 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!