We have seen various methods of managing filters. While filters are a great way to save searches and access them quickly at a later point in time, filter subscriptions are even better! The subscriptions help us to see the issues of interest at regular intervals, without even logging in to JIRA.
How do we subscribe to a filter programmatically? In this recipe, we will focus on subscribing to a filter in our plugins.
For the subscription of filters, JIRA provides a manager class implementing the FilterSubscriptionService
interface. This class provides the important methods needed for managing filter subscriptions.
There are three important parameters for filter subscriptions:
Field |
Allowed Values |
Allowed Special Characters |
Second |
0-59 |
, - * / |
Minute |
0-59 |
, - * / |
Hour |
0-23 |
, - * / |
Day-of-Month |
1-31 |
, - * / ? L W C |
Month |
1-12 Or JAN-DEC |
, - * / |
Day-of-week |
1-7 Or SUN-SAT |
, - * / ? L C # |
Year (Optional) |
1970-2099 |
, - * / |
The special characters denote the following:
Special Character |
Usage |
---|---|
, |
List of values. For example, "MON,WED,FRI" means "every Monday, Wednesday, and Friday". |
- |
Range of Values. For example, "MON-WED" means "every Monday, Tuesday, Wednesday". |
* |
All possible values. For example, * in the |
/ |
Increments to the give value. For example, 1/3 in |
? |
No particular value. This is useful when you need to specify a value for only one of the two fields, |
L |
Last possible value. It has different meanings based on the context. For example:
|
W |
Weekday (MON-FRI) nearest to the given day of the month.
For example, 1W means "nearest working day to the 1st of the month" - useful when you want to get the first working day of the month!
It cannot be used with a range of days. |
# |
N'th occurance of a given day of the week.
For example, MON#3 means "3rd Monday of the month". |
We need to create a valid Cron expression based on the subscription we want to set up. The following are some examples based on these rules:
0 7 30 * * ?
- 7:30 AM Every Day0 0/15 15 * * ?
- Every 15 minutes starting at 3.00PM and ending at 3:59 PMYou can find more examples in the Atlassian documentation for filter subscriptions at http://confluence.atlassian.com/display/JIRA/Receiving+Search+Results+via+Email.
Now let us see the steps to subscribe to a known filter:
FilterSubscriptionService
. You can either inject the class in the constructor, or get it using the ComponentAccessor
class, as follows:FilterSubscriptionService filterSubscriptionService = ComponentAccessor.getComponent(FilterSubscriptionService.class);
String cronExpression = "0 0/15 * * * ? *"; // Denotes every 15 minutes
null
if it is a personal subscription:String groupName = "jira-administrators";
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 is subscribed, in case it is a personal subscription.
boolean mailOnEmpty = true;
filterSubscriptionService.validateCronExpression (ctx, cronExpression);
If there are any errors, the Error Collection in JiraServiceContext
will be populated with an error message.
FilterSubscriptionService
class to store the subscription:if (!ctx.getErrorCollection().hasAnyErrors()){ filterSubscriptionService.storeSubscription(ctx, filterId, groupName, cronExpression, emailOnEmpty); }
Here, filterId
is the ID of the filter we want to subscribe to, and can be obtained as searchRequest.getId()
.
The subscription should now be saved and the mails will be sent based on the schedule defined by the Cron expression.
We can also update an existing subscription using FilterSubscriptionService
, using the following method:
filterSubscriptionService.updateSubscription(ctx, subId, groupName, cronExpression, emailOnEmpty);
Here, subId
is the existing subscription ID.
Each subscription we create is stored as a scheduled job in the system, which run based on the Cron expression we have defined while storing the subscription.
If you want to use a web form like the one used in JIRA to create filter subscriptions, and you don't want to write the Cron expression, you can create a CronEditorBean
using the parameters from the web form.
The various attributes supported in the form can be found from the CronEditorBean class. The Java Docs can be found at http://docs.atlassian.com/software/jira/docs/api/latest/com/atlassian/jira/web/component/cron/CronEditorBean.html .
Once the CronEditorBean
is created, it can be parsed into a Cron expression, as follows:
String cronExpression = new CronExpressionGenerator().getCronExpressionFromInput(cronEditorBean);
52.14.174.132