From f22067fbc907c0ca0c5e919a25cc1afdab90d2c6 Mon Sep 17 00:00:00 2001 From: Zacqary Adam Xeper Date: Wed, 11 Sep 2024 12:10:07 -0500 Subject: [PATCH] [Alerting] Fix error when saving a rule after toggling alerts filter properties on and off (#192522) ## Summary Closes #184170 Fixes a bug where the `alertsFilter` property gets added to `action`s configured in the rule form when toggled on, but then never gets deleted when toggled off. This would throw a validation error on the server for an empty `alertsFilter` object, which is only meant to throw for user-configured API requests and not for form usage. ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios ## Release notes Fixed a bug where toggling on and off the "If alert matches a query" or "If alert is generated during timeframe" toggles makes it unable to save the rule due to validation errors. --------- Co-authored-by: Elastic Machine --- .../sections/rule_form/rule_reducer.test.ts | 34 +++++++++++++++++++ .../sections/rule_form/rule_reducer.ts | 4 +-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_form/rule_reducer.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_form/rule_reducer.test.ts index 373cb70e0ca36..2d2082b64b6e2 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_form/rule_reducer.test.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_form/rule_reducer.test.ts @@ -240,4 +240,38 @@ describe('rule reducer', () => { ); expect(updatedRule.rule.alertDelay?.active).toBe(10); }); + + test('if rule action alerts filter was toggled on, then off', () => { + initialRule.actions.push({ + id: '', + actionTypeId: 'testId', + group: 'Rule', + params: {}, + uuid: '123-456', + }); + let updatedRule = ruleReducer( + { rule: initialRule }, + { + command: { type: 'setRuleActionAlertsFilter' }, + payload: { + key: 'query', + value: 'hello', + index: 0, + }, + } + ); + expect((updatedRule.rule.actions[0] as SanitizedRuleAction).alertsFilter).toBeDefined(); + updatedRule = ruleReducer( + { rule: initialRule }, + { + command: { type: 'setRuleActionAlertsFilter' }, + payload: { + key: 'query', + value: undefined, + index: 0, + }, + } + ); + expect((updatedRule.rule.actions[0] as SanitizedRuleAction).alertsFilter).toBeUndefined(); + }); }); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_form/rule_reducer.ts b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_form/rule_reducer.ts index c11dd0edb3e71..33a7284ac2bef 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_form/rule_reducer.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_form/rule_reducer.ts @@ -6,7 +6,7 @@ */ import { SavedObjectAttribute } from '@kbn/core/public'; -import { isEqual } from 'lodash'; +import { isEqual, isUndefined, omitBy } from 'lodash'; import { Reducer } from 'react'; import { RuleActionParam, @@ -262,7 +262,7 @@ export const getRuleReducer = return state; const { alertsFilter, ...rest } = oldSanitizedAction; - const updatedAlertsFilter = { ...alertsFilter, [key]: value }; + const updatedAlertsFilter = omitBy({ ...alertsFilter, [key]: value }, isUndefined); const updatedAction = { ...rest,