[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 <elasticmachine@users.noreply.github.com>
This commit is contained in:
Zacqary Adam Xeper 2024-09-11 12:10:07 -05:00 committed by GitHub
parent 233fe603b5
commit f22067fbc9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 2 deletions

View file

@ -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();
});
});

View file

@ -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,