Managing filters programmatically

Be you a beginner in JIRA or a professional, one of the features used often is creating and managing filters. The fact that we can save the searches, share them, and subscribe to them adds a lot of value to JIRA. So, how do we programmatically create and manage filters?

In this recipe, we will learn how to manage filters programmatically.

How to do it...

We will see the various aspects of managing the filters one-by-one.

Creating a filter

Most of the operations on managing filters are done using SearchRequestService. For creating a filter, the following are the steps:

  1. Create the query to be saved as a filter. The query can be created using JqlQueryBuilder, as we have seen in the previous recipes.
  2. Create a SearchRequest object from the query, using the appropriate name, description, and so on:
            SearchRequest searchRequest = new SearchRequest(query, 
            loggedInUser, "Test Filter", "Test Description");
  3. Create a JIRA Service Context. If you are in an action class, you can get the service context by calling getJiraServiceContext() and if not, an instance can be created as follows:
            JiraServiceContext ctx = new JiraServiceContextImpl(user);

    Here, user is the user for whom the filter should be created.

  4. Get an instance of SearchRequestService. It can be either injected in the constructor or as follows:
            SearchRequestService searchRequestService = 
            ComponentAccessor.getComponent(SearchRequestService.class);
  5. Create the filter:
            final SearchRequest newSearchRequest = 
            searchRequestService.createFilter(ctx, searchRequest, favorite);

    Here, favorite is a Boolean, which can be set to true if you want the filter to be made a favorite.

Updating a filter

Updating a filter is very similar to creating a filter. Here we have to retrieve the SearchRequest using its ID and then update the attributes such as name, description, and so on, using the setter methods.

Once the SearchRequest is modified, we can use SearchRequestService to update the filter:

SearchRequest updatedFilter = searchRequestService.updateFilter(ctx, newSearchRequest, favorite);

Deleting a filter

JIRA takes the filter ID as the input for deleting a filter. Before we actually delete the filter, we need to validate the deletion as follows:

searchRequestService.validateForDelete(ctx, filterId);

If there are any errors, it will be added into the action's error collection. We can then check for the errors, and delete the filter if there are no errors:

if(!ctx.getErrorCollection().hasAnyErrors()))
{
         searchRequestService.deleteFilter(ctx, filterId);
}

We can also delete all the filters of a user by using the following:

deleteAllFiltersForUser(JiraServiceContextserviceCtx, ApplicationUser user);

Retrieving filters

The SearchRequestService also has a few methods to retrieve favorite filters, filters owned by a user, non-private filters, and so on. Key methods are listed as follows:

Collection<SearchRequest> getFavouriteFilters(ApplicationUser user); Collection<SearchRequest> getOwnedFilters(ApplicationUser user); Collection<SearchRequest> getNonPrivateFilters(ApplicationUser user); Collection<SearchRequest> getFiltersFavouritedByOthers(ApplicationUser user);

The method names are self-explanatory.

Sharing a filter

In order to share a filter, we need to retrieve the relevant filter and set the permissions on it using the following:

searchRequest.setPermissions(permissions);

Here, permissions is a set of SharePermission objects. The SharePermission objects can be created from a JSONArray using the SharePermissionUtils utility class. The JSONObject can have three keys - Type, Param1, and Param2.

The Type can have the following values: global, group, or project:

  • When Type is global, Param1 and Param2 are not required
  • When it is group, Param1 is populated with the groupname
  • When it is project, Param1 is the ID of the project and Param2 is the ID of the project role

An example of JSON arrays is as follows:

[{"type":"global"}]
[{"type":"group","param1":"jira-administrators"},{"type":"project","param1":"10000","param2":"10010"}]

If you want to share a filter with everyone, permissions can be set as follows:

newSearchRequest.setPermissions(SharePermissionUtils.fromJsonArrayString("[{"type":"global"}]"));

See also

  • The Searching in plugins recipe in this chapter
..................Content has been hidden....................

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