From 5e0cdb191484c9fdd72341ad71589f20e7323e46 Mon Sep 17 00:00:00 2001 From: Antonio Date: Thu, 5 Dec 2024 18:46:59 +0100 Subject: [PATCH] [8.17] [ResponseOps][Rules] Hide the "Role visibility" dropdown in the new rule form in serverless (#200727) (#203126) # Backport This will backport the following commits from `main` to `8.17`: - [[ResponseOps][Rules] Hide the "Role visibility" dropdown in the new rule form in serverless (#200727)](https://github.com/elastic/kibana/pull/200727) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) \n\n### Questions ?\nPlease refer to the [Backport tool\ndocumentation](https://github.com/sqren/backport)\n\n\n\nCo-authored-by: Antonio "}},{"branch":"8.16","label":"v8.16.2","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> --- .../rule_definition/rule_definition.test.tsx | 28 +++++++++++++++++++ .../rule_definition/rule_definition.tsx | 25 +++++++++-------- 2 files changed, 42 insertions(+), 11 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..db72e3369747b 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,27 +27,27 @@ 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 { ALERTING_FEATURE_ID, MULTI_CONSUMER_RULE_TYPE_IDS } from '../constants'; +import { useRuleFormDispatch, useRuleFormState } from '../hooks'; 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'; import { RuleAlertDelay } from './rule_alert_delay'; import { RuleConsumerSelection } from './rule_consumer_selection'; import { RuleSchedule } from './rule_schedule'; -import { useRuleFormState, useRuleFormDispatch } from '../hooks'; -import { ALERTING_FEATURE_ID, MULTI_CONSUMER_RULE_TYPE_IDS } from '../constants'; import { getAuthorizedConsumers } from '../utils'; import { RuleSettingsFlappingTitleTooltip } from '../../rule_settings/rule_settings_flapping_title_tooltip'; import { RuleSettingsFlappingForm } from '../../rule_settings/rule_settings_flapping_form'; @@ -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]);