> = [
+export const NOTIFY_WHEN_OPTIONS: NotifyWhenSelectOptions[] = [
{
- value: 'onActionGroupChange',
- inputDisplay: i18n.translate(
- 'xpack.triggersActionsUI.sections.ruleForm.ruleNotifyWhen.onActionGroupChange.display',
- {
- defaultMessage: 'On status changes',
- }
- ),
- 'data-test-subj': 'onActionGroupChange',
- dropdownDisplay: (
- <>
-
-
-
-
-
+ isSummaryOption: false,
+ isForEachAlertOption: true,
+ value: {
+ value: 'onActionGroupChange',
+ inputDisplay: i18n.translate(
+ 'xpack.triggersActionsUI.sections.ruleForm.ruleNotifyWhen.onActionGroupChange.display',
+ {
+ defaultMessage: 'On status changes',
+ }
+ ),
+ 'data-test-subj': 'onActionGroupChange',
+ dropdownDisplay: (
+ <>
+
-
-
- >
- ),
+
+
+
+
+
+
+ >
+ ),
+ },
},
{
- value: 'onActiveAlert',
- inputDisplay: i18n.translate(
- 'xpack.triggersActionsUI.sections.ruleForm.ruleNotifyWhen.onActiveAlert.display',
- {
- defaultMessage: 'On check intervals',
- }
- ),
- 'data-test-subj': 'onActiveAlert',
- dropdownDisplay: (
- <>
-
-
-
-
-
+ isSummaryOption: true,
+ isForEachAlertOption: true,
+ value: {
+ value: 'onActiveAlert',
+ inputDisplay: i18n.translate(
+ 'xpack.triggersActionsUI.sections.ruleForm.ruleNotifyWhen.onActiveAlert.display',
+ {
+ defaultMessage: 'On check intervals',
+ }
+ ),
+ 'data-test-subj': 'onActiveAlert',
+ dropdownDisplay: (
+ <>
+
-
-
- >
- ),
+
+
+
+
+
+
+ >
+ ),
+ },
},
{
- value: 'onThrottleInterval',
- inputDisplay: i18n.translate(
- 'xpack.triggersActionsUI.sections.ruleForm.ruleNotifyWhen.onThrottleInterval.display',
- {
- defaultMessage: 'On custom action intervals',
- }
- ),
- 'data-test-subj': 'onThrottleInterval',
- dropdownDisplay: (
- <>
-
-
-
-
-
+ isSummaryOption: true,
+ isForEachAlertOption: true,
+ value: {
+ value: 'onThrottleInterval',
+ inputDisplay: i18n.translate(
+ 'xpack.triggersActionsUI.sections.ruleForm.ruleNotifyWhen.onThrottleInterval.display',
+ {
+ defaultMessage: 'On custom action intervals',
+ }
+ ),
+ 'data-test-subj': 'onThrottleInterval',
+ dropdownDisplay: (
+ <>
+
-
-
- >
- ),
+
+
+
+
+
+
+ >
+ ),
+ },
},
];
@@ -128,6 +139,8 @@ interface ActionNotifyWhenProps {
hasSummary?: boolean;
showMinimumThrottleWarning?: boolean;
showMinimumThrottleUnitWarning?: boolean;
+ notifyWhenSelectOptions?: NotifyWhenSelectOptions[];
+ defaultNotifyWhenValue?: RuleNotifyWhenType;
}
export const ActionNotifyWhen = ({
@@ -140,11 +153,12 @@ export const ActionNotifyWhen = ({
onSummaryChange,
showMinimumThrottleWarning,
showMinimumThrottleUnitWarning,
+ notifyWhenSelectOptions = NOTIFY_WHEN_OPTIONS,
+ defaultNotifyWhenValue = DEFAULT_FREQUENCY.notifyWhen,
}: ActionNotifyWhenProps) => {
const [showCustomThrottleOpts, setShowCustomThrottleOpts] = useState(false);
- const [notifyWhenValue, setNotifyWhenValue] = useState(
- DEFAULT_FREQUENCY.notifyWhen
- );
+ const [notifyWhenValue, setNotifyWhenValue] =
+ useState(defaultNotifyWhenValue);
const [summaryMenuOpen, setSummaryMenuOpen] = useState(false);
@@ -179,15 +193,53 @@ export const ActionNotifyWhen = ({
[onNotifyWhenChange, onThrottleChange, throttle, throttleUnit]
);
+ const summaryNotifyWhenOptions = useMemo(
+ () => notifyWhenSelectOptions.filter((o) => o.isSummaryOption).map((o) => o.value),
+ [notifyWhenSelectOptions]
+ );
+
+ const forEachAlertNotifyWhenOptions = useMemo(
+ () => notifyWhenSelectOptions.filter((o) => o.isForEachAlertOption).map((o) => o.value),
+ [notifyWhenSelectOptions]
+ );
+
+ const notifyWhenOptions = useMemo(
+ () => (frequency.summary ? summaryNotifyWhenOptions : forEachAlertNotifyWhenOptions),
+ [forEachAlertNotifyWhenOptions, frequency.summary, summaryNotifyWhenOptions]
+ );
+
+ const selectedOptionDoesNotExist = useCallback(
+ (summary: boolean) =>
+ (summary &&
+ !summaryNotifyWhenOptions.filter((o) => o.value === frequency.notifyWhen).length) ||
+ (!summary &&
+ !forEachAlertNotifyWhenOptions.filter((o) => o.value === frequency.notifyWhen).length),
+ [forEachAlertNotifyWhenOptions, frequency.notifyWhen, summaryNotifyWhenOptions]
+ );
+
+ const getDefaultNotifyWhenOption = useCallback(
+ (summary: boolean) => {
+ if (summary) {
+ return summaryNotifyWhenOptions.length
+ ? summaryNotifyWhenOptions[0].value
+ : RuleNotifyWhen.ACTIVE;
+ }
+ return forEachAlertNotifyWhenOptions.length
+ ? forEachAlertNotifyWhenOptions[0].value
+ : RuleNotifyWhen.ACTIVE;
+ },
+ [forEachAlertNotifyWhenOptions, summaryNotifyWhenOptions]
+ );
+
const selectSummaryOption = useCallback(
(summary: boolean) => {
onSummaryChange(summary);
setSummaryMenuOpen(false);
- if (summary && frequency.notifyWhen === RuleNotifyWhen.CHANGE) {
- onNotifyWhenChange(RuleNotifyWhen.ACTIVE);
+ if (selectedOptionDoesNotExist(summary)) {
+ onNotifyWhenChange(getDefaultNotifyWhenOption(summary));
}
},
- [onSummaryChange, frequency.notifyWhen, onNotifyWhenChange]
+ [onSummaryChange, selectedOptionDoesNotExist, onNotifyWhenChange, getDefaultNotifyWhenOption]
);
const summaryOptions = useMemo(
@@ -198,6 +250,7 @@ export const ActionNotifyWhen = ({
onClick={() => selectSummaryOption(true)}
icon={frequency.summary ? 'check' : 'empty'}
id="actionNotifyWhen-option-summary"
+ data-test-subj="actionNotifyWhen-option-summary"
>
{SUMMARY_OF_ALERTS}
,
@@ -207,6 +260,7 @@ export const ActionNotifyWhen = ({
onClick={() => selectSummaryOption(false)}
icon={!frequency.summary ? 'check' : 'empty'}
id="actionNotifyWhen-option-for_each"
+ data-test-subj="actionNotifyWhen-option-for_each"
>
{FOR_EACH_ALERT}
,
@@ -242,14 +296,6 @@ export const ActionNotifyWhen = ({
);
- const notifyWhenOptions = useMemo(
- () =>
- frequency.summary
- ? NOTIFY_WHEN_OPTIONS.filter((o) => o.value !== RuleNotifyWhen.CHANGE)
- : NOTIFY_WHEN_OPTIONS,
- [frequency.summary]
- );
-
return (
{'Per rule run'}>,
+ },
+ },
+ {
+ isSummaryOption: true,
+ isForEachAlertOption: false,
+ value: {
+ value: 'onThrottleInterval',
+ inputDisplay: 'Custom frequency',
+ 'data-test-subj': 'onThrottleInterval',
+ dropdownDisplay: <>{'Custom frequency'}>,
+ },
+ },
+];
const actionTypeRegistry = actionTypeRegistryMock.create();
@@ -367,6 +391,116 @@ describe('action_type_form', () => {
],
]);
});
+
+ describe('Customize notify when options', () => {
+ it('should not have "On status changes" notify when option for summary actions', async () => {
+ const actionType = actionTypeRegistryMock.createMockActionTypeModel({
+ id: '.pagerduty',
+ iconClass: 'test',
+ selectMessage: 'test',
+ validateParams: (): Promise> => {
+ const validationResult = { errors: {} };
+ return Promise.resolve(validationResult);
+ },
+ actionConnectorFields: null,
+ actionParamsFields: mockedActionParamsFields,
+ defaultActionParams: {
+ dedupKey: 'test',
+ eventAction: 'resolve',
+ },
+ });
+ actionTypeRegistry.get.mockReturnValue(actionType);
+ const actionItem = {
+ id: '123',
+ actionTypeId: '.pagerduty',
+ group: 'default',
+ params: {},
+ frequency: {
+ notifyWhen: RuleNotifyWhen.ACTIVE,
+ throttle: null,
+ summary: true,
+ },
+ };
+ const wrapper = render(
+
+ {getActionTypeForm({
+ index: 1,
+ actionItem,
+ notifyWhenSelectOptions: CUSTOM_NOTIFY_WHEN_OPTIONS,
+ defaultNotifyWhenValue: RuleNotifyWhen.ACTIVE,
+ })}
+
+ );
+
+ wrapper.getByTestId('notifyWhenSelect').click();
+ await act(async () => {
+ expect(wrapper.queryByText('On status changes')).not.toBeTruthy();
+ expect(wrapper.queryByText('On check intervals')).not.toBeTruthy();
+ expect(wrapper.queryByText('On custom action intervals')).not.toBeTruthy();
+
+ expect(wrapper.getAllByText('Per rule run')).toBeTruthy();
+ expect(wrapper.getAllByText('Custom frequency')).toBeTruthy();
+
+ expect(wrapper.queryByTestId('onActionGroupChange')).not.toBeTruthy();
+ expect(wrapper.getByTestId('onActiveAlert')).toBeTruthy();
+ expect(wrapper.getByTestId('onThrottleInterval')).toBeTruthy();
+ });
+ });
+
+ it('should have only "Per rule run" notify when option for "For each alert" actions', async () => {
+ const actionType = actionTypeRegistryMock.createMockActionTypeModel({
+ id: '.pagerduty',
+ iconClass: 'test',
+ selectMessage: 'test',
+ validateParams: (): Promise> => {
+ const validationResult = { errors: {} };
+ return Promise.resolve(validationResult);
+ },
+ actionConnectorFields: null,
+ actionParamsFields: mockedActionParamsFields,
+ defaultActionParams: {
+ dedupKey: 'test',
+ eventAction: 'resolve',
+ },
+ });
+ actionTypeRegistry.get.mockReturnValue(actionType);
+ const actionItem = {
+ id: '123',
+ actionTypeId: '.pagerduty',
+ group: 'default',
+ params: {},
+ frequency: {
+ notifyWhen: RuleNotifyWhen.ACTIVE,
+ throttle: null,
+ summary: false,
+ },
+ };
+ const wrapper = render(
+
+ {getActionTypeForm({
+ index: 1,
+ actionItem,
+ notifyWhenSelectOptions: CUSTOM_NOTIFY_WHEN_OPTIONS,
+ defaultNotifyWhenValue: RuleNotifyWhen.ACTIVE,
+ })}
+
+ );
+
+ wrapper.getByTestId('notifyWhenSelect').click();
+ await act(async () => {
+ expect(wrapper.queryByText('On status changes')).not.toBeTruthy();
+ expect(wrapper.queryByText('On check intervals')).not.toBeTruthy();
+ expect(wrapper.queryByText('On custom action intervals')).not.toBeTruthy();
+
+ expect(wrapper.getAllByText('Per rule run')).toBeTruthy();
+ expect(wrapper.queryByText('Custom frequency')).not.toBeTruthy();
+
+ expect(wrapper.queryByTestId('onActionGroupChange')).not.toBeTruthy();
+ expect(wrapper.getByTestId('onActiveAlert')).toBeTruthy();
+ expect(wrapper.queryByTestId('onThrottleInterval')).not.toBeTruthy();
+ });
+ });
+ });
});
function getActionTypeForm({
@@ -383,6 +517,8 @@ function getActionTypeForm({
setActionAlertsFilterProperty,
hasSummary = true,
messageVariables = { context: [], state: [], params: [] },
+ notifyWhenSelectOptions,
+ defaultNotifyWhenValue,
}: {
index?: number;
actionConnector?: ActionConnector, Record>;
@@ -397,6 +533,8 @@ function getActionTypeForm({
setActionAlertsFilterProperty?: () => void;
hasSummary?: boolean;
messageVariables?: ActionVariables;
+ notifyWhenSelectOptions?: NotifyWhenSelectOptions[];
+ defaultNotifyWhenValue?: RuleNotifyWhenType;
}) {
const actionConnectorDefault = {
actionTypeId: '.pagerduty',
@@ -481,6 +619,8 @@ function getActionTypeForm({
actionTypeRegistry={actionTypeRegistry}
hasSummary={hasSummary}
messageVariables={messageVariables}
+ notifyWhenSelectOptions={notifyWhenSelectOptions}
+ defaultNotifyWhenValue={defaultNotifyWhenValue}
/>
);
}
diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/action_connector_form/action_type_form.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/action_connector_form/action_type_form.tsx
index c7a2f10ebc466c7..38d267d01482406 100644
--- a/x-pack/plugins/triggers_actions_ui/public/application/sections/action_connector_form/action_type_form.tsx
+++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/action_connector_form/action_type_form.tsx
@@ -34,6 +34,7 @@ import {
ActionVariable,
RuleActionAlertsFilterProperty,
RuleActionParam,
+ RuleNotifyWhenType,
} from '@kbn/alerting-plugin/common';
import {
getDurationNumberInItsUnit,
@@ -49,6 +50,7 @@ import {
ActionVariables,
ActionTypeRegistryContract,
ActionConnectorMode,
+ NotifyWhenSelectOptions,
} from '../../../types';
import { checkActionFormActionTypeEnabled } from '../../lib/check_action_type_enabled';
import { hasSaveActionsCapability } from '../../lib/capabilities';
@@ -82,6 +84,8 @@ export type ActionTypeFormProps = {
hideNotifyWhen?: boolean;
hasSummary?: boolean;
minimumThrottleInterval?: [number | undefined, string];
+ notifyWhenSelectOptions?: NotifyWhenSelectOptions[];
+ defaultNotifyWhenValue?: RuleNotifyWhenType;
showActionAlertsFilter?: boolean;
} & Pick<
ActionAccordionFormProps,
@@ -125,6 +129,8 @@ export const ActionTypeForm = ({
defaultSummaryMessage,
hasSummary,
minimumThrottleInterval,
+ notifyWhenSelectOptions,
+ defaultNotifyWhenValue,
showActionAlertsFilter,
}: ActionTypeFormProps) => {
const {
@@ -294,6 +300,8 @@ export const ActionTypeForm = ({
)}
showMinimumThrottleWarning={showMinimumThrottleWarning}
showMinimumThrottleUnitWarning={showMinimumThrottleUnitWarning}
+ notifyWhenSelectOptions={notifyWhenSelectOptions}
+ defaultNotifyWhenValue={defaultNotifyWhenValue}
/>
);
diff --git a/x-pack/plugins/triggers_actions_ui/public/index.ts b/x-pack/plugins/triggers_actions_ui/public/index.ts
index d3cf89286ad9642..8fa34efd2e644c1 100644
--- a/x-pack/plugins/triggers_actions_ui/public/index.ts
+++ b/x-pack/plugins/triggers_actions_ui/public/index.ts
@@ -42,6 +42,7 @@ export type {
RuleDefinitionProps,
RulesListVisibleColumns,
AlertSummaryTimeRange,
+ NotifyWhenSelectOptions,
} from './types';
export type {
diff --git a/x-pack/plugins/triggers_actions_ui/public/types.ts b/x-pack/plugins/triggers_actions_ui/public/types.ts
index 83e00bc6dd7c330..f2ecc1d1ec32d7c 100644
--- a/x-pack/plugins/triggers_actions_ui/public/types.ts
+++ b/x-pack/plugins/triggers_actions_ui/public/types.ts
@@ -23,6 +23,7 @@ import type {
EuiDataGridRefProps,
EuiDataGridColumnCellAction,
EuiDataGridToolBarVisibilityOptions,
+ EuiSuperSelectOption,
} from '@elastic/eui';
import { EuiDataGridColumn, EuiDataGridControlColumn, EuiDataGridSorting } from '@elastic/eui';
import { HttpSetup } from '@kbn/core/public';
@@ -737,3 +738,9 @@ export interface TableUpdateHandlerArgs {
export interface LazyLoadProps {
hideLazyLoader?: boolean;
}
+
+export interface NotifyWhenSelectOptions {
+ isSummaryOption?: boolean;
+ isForEachAlertOption?: boolean;
+ value: EuiSuperSelectOption;
+}