From 47ca6e42e5d3098c622665abb33976b8fd5bb91f Mon Sep 17 00:00:00 2001 From: Christos Nasikas Date: Tue, 19 Sep 2023 11:20:44 +0300 Subject: [PATCH] Improvements and fixes --- .../transform_operations/v1.test.ts | 66 +++++++++++++++++++ .../transforms/transform_operations/v1.ts | 4 +- .../transforms/transform_create_body/v1.ts | 2 +- .../lib/add_generated_action_values.test.ts | 6 ++ .../lib/add_generated_action_values.ts | 1 - .../format_legacy_actions.ts | 9 ++- 6 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 x-pack/plugins/alerting/server/routes/rule/apis/bulk_edit/transforms/transform_operations/v1.test.ts diff --git a/x-pack/plugins/alerting/server/routes/rule/apis/bulk_edit/transforms/transform_operations/v1.test.ts b/x-pack/plugins/alerting/server/routes/rule/apis/bulk_edit/transforms/transform_operations/v1.test.ts new file mode 100644 index 0000000000000..e186621490dea --- /dev/null +++ b/x-pack/plugins/alerting/server/routes/rule/apis/bulk_edit/transforms/transform_operations/v1.test.ts @@ -0,0 +1,66 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { transformOperations } from './v1'; + +describe('transformOperations', () => { + const isSystemAction = (id: string) => id === 'my-system-action-id'; + + describe('actions', () => { + const defaultAction = { + id: 'default-action', + params: {}, + }; + + const systemAction = { + id: 'my-system-action-id', + params: {}, + }; + + it('transform the actions correctly', async () => { + expect( + transformOperations({ + operations: [ + { field: 'actions', operation: 'add', value: [defaultAction, systemAction] }, + ], + isSystemAction, + }) + ).toEqual([ + { + field: 'actions', + operation: 'add', + value: [ + { + group: 'default', + id: 'default-action', + params: {}, + type: 'default', + }, + { id: 'my-system-action-id', params: {}, type: 'system' }, + ], + }, + ]); + }); + + it('returns an empty array if the operations are empty', async () => { + expect( + transformOperations({ + operations: [], + isSystemAction, + }) + ).toEqual([]); + }); + + it('returns an empty array if the operations are undefined', async () => { + expect( + transformOperations({ + isSystemAction, + }) + ).toEqual([]); + }); + }); +}); diff --git a/x-pack/plugins/alerting/server/routes/rule/apis/bulk_edit/transforms/transform_operations/v1.ts b/x-pack/plugins/alerting/server/routes/rule/apis/bulk_edit/transforms/transform_operations/v1.ts index ef5b2289ccde6..aa88e9423f568 100644 --- a/x-pack/plugins/alerting/server/routes/rule/apis/bulk_edit/transforms/transform_operations/v1.ts +++ b/x-pack/plugins/alerting/server/routes/rule/apis/bulk_edit/transforms/transform_operations/v1.ts @@ -30,7 +30,7 @@ export const transformOperations = ({ return { id: action.id, params: action.params, - uuid: action.uuid, + ...(action.uuid && { frequency: action.uuid }), type: RuleActionTypes.SYSTEM, }; } @@ -40,6 +40,8 @@ export const transformOperations = ({ group: action.group ?? 'default', params: action.params, uuid: action.uuid, + ...(action.frequency && { frequency: action.frequency }), + ...(action.uuid && { frequency: action.uuid }), frequency: action.frequency, type: RuleActionTypes.DEFAULT, }; diff --git a/x-pack/plugins/alerting/server/routes/rule/apis/create/transforms/transform_create_body/v1.ts b/x-pack/plugins/alerting/server/routes/rule/apis/create/transforms/transform_create_body/v1.ts index 4b0cd835450e8..51ccc96d4e9c6 100644 --- a/x-pack/plugins/alerting/server/routes/rule/apis/create/transforms/transform_create_body/v1.ts +++ b/x-pack/plugins/alerting/server/routes/rule/apis/create/transforms/transform_create_body/v1.ts @@ -25,7 +25,7 @@ const transformCreateBodyActions = ( id: action.id, params: action.params, actionTypeId: action.actionTypeId, - uuid: action.uuid, + ...(action.uuid ? { uuid: action.uuid } : {}), type: RuleActionTypes.SYSTEM, }; } diff --git a/x-pack/plugins/alerting/server/rules_client/lib/add_generated_action_values.test.ts b/x-pack/plugins/alerting/server/rules_client/lib/add_generated_action_values.test.ts index 9ae209e5d9f04..bb52ba25e0a4a 100644 --- a/x-pack/plugins/alerting/server/rules_client/lib/add_generated_action_values.test.ts +++ b/x-pack/plugins/alerting/server/rules_client/lib/add_generated_action_values.test.ts @@ -55,6 +55,12 @@ describe('addGeneratedActionValues()', () => { expect(actionWithGeneratedValues[0].uuid).toBe('111-222'); }); + test('adds uuid to a system action', async () => { + const { uuid, ...systemActionWithoutUUID } = systemAction; + const actionWithGeneratedValues = addGeneratedActionValues([systemActionWithoutUUID]); + expect(actionWithGeneratedValues[0].uuid).toBe('111-222'); + }); + test('does not overrides the uuid of a system action', async () => { const actionWithGeneratedValues = addGeneratedActionValues([systemAction]); expect(actionWithGeneratedValues[0].uuid).toBe('my-uid'); diff --git a/x-pack/plugins/alerting/server/rules_client/lib/add_generated_action_values.ts b/x-pack/plugins/alerting/server/rules_client/lib/add_generated_action_values.ts index 0edaf3af64eed..4212cb766f7f5 100644 --- a/x-pack/plugins/alerting/server/rules_client/lib/add_generated_action_values.ts +++ b/x-pack/plugins/alerting/server/rules_client/lib/add_generated_action_values.ts @@ -49,7 +49,6 @@ export function addGeneratedActionValues( }, } : {}), - // TODO: Fix } as NormalizedAlertActionWithGeneratedValues; }); } diff --git a/x-pack/plugins/alerting/server/rules_client/lib/siem_legacy_actions/format_legacy_actions.ts b/x-pack/plugins/alerting/server/rules_client/lib/siem_legacy_actions/format_legacy_actions.ts index b63c489e0b7f0..1b6e7740cd53f 100644 --- a/x-pack/plugins/alerting/server/rules_client/lib/siem_legacy_actions/format_legacy_actions.ts +++ b/x-pack/plugins/alerting/server/rules_client/lib/siem_legacy_actions/format_legacy_actions.ts @@ -31,7 +31,7 @@ interface LegacyGetBulkRuleActionsSavedObject { */ export interface LegacyActionsObj { ruleThrottle: string | null; - legacyRuleActions: Array>; + legacyRuleActions: Array & { group: string }>; } /** @@ -102,7 +102,12 @@ export const legacyGetBulkRuleActionsSavedObject = async ({ ruleAlertIdKey, legacyRawActions, savedObject.references - ) // remove uuid from action, as this uuid is not persistent + ) + /** + * Remove uuid from action, as this uuid is not persistent. + * TS complains about the group being undefined (system actions). + * Legacy actions will have a group. + */ .map(({ uuid, ...action }) => action) as RuleDefaultAction[], }; } else {