From 5e55144c4bb469ad5be454205b1c83d234a25b2a Mon Sep 17 00:00:00 2001 From: Antonio Date: Thu, 5 Dec 2024 12:20:37 +0100 Subject: [PATCH] [ResponseOps][Rules] Hide the "Role visibility" dropdown in the new rule form in serverless (#200727) Fixes #199642 ## Summary ~~This PR hides the role visibility dropdown in the new rule form when in serverless.~~ This PR hides the role visibility dropdown in the new rule form **when only one consumer is available**. ## How to test 1. Run Kibana security serverless and confirm the rules in stack management do not have the role visibility dropdown. 2. Please also make sure that the drop-down still shows when needed(outside of serverless). (cherry picked from commit 7498ab00618baf5d5d30d32b989d4ba93da2803e) # Conflicts: # packages/kbn-alerts-ui-shared/src/rule_form/rule_definition/rule_definition.tsx --- .../rule_definition/rule_definition.test.tsx | 28 +++++++++++++++++++ .../rule_definition/rule_definition.tsx | 21 ++++++++------ 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/packages/kbn-alerts-ui-shared/src/rule_form/rule_definition/rule_definition.test.tsx b/packages/kbn-alerts-ui-shared/src/rule_form/rule_definition/rule_definition.test.tsx index f45d1d0ae00fc..21cb7b5c7cd9d 100644 --- a/packages/kbn-alerts-ui-shared/src/rule_form/rule_definition/rule_definition.test.tsx +++ b/packages/kbn-alerts-ui-shared/src/rule_form/rule_definition/rule_definition.test.tsx @@ -236,6 +236,34 @@ describe('Rule Definition', () => { expect(screen.queryByTestId('ruleConsumerSelection')).not.toBeInTheDocument(); }); + test('Hides consumer selection if there are irrelevant consumers and only 1 consumer to select', () => { + useRuleFormState.mockReturnValue({ + plugins, + formData: { + id: 'test-id', + params: {}, + schedule: { + interval: '1m', + }, + alertDelay: { + active: 5, + }, + notifyWhen: null, + consumer: 'stackAlerts', + ruleTypeId: '.es-query', + }, + selectedRuleType: ruleType, + selectedRuleTypeModel: ruleModel, + availableRuleTypes: [ruleType], + canShowConsumerSelect: true, + validConsumers: ['logs', 'observability'], + }); + + render(); + + expect(screen.queryByTestId('ruleConsumerSelection')).not.toBeInTheDocument(); + }); + test('Hides consumer selection if valid consumers contain observability', () => { useRuleFormState.mockReturnValue({ plugins, diff --git a/packages/kbn-alerts-ui-shared/src/rule_form/rule_definition/rule_definition.tsx b/packages/kbn-alerts-ui-shared/src/rule_form/rule_definition/rule_definition.tsx index 997e666e8340f..a90febd7598a9 100644 --- a/packages/kbn-alerts-ui-shared/src/rule_form/rule_definition/rule_definition.tsx +++ b/packages/kbn-alerts-ui-shared/src/rule_form/rule_definition/rule_definition.tsx @@ -27,19 +27,19 @@ import { } from '@elastic/eui'; import { RuleSpecificFlappingProperties } from '@kbn/alerting-types'; import { EuiThemeProvider } from '@kbn/kibana-react-plugin/common'; -import { AlertConsumers } from '@kbn/rule-data-utils'; import { DOC_LINK_TITLE, LOADING_RULE_TYPE_PARAMS_TITLE, SCHEDULE_TITLE, SCHEDULE_DESCRIPTION_TEXT, SCHEDULE_TOOLTIP_TEXT, - ALERT_DELAY_TITLE, SCOPE_TITLE, SCOPE_DESCRIPTION_TEXT, ADVANCED_OPTIONS_TITLE, ALERT_DELAY_DESCRIPTION_TEXT, ALERT_DELAY_HELP_TEXT, + ALERT_DELAY_TITLE, + FEATURE_NAME_MAP, ALERT_FLAPPING_DETECTION_TITLE, ALERT_FLAPPING_DETECTION_DESCRIPTION, } from '../translations'; @@ -114,15 +114,18 @@ export const RuleDefinition = () => { if (!canShowConsumerSelection) { return false; } - if (!authorizedConsumers.length) { - return false; - } - if ( - authorizedConsumers.length <= 1 || - authorizedConsumers.includes(AlertConsumers.OBSERVABILITY) - ) { + + /* + * This will filter out values like 'alerts' and 'observability' that will not be displayed + * in the drop down. It will allow us to hide the consumer select when there is only one + * selectable value. + */ + const authorizedValidConsumers = authorizedConsumers.filter((c) => c in FEATURE_NAME_MAP); + + if (authorizedValidConsumers.length <= 1) { return false; } + return !!(ruleTypeId && MULTI_CONSUMER_RULE_TYPE_IDS.includes(ruleTypeId)); }, [ruleTypeId, authorizedConsumers, canShowConsumerSelection]);