<img height="1" width="1" style="display: none" src="https://www.facebook.com/tr?id=976470819114134&amp;ev=PageView&amp;noscript=1">

Sharing your filter editing burdens across the team

One of the useful features of Jira 7.12 is granting edit rights for filters and dashboards. Why? Because filters allow you to quickly display popular search results and share them with other team members. You can choose viewers and editors of the filters, allowing them to make changes whenever it is needed.


Problem

The problem you will likely face is from already having thousands of filters in your system and this feature won't just go ahead and make them editable by others. So how are you ever going to share the burden of editing them all?


Solution

An Atlassian Marketplace App called Scriptrunner can help. We have observed that most clients write a pattern in their filter names to help sort them. In a particular example one of our client used "NA" to distinguish filters for users in the North America region. So we were required to write a short script that will:

  1. Initiate SearchRequestService
  2. Copy the editor share settings from 
  3. Retrieve all SearchRequests with "NA" in the name
  4. Create new SharedEntity.SharePermissions with NA-Administrators as editor
  5. Attempt to save SharedEntity.SharePermissions - this will fail if the filter owner is not already in NA-Administrators


Using a helpful script that Jonny Carter shared on the Atlassian Community forum we came up with this:

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchRequestEntity
import com.atlassian.jira.issue.search.SearchRequestAdminManager
import com.atlassian.jira.issue.search.SearchRequestManager
import com.atlassian.jira.util.Visitor
import com.atlassian.jira.sharing.SharedEntity.SharePermissions
import com.atlassian.jira.sharing.SharePermissionImpl
import com.atlassian.jira.sharing.rights.ShareRights
import com.atlassian.jira.sharing.type.ShareType
import com.google.common.collect.Sets

def filtersWithName = []
def myNameRegex = ~/.*?NA.*?/
def editorGroup = 'NA-Administrators'
def searchService = ComponentAccessor.getComponent(SearchService)
def searchRequestAdminManager = ComponentAccessor.getComponent(SearchRequestAdminManager)
def searchRequestManager = ComponentAccessor.getComponent(SearchRequestManager)
    
def editPermission = new SharePermissionImpl(ShareType.Name.GROUP, editorGroup, null, ShareRights.VIEW_EDIT)

searchRequestManager.visitAll(new Visitor<SearchRequestEntity>() {
 @Override
 void visit(SearchRequestEntity filter) {
	def name = filter.name
 	if (name.findAll(myNameRegex)) {
		def searchRequest = searchRequestManager.getSearchRequestById(filter.id)
        log.warn searchRequest.getPermissions()
 		if (searchRequest.getPermissions().getPermissionSet().contains(editPermission)) {
    		log.warn(searchRequest.getName() + ' already has edit permission for ' + editorGroup)
        } else {
            searchRequest.setPermissions(new SharePermissions(Sets.union(searchRequest.getPermissions().getPermissionSet(), Collections.singleton(editPermission))))
            searchRequestManager.update(searchRequest)
            log.warn searchRequest.getPermissions()
        }
 	}
 }
})


Editing Rights for Dashboards


Here's a similar solution for dashboards. You can change it to search based on a dashboard name rather than specify the dashboard IDs. 


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.portal.PortalPageManager
import com.atlassian.jira.portal.PortalPage.Builder
import com.atlassian.jira.portal.PortalPage
import com.atlassian.jira.sharing.SharedEntity.SharePermissions
import com.atlassian.jira.sharing.SharePermissionImpl
import com.atlassian.jira.sharing.rights.ShareRights
import com.atlassian.jira.sharing.type.ShareType
import com.google.common.collect.Sets


// This is for a static list of Dashboard IDs that need to have edit rights updated to allow gadgets to be modified.
// You could search for IDs by using the PortalPageManager search function
// see https://docs.atlassian.com/software/jira/docs/api/7.13.1/com/atlassian/jira/portal/PortalPageManager.html

def portalPageManager = ComponentAccessor.getComponent(PortalPageManager)
def output = ""
for (region in ['NA','EU']) {
 def dashboardIDs = []
 def editorGroup = ''
 if (region == 'EU') {
 dashboardIDs = [19432,20234]
 editorGroup = 'EU- administrators'
 } else if (region == 'NA') {
 dashboardIDs = [19410,20300,20812]
 editorGroup = 'NA-Administrators'
 }
 def editPermission = new SharePermissionImpl(ShareType.Name.GROUP, editorGroup, null, ShareRights.VIEW_EDIT)
 
 for (id in dashboardIDs) {
 // 1. Get portal page
 def portalPage = portalPageManager.getPortalPageById(id)
 
 // 2. Get permissions
 // output = portalPage.getPermissions().getPermissionSet().toString()
 // 3. Create new portal page with new permission
 
 def newPortalPage = new PortalPage.Builder().
 /* .description(portalPage.getDescription())
 .favouriteCount(portalPage.getFavouriteCount())
 .layout(portalPage.getLayout())
 .name(portalPage.getName())
 .owner(portalPage.getOwner()) */
 portalPage(portalPage).
 permissions(new SharePermissions(Sets.union(
 portalPage.getPermissions().getPermissionSet(), 
 Collections.singleton(editPermission)))).
 build()
 
 
 output += id +": " + newPortalPage.getPermissions().getPermissionSet().toString() + "\n"
 
 // 4. Update portal page
 portalPageManager.update(newPortalPage) 
 }
}
output