This script searches for Jira filters by name, then by filter ID, and then updates the permissions set applied to them.
It’s useful for updating a bulk list of filter names, but really it’s an exercise in working with filters and the search service.
The script iterates through an array of filter names. For each filter name, it uses the searchRequestManager to find that filter object. It then uses the filter ID of the object to return a filter object. This seems redundant, but the object returned by the searchRequestManager isn’t actually a filter. It’s information about the filter.
When we search for the filter by name using searchRequestManager , it will return any filter that has that name. For that reason, we must treat the results like an array, and iterate through them. For each filter ID that is returned by the name search, we use the searchRequestManager to search for the filter object.
Once we’ve got a filter object, we apply the new permissions to it and commit the change with updateFilter().
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.filter.SearchRequestService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.sharing.SharePermissionImpl
import com.atlassian.jira.sharing.SharedEntity
import com.atlassian.jira.sharing.type.ShareType
import com.atlassian.jira.issue.search.*
def searchRequestService = ComponentAccessor.getComponent(SearchRequestService)
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchRequestManager = ComponentAccessor.getComponent(SearchRequestManager)
final authenticatedUserSharePerms = new SharedEntity.SharePermissions([new SharePermissionImpl(null, ShareType.Name.GROUP, "jira-users", null)] as Set)
//Create the permissions that will be applied to the filter
//SharedEntity is a class for any entity that can be shared or favorited
//https://docs.atlassian.com/software/jira/docs/api/7.2.0/com/atlassian/jira/sharing/SharedEntity.html
//We're definine a new set of share permissions:
//https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/sharing/SharePermissionImpl.html
def filters = []
filters.each{filterName ->
def filterIDs = searchRequestManager.findByNameIgnoreCase(filterName)
//Find the filters using their names
//Sometimes more than one filter has the same name, so we treat the results like an array:
filterIDs.each{filterID ->
def filter = searchRequestManager.getSearchRequestById(filterID.id)
//For each result returned when we search for the filter name
filter.setPermissions(authenticatedUserSharePerms)
//Set the updated permissions on the filter
def filterUpdateContext = new JiraServiceContextImpl(currentUser)
//Define the context in which this update will be performed
//If we try to run the update as the filter.owner, it doesn't work with filters that are exposed to anyone on the web
searchRequestService.updateFilter(filterUpdateContext, filter)
//Actually perform the update, using the context and running against the filter
log.warn("Updating permissions for filter: ${filter.name}")
if (filterUpdateContext.errorCollection.hasAnyErrors()) {
log.warn("Error updating filter - possibly owner has been deleted. Just delete the filter. " + filterUpdateContext.errorCollection)
}
}
}
Leave a Reply