From b89efdbd33fef1145a9ba37770ae40c169fa9fd2 Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Thu, 8 Apr 2021 20:09:26 -0700 Subject: [PATCH] [Actions UI] Changed PagerDuty action form UI to fill payload fields according to the API docs for Resolve and Acknowledge events. (#96363) (#96677) * [Actions UI] Changed PagerDuty action form UI to fill payload fields according to the API docs for Resolve and Acknowledge events. * fixed test * fixed test Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../pagerduty/pagerduty.tsx | 6 +- .../pagerduty/pagerduty_params.test.tsx | 50 +++- .../pagerduty/pagerduty_params.tsx | 257 +++++++++--------- .../action_type_form.tsx | 11 + 4 files changed, 196 insertions(+), 128 deletions(-) diff --git a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/pagerduty/pagerduty.tsx b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/pagerduty/pagerduty.tsx index f3dbfc9c363cc..cae4221e5d7ce 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/pagerduty/pagerduty.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/pagerduty/pagerduty.tsx @@ -18,6 +18,7 @@ import { PagerDutyConfig, PagerDutySecrets, PagerDutyActionParams, + EventActionOptions, } from '.././types'; import pagerDutySvg from './pagerduty.svg'; import { hasMustacheTokens } from '../../../lib/has_mustache_tokens'; @@ -88,7 +89,10 @@ export function getActionType(): ActionTypeModel< ) ); } - if (!actionParams.summary?.length) { + if ( + actionParams.eventAction === EventActionOptions.TRIGGER && + !actionParams.summary?.length + ) { errors.summary.push( i18n.translate( 'xpack.triggersActionsUI.components.builtinActionTypes.pagerDutyAction.error.requiredSummaryText', diff --git a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/pagerduty/pagerduty_params.test.tsx b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/pagerduty/pagerduty_params.test.tsx index 4d47cbf3685a1..f67267a75ed33 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/pagerduty/pagerduty_params.test.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/pagerduty/pagerduty_params.test.tsx @@ -57,9 +57,29 @@ describe('PagerDutyParamsFields renders', () => { expect(wrapper.find('[data-test-subj="dedupKeyAddVariableButton"]').length > 0).toBeTruthy(); }); - test('params select fields dont auto set values ', () => { + test('params select fields do not auto set values eventActionSelect', () => { const actionParams = {}; + const wrapper = mountWithIntl( + {}} + index={0} + /> + ); + expect(wrapper.find('[data-test-subj="eventActionSelect"]').length > 0).toBeTruthy(); + expect( + wrapper.find('[data-test-subj="eventActionSelect"]').first().prop('value') + ).toStrictEqual(undefined); + }); + + test('params select fields do not auto set values severitySelect', () => { + const actionParams = { + eventAction: EventActionOptions.TRIGGER, + dedupKey: 'test', + }; + const wrapper = mountWithIntl( { expect(wrapper.find('[data-test-subj="severitySelect"]').first().prop('value')).toStrictEqual( undefined ); + }); + + test('only eventActionSelect is available as a payload params for PagerDuty Resolve event', () => { + const actionParams = { + eventAction: EventActionOptions.RESOLVE, + dedupKey: 'test', + }; + + const wrapper = mountWithIntl( + {}} + index={0} + /> + ); + expect(wrapper.find('[data-test-subj="dedupKeyInput"]').length > 0).toBeTruthy(); + expect(wrapper.find('[data-test-subj="dedupKeyInput"]').first().prop('value')).toStrictEqual( + 'test' + ); expect(wrapper.find('[data-test-subj="eventActionSelect"]').length > 0).toBeTruthy(); expect( wrapper.find('[data-test-subj="eventActionSelect"]').first().prop('value') - ).toStrictEqual(undefined); + ).toStrictEqual('resolve'); + expect(wrapper.find('[data-test-subj="dedupKeyInput"]').length > 0).toBeTruthy(); + expect(wrapper.find('[data-test-subj="timestampInput"]').length > 0).toBeFalsy(); + expect(wrapper.find('[data-test-subj="componentInput"]').length > 0).toBeFalsy(); + expect(wrapper.find('[data-test-subj="groupInput"]').length > 0).toBeFalsy(); + expect(wrapper.find('[data-test-subj="sourceInput"]').length > 0).toBeFalsy(); + expect(wrapper.find('[data-test-subj="summaryInput"]').length > 0).toBeFalsy(); }); }); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/pagerduty/pagerduty_params.tsx b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/pagerduty/pagerduty_params.tsx index 6923c8dac0001..98dd9c6bf8431 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/pagerduty/pagerduty_params.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/pagerduty/pagerduty_params.tsx @@ -99,32 +99,11 @@ const PagerDutyParamsFields: React.FunctionComponent - - - { - editAction('severity', e.target.value, index); - }} - /> - - - - 0 && summary !== undefined} - label={i18n.translate( - 'xpack.triggersActionsUI.components.builtinActionTypes.pagerDutyAction.summaryFieldLabel', - { - defaultMessage: 'Summary', - } - )} - > - - - - + + {isTriggerPagerDutyEvent ? ( + <> + 0 && timestamp !== undefined} + error={errors.summary} + isInvalid={errors.summary.length > 0 && summary !== undefined} label={i18n.translate( - 'xpack.triggersActionsUI.components.builtinActionTypes.pagerDutyAction.timestampTextFieldLabel', + 'xpack.triggersActionsUI.components.builtinActionTypes.pagerDutyAction.summaryFieldLabel', { - defaultMessage: 'Timestamp (optional)', + defaultMessage: 'Summary', } )} > @@ -218,83 +178,130 @@ const PagerDutyParamsFields: React.FunctionComponent - - - - - - - - - - - - - - - + + + + + { + editAction('severity', e.target.value, index); + }} + /> + + + + 0 && timestamp !== undefined} + label={i18n.translate( + 'xpack.triggersActionsUI.components.builtinActionTypes.pagerDutyAction.timestampTextFieldLabel', + { + defaultMessage: 'Timestamp (optional)', + } + )} + > + + + + + + + + + + + + + + + + + + + ) : null} ); }; diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/action_connector_form/action_type_form.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/action_connector_form/action_type_form.tsx index 7fbe68776ca28..48c6c1b42d7af 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/action_connector_form/action_type_form.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/action_connector_form/action_type_form.tsx @@ -105,6 +105,7 @@ export const ActionTypeForm = ({ const defaultActionGroup = actionGroups?.find(({ id }) => id === defaultActionGroupId); const selectedActionGroup = actionGroups?.find(({ id }) => id === actionItem.group) ?? defaultActionGroup; + const [actionGroup, setActionGroup] = useState(); useEffect(() => { setAvailableActionVariables( @@ -120,6 +121,15 @@ export const ActionTypeForm = ({ // eslint-disable-next-line react-hooks/exhaustive-deps }, [actionItem.group]); + useEffect(() => { + if (defaultParams && actionGroup) { + for (const [key, paramValue] of Object.entries(defaultParams)) { + setActionParamsProperty(key, paramValue, index); + } + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [actionGroup]); + const canSave = hasSaveActionsCapability(capabilities); const getSelectedOptions = (actionItemId: string) => { const selectedConnector = connectors.find((connector) => connector.id === actionItemId); @@ -223,6 +233,7 @@ export const ActionTypeForm = ({ valueOfSelected={selectedActionGroup.id} onChange={(group) => { setActionGroupIdByIndex(group, index); + setActionGroup(group); }} />