Get All Filters in a Jira System
Here’s the truth: getting all of the filters in a Jira DC instance with ScriptRunner is awkward and fussy. There’s no method that simply returns all of the filters.
Instead, we need to first return all of the users in the system. And then we need to examine all of the filters that each of them owns, as each filter must have an owner. Here’s an example of some code that does that:
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
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()
//Define the parameters of the query
//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
sb.append("Found: ${filter.name}, ${jql}\n" + "<br>")
}
} catch (Exception e) {
//if filter is private
sb.append("Unable to get filters for ${filter_owner.displayName} due to ${e}")
}
}
return sb
Getting a list of filters on Jira Cloud is much simpler, as there’s a REST API that accomplishes this. If we call /rest/api/3/filter/search, a paginated list of filters in the system is returned. As a reminder, I wrote a little pagination primer. The results look like this:
{
"self": "https://<url>.atlassian.net/rest/api/3/filter/search?maxResults=50&startAt=0",
"maxResults": 50,
"startAt": 0,
"total": 2,
"isLast": true,
"values": [{
"expand": "description,owner,jql,viewUrl,searchUrl,favourite,favouritedCount,sharePermissions,editPermissions,isWritable,subscriptions",
"self": "https://<url>.atlassian.net/rest/api/3/filter/10002",
"id": "10002",
"name": "Filter For EP Board"
}, {
"expand": "description,owner,jql,viewUrl,searchUrl,favourite,favouritedCount,sharePermissions,editPermissions,isWritable,subscriptions",
"self": "https://<url>.atlassian.net/rest/api/3/filter/10001",
"id": "10001",
"name": "Filter for EP board"
}]
}
What you do with the results is up to you!
Leave a Reply