JIRA provides an add-on framework for people with programming skills to create add-ons to extend its features, or to perform tasks that would otherwise be impossible or tedious. However, even with that, it is sometimes an overkill to create a full-blown add-on for what may seem like a simple task. The good news is that there is an option for you to write or program scripts that can take advantage of what the API offers while not having the burden of a full add-on development.
In this recipe, we will create a Groovy script that will share a number of search filters by adding them as favorites for everyone in JIRA—a task that would otherwise take a lot of time if done manually.
For this recipe, we need to have the Script Runner add-on installed. You can download it from the following link and install it with the UPM:
https://marketplace.atlassian.com/plugins/com.onresolve.jira.groovy.groovyrunner
Perform the following steps to run a custom Groovy script in JIRA (note that you will need to update the filter IDs accordingly):
import com.atlassian.jira.ComponentManager import com.atlassian.jira .favourites.FavouritesManager import com.atlassian.jira .issue.search.SearchRequest import com.atlassian.jira .issue.search.SearchRequestManager import com.atlassian.jira.user.util.UserManager import com.atlassian.jira.security .groups.GroupManager //Set the filter ID and group to share with here Long[] searchRequestIds = [10801,10802,10803] String shareWith = "jira-users" ComponentManager componentManager = ComponentManager.getInstance() FavouritesManager favouritesManager = (FavouritesManager)componentManager .getComponentInstanceOfType (FavouritesManager.class) SearchRequestManager searchRequestManager = (SearchRequestManager)componentManager .getComponentInstanceOfType (SearchRequestManager.class) UserManager userManager = componentManager.getComponentInstanceOfType (UserManager.class) GroupManager groupManager = componentManager.getComponentInstanceOfType (GroupManager.class) for(Long searchRequestId in searchRequestIds) { SearchRequest searchRequest = searchRequestManager.getSharedEntity (searchRequestId) for (String userName in groupManager.getUserNamesInGroup (shareWith)){ favouritesManager.addFavourite( userManager.getUserByName(userName), searchRequest) } }
The Script Console window is depicted in the following screenshot:
The Script Runner add-on comes with support for a number of script engines, Groovy being one of them. In the script, we list a number of search filters by their IDs (these filters need to be shared so that other users can favorite them), loop through them, and add each ID as a favorite to the users in the JIRA-users group—all done using JIRA's API.