Skip to content

Commit

Permalink
[8.7] [Response Ops][Alerting] Editing rule action does not auto-popu…
Browse files Browse the repository at this point in the history
…late default summary message (#151048) (#151134)

# Backport

This will backport the following commits from `main` to `8.7`:
- [[Response Ops][Alerting] Editing rule action does not auto-populate
default summary message
(#151048)](#151048)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Alexi
Doak","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-02-14T13:44:43Z","message":"[Response
Ops][Alerting] Editing rule action does not auto-populate default
summary message (#151048)\n\nResolves
https://github.com/elastic/kibana/issues/150715\r\n\r\n##
Summary\r\n\r\nWhen editing an existing action on an existing rule,
switching between\r\nthe For each alert and Summary alert dropdown does
switch the existing\r\naction message to the default
message.\r\n\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n\r\n### To
verify\r\n\r\n- create a rule with an action that uses `For each alert`
notification\r\nfrequency; use the default message\r\n- edit your new
rule and switch the action frequency to `Summary of\r\nalerts`; notice
that the message does change to the default summary\r\nmessage\r\n-
create a rule with an action that uses `Summary of
alerts`\r\nnotification frequency; use the default message\r\n- edit
your new rule and switch the action frequency to `For each\r\nalert`;
notice that the message does change to the default
summary\r\nmessage\r\n- repeat the steps above and but create the rule a
with an action that\r\ndoes not use the default
message","sha":"96d10f05b325cb2a621b24194f5ebabab578e793","branchLabelMapping":{"^v8.8.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","Feature:Alerting","release_note:skip","Team:ResponseOps","v8.7.0","v8.8.0"],"number":151048,"url":"https://github.com/elastic/kibana/pull/151048","mergeCommit":{"message":"[Response
Ops][Alerting] Editing rule action does not auto-populate default
summary message (#151048)\n\nResolves
https://github.com/elastic/kibana/issues/150715\r\n\r\n##
Summary\r\n\r\nWhen editing an existing action on an existing rule,
switching between\r\nthe For each alert and Summary alert dropdown does
switch the existing\r\naction message to the default
message.\r\n\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n\r\n### To
verify\r\n\r\n- create a rule with an action that uses `For each alert`
notification\r\nfrequency; use the default message\r\n- edit your new
rule and switch the action frequency to `Summary of\r\nalerts`; notice
that the message does change to the default summary\r\nmessage\r\n-
create a rule with an action that uses `Summary of
alerts`\r\nnotification frequency; use the default message\r\n- edit
your new rule and switch the action frequency to `For each\r\nalert`;
notice that the message does change to the default
summary\r\nmessage\r\n- repeat the steps above and but create the rule a
with an action that\r\ndoes not use the default
message","sha":"96d10f05b325cb2a621b24194f5ebabab578e793"}},"sourceBranch":"main","suggestedTargetBranches":["8.7"],"targetPullRequestStates":[{"branch":"8.7","label":"v8.7.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.8.0","labelRegex":"^v8.8.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/151048","number":151048,"mergeCommit":{"message":"[Response
Ops][Alerting] Editing rule action does not auto-populate default
summary message (#151048)\n\nResolves
https://github.com/elastic/kibana/issues/150715\r\n\r\n##
Summary\r\n\r\nWhen editing an existing action on an existing rule,
switching between\r\nthe For each alert and Summary alert dropdown does
switch the existing\r\naction message to the default
message.\r\n\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n\r\n### To
verify\r\n\r\n- create a rule with an action that uses `For each alert`
notification\r\nfrequency; use the default message\r\n- edit your new
rule and switch the action frequency to `Summary of\r\nalerts`; notice
that the message does change to the default summary\r\nmessage\r\n-
create a rule with an action that uses `Summary of
alerts`\r\nnotification frequency; use the default message\r\n- edit
your new rule and switch the action frequency to `For each\r\nalert`;
notice that the message does change to the default
summary\r\nmessage\r\n- repeat the steps above and but create the rule a
with an action that\r\ndoes not use the default
message","sha":"96d10f05b325cb2a621b24194f5ebabab578e793"}}]}]
BACKPORT-->

Co-authored-by: Alexi Doak <[email protected]>
  • Loading branch information
kibanamachine and doakalexi authored Feb 14, 2023
1 parent 6050429 commit 601e718
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,62 @@ describe('EmailParamsFields renders', () => {

expect(editAction).not.toHaveBeenCalledWith('message', 'Some different default message', 0);
});

test('when useDefaultMessage is set to true and the default message changes, the underlying message is replaced with the default message', () => {
const actionParams = {
cc: [],
bcc: [],
to: ['[email protected]'],
subject: 'test',
};

const editAction = jest.fn();
const wrapper = mountWithIntl(
<EmailParamsFields
actionParams={{ ...actionParams, message: 'not the default message' }}
errors={{ to: [], cc: [], bcc: [], subject: [], message: [] }}
editAction={editAction}
defaultMessage={'Some default message'}
index={0}
/>
);
const text = wrapper.find('[data-test-subj="messageTextArea"]').first().text();
expect(text).toEqual('not the default message');

wrapper.setProps({
useDefaultMessage: true,
defaultMessage: 'Some different default message',
});

expect(editAction).toHaveBeenCalledWith('message', 'Some different default message', 0);
});

test('when useDefaultMessage is set to false and the default message changes, the underlying message is not changed', () => {
const actionParams = {
cc: [],
bcc: [],
to: ['[email protected]'],
subject: 'test',
};

const editAction = jest.fn();
const wrapper = mountWithIntl(
<EmailParamsFields
actionParams={{ ...actionParams, message: 'not the default message' }}
errors={{ to: [], cc: [], bcc: [], subject: [], message: [] }}
editAction={editAction}
defaultMessage={'Some default message'}
index={0}
/>
);
const text = wrapper.find('[data-test-subj="messageTextArea"]').first().text();
expect(text).toEqual('not the default message');

wrapper.setProps({
useDefaultMessage: false,
defaultMessage: 'Some different default message',
});

expect(editAction).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const EmailParamsFields = ({
isDisabled,
onBlur = noop,
showEmailSubjectAndMessage = true,
useDefaultMessage,
}: ActionParamsProps<EmailActionParams>) => {
const { to, cc, bcc, subject, message } = actionParams;
const toOptions = to ? to.map((label: string) => ({ label })) : [];
Expand All @@ -42,6 +43,7 @@ export const EmailParamsFields = ({
>([false, defaultMessage]);
useEffect(() => {
if (
useDefaultMessage ||
!actionParams?.message ||
(isUsingDefault &&
actionParams?.message === defaultMessageUsed &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,56 @@ describe('ServerLogParamsFields renders', () => {

expect(editAction).not.toHaveBeenCalledWith('message', 'Some different default message', 0);
});

test('when useDefaultMessage is set to true and the default message changes, the underlying message is replaced with the default message', () => {
const actionParams = {
level: ServerLogLevelOptions.TRACE,
};

const editAction = jest.fn();
const wrapper = mountWithIntl(
<ServerLogParamsFields
actionParams={{ ...actionParams, message: 'not the default message' }}
errors={{ message: [] }}
editAction={editAction}
defaultMessage={'Some default message'}
index={0}
/>
);
const text = wrapper.find('[data-test-subj="messageTextArea"]').first().text();
expect(text).toEqual('not the default message');

wrapper.setProps({
useDefaultMessage: true,
defaultMessage: 'Some different default message',
});

expect(editAction).toHaveBeenCalledWith('message', 'Some different default message', 0);
});

test('when useDefaultMessage is set to false and the default message changes, the underlying message is not changed', () => {
const actionParams = {
level: ServerLogLevelOptions.TRACE,
};

const editAction = jest.fn();
const wrapper = mountWithIntl(
<ServerLogParamsFields
actionParams={{ ...actionParams, message: 'not the default message' }}
errors={{ message: [] }}
editAction={editAction}
defaultMessage={'Some default message'}
index={0}
/>
);
const text = wrapper.find('[data-test-subj="messageTextArea"]').first().text();
expect(text).toEqual('not the default message');

wrapper.setProps({
useDefaultMessage: false,
defaultMessage: 'Some different default message',
});

expect(editAction).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ import { ServerLogActionParams } from '../types';

export const ServerLogParamsFields: React.FunctionComponent<
ActionParamsProps<ServerLogActionParams>
> = ({ actionParams, editAction, index, errors, messageVariables, defaultMessage }) => {
> = ({
actionParams,
editAction,
index,
errors,
messageVariables,
defaultMessage,
useDefaultMessage,
}) => {
const { message, level } = actionParams;
const levelOptions = [
{ value: 'trace', text: 'Trace' },
Expand All @@ -36,6 +44,7 @@ export const ServerLogParamsFields: React.FunctionComponent<
>([false, defaultMessage]);
useEffect(() => {
if (
useDefaultMessage ||
!actionParams?.message ||
(isUsingDefault &&
actionParams?.message === defaultMessageUsed &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,56 @@ describe('SlackParamsFields renders', () => {
'test message'
);
});

test('when useDefaultMessage is set to true and the default message changes, the underlying message is replaced with the default message', () => {
const actionParams = {
message: 'not the default message',
};

const editAction = jest.fn();
const wrapper = mountWithIntl(
<SlackParamsFields
actionParams={actionParams}
errors={{ message: [] }}
editAction={editAction}
defaultMessage={'Some default message'}
index={0}
/>
);
const text = wrapper.find('[data-test-subj="messageTextArea"]').first().text();
expect(text).toEqual('not the default message');

wrapper.setProps({
useDefaultMessage: true,
defaultMessage: 'Some different default message',
});

expect(editAction).toHaveBeenCalledWith('message', 'Some different default message', 0);
});

test('when useDefaultMessage is set to false and the default message changes, the underlying message is not changed', () => {
const actionParams = {
message: 'not the default message',
};

const editAction = jest.fn();
const wrapper = mountWithIntl(
<SlackParamsFields
actionParams={actionParams}
errors={{ message: [] }}
editAction={editAction}
defaultMessage={'Some default message'}
index={0}
/>
);
const text = wrapper.find('[data-test-subj="messageTextArea"]').first().text();
expect(text).toEqual('not the default message');

wrapper.setProps({
useDefaultMessage: false,
defaultMessage: 'Some different default message',
});

expect(editAction).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ const SlackParamsFields: React.FunctionComponent<ActionParamsProps<SlackActionPa
errors,
messageVariables,
defaultMessage,
useDefaultMessage,
}) => {
const { message } = actionParams;
const [[isUsingDefault, defaultMessageUsed], setDefaultMessageUsage] = useState<
[boolean, string | undefined]
>([false, defaultMessage]);
useEffect(() => {
if (
useDefaultMessage ||
!actionParams?.message ||
(isUsingDefault &&
actionParams?.message === defaultMessageUsed &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,56 @@ describe('TeamsParamsFields renders', () => {
'test message'
);
});

test('when useDefaultMessage is set to true and the default message changes, the underlying message is replaced with the default message', () => {
const actionParams = {
message: 'not the default message',
};

const editAction = jest.fn();
const wrapper = mountWithIntl(
<TeamsParamsFields
actionParams={actionParams}
errors={{ message: [] }}
editAction={editAction}
defaultMessage={'Some default message'}
index={0}
/>
);
const text = wrapper.find('[data-test-subj="messageTextArea"]').first().text();
expect(text).toEqual('not the default message');

wrapper.setProps({
useDefaultMessage: true,
defaultMessage: 'Some different default message',
});

expect(editAction).toHaveBeenCalledWith('message', 'Some different default message', 0);
});

test('when useDefaultMessage is set to false and the default message changes, the underlying message is not changed', () => {
const actionParams = {
message: 'not the default message',
};

const editAction = jest.fn();
const wrapper = mountWithIntl(
<TeamsParamsFields
actionParams={actionParams}
errors={{ message: [] }}
editAction={editAction}
defaultMessage={'Some default message'}
index={0}
/>
);
const text = wrapper.find('[data-test-subj="messageTextArea"]').first().text();
expect(text).toEqual('not the default message');

wrapper.setProps({
useDefaultMessage: false,
defaultMessage: 'Some different default message',
});

expect(editAction).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ const TeamsParamsFields: React.FunctionComponent<ActionParamsProps<TeamsActionPa
errors,
messageVariables,
defaultMessage,
useDefaultMessage,
}) => {
const { message } = actionParams;
useEffect(() => {
if (!message && defaultMessage && defaultMessage.length > 0) {
if (useDefaultMessage || (!message && defaultMessage && defaultMessage.length > 0)) {
editAction('message', defaultMessage, index);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [defaultMessage]);

return (
<TextAreaWithMessageVariables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export const ActionTypeForm = ({
-1,
's',
];
const [useDefaultMessage, setUseDefaultMessage] = useState(false);
const isSummaryAction = actionItem.frequency?.summary;

const getDefaultParams = async () => {
Expand Down Expand Up @@ -264,6 +265,8 @@ export const ActionTypeForm = ({
)}
onSummaryChange={useCallback(
(summary: boolean) => {
// use the default message when a user toggles between action frequencies
setUseDefaultMessage(true);
setActionFrequencyProperty('summary', summary, index);
},
[setActionFrequencyProperty, index]
Expand Down Expand Up @@ -387,6 +390,7 @@ export const ActionTypeForm = ({
? defaultSummaryMessage
: selectedActionGroup?.defaultActionMessage ?? defaultActionMessage
}
useDefaultMessage={useDefaultMessage}
actionConnector={actionConnector}
executionMode={ActionConnectorMode.ActionForm}
/>
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/triggers_actions_ui/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ export interface ActionParamsProps<TParams> {
errors: IErrorObject;
messageVariables?: ActionVariable[];
defaultMessage?: string;
useDefaultMessage?: boolean;
actionConnector?: ActionConnector;
isLoading?: boolean;
isDisabled?: boolean;
Expand Down

0 comments on commit 601e718

Please sign in to comment.