Skip to content

Commit

Permalink
[Alerting] Fix error when saving a rule after toggling alerts filter …
Browse files Browse the repository at this point in the history
…properties on and off (elastic#192522)

## Summary

Closes elastic#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 <[email protected]>
  • Loading branch information
Zacqary and elasticmachine authored Sep 11, 2024
1 parent 233fe60 commit f22067f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f22067f

Please sign in to comment.