Running scripts in JIRA

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.

Getting ready

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

How to do it...

Perform the following steps to run a custom Groovy script in JIRA (note that you will need to update the filter IDs accordingly):

  1. Navigate to Administration | Add-ons | Script Runner.
  2. Select Groovy as the Script Engine.
  3. Copy the following script into the Script text area:
            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)
    
              }
    
            }
  4. Click on Run now to execute the script.

The Script Console window is depicted in the following screenshot:

How to do it...

How it works...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset