From ce5145748e70b6621ac02c5b58ffe56f71a76115 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Fri, 28 May 2021 13:54:32 -0400 Subject: [PATCH] [Actions] Taking space id into account when creating email footer link (#100734) (#100913) * Taking space id into account when creating email footer link * Handling undefined space when spaces is disabled * Handling undefined space when spaces is disabled Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: ymao1 --- .../create_execution_handler.test.ts | 3 +- .../task_runner/create_execution_handler.ts | 3 +- .../task_runner/inject_action_params.test.ts | 60 +++++++++++++++++-- .../task_runner/inject_action_params.ts | 12 ++-- 4 files changed, 68 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/alerting/server/task_runner/create_execution_handler.test.ts b/x-pack/plugins/alerting/server/task_runner/create_execution_handler.test.ts index 120ab6de296dd8..78d74b78c99ba6 100644 --- a/x-pack/plugins/alerting/server/task_runner/create_execution_handler.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/create_execution_handler.test.ts @@ -180,7 +180,8 @@ test('enqueues execution per selected action', async () => { `); expect(jest.requireMock('./inject_action_params').injectActionParams).toHaveBeenCalledWith({ - alertId: '1', + ruleId: '1', + spaceId: 'default', actionTypeId: 'test', actionParams: { alertVal: 'My 1 name-of-alert default tag-A,tag-B 2 goes here', diff --git a/x-pack/plugins/alerting/server/task_runner/create_execution_handler.ts b/x-pack/plugins/alerting/server/task_runner/create_execution_handler.ts index 2ecf5404856954..93cced2043d5e1 100644 --- a/x-pack/plugins/alerting/server/task_runner/create_execution_handler.ts +++ b/x-pack/plugins/alerting/server/task_runner/create_execution_handler.ts @@ -138,7 +138,8 @@ export function createExecutionHandler< .map((action) => ({ ...action, params: injectActionParams({ - alertId, + ruleId: alertId, + spaceId, actionParams: action.params, actionTypeId: action.actionTypeId, }), diff --git a/x-pack/plugins/alerting/server/task_runner/inject_action_params.test.ts b/x-pack/plugins/alerting/server/task_runner/inject_action_params.test.ts index 62d834eb91da0d..0416a3c4d1214a 100644 --- a/x-pack/plugins/alerting/server/task_runner/inject_action_params.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/inject_action_params.test.ts @@ -14,7 +14,8 @@ describe('injectActionParams', () => { }; const result = injectActionParams({ actionParams, - alertId: '1', + ruleId: '1', + spaceId: 'the-space', actionTypeId: '.server-log', }); expect(result).toMatchInlineSnapshot(` @@ -32,7 +33,8 @@ describe('injectActionParams', () => { }; const result = injectActionParams({ actionParams, - alertId: '1', + ruleId: '1', + spaceId: 'default', actionTypeId: '.email', }); expect(result).toMatchInlineSnapshot(` @@ -41,8 +43,58 @@ describe('injectActionParams', () => { "message": "State: \\"{{state.value}}\\", Context: \\"{{context.value}}\\"", }, "kibanaFooterLink": Object { - "path": "/app/management/insightsAndAlerting/triggersActions/alert/1", - "text": "View alert in Kibana", + "path": "/app/management/insightsAndAlerting/triggersActions/rule/1", + "text": "View rule in Kibana", + }, + } + `); + }); + + test('injects viewInKibanaPath and viewInKibanaText when actionTypeId is .email and spaceId is undefined', () => { + const actionParams = { + body: { + message: 'State: "{{state.value}}", Context: "{{context.value}}"', + }, + }; + const result = injectActionParams({ + actionParams, + ruleId: '1', + spaceId: undefined, + actionTypeId: '.email', + }); + expect(result).toMatchInlineSnapshot(` + Object { + "body": Object { + "message": "State: \\"{{state.value}}\\", Context: \\"{{context.value}}\\"", + }, + "kibanaFooterLink": Object { + "path": "/app/management/insightsAndAlerting/triggersActions/rule/1", + "text": "View rule in Kibana", + }, + } + `); + }); + + test('injects viewInKibanaPath with space ID and viewInKibanaText when actionTypeId is .email', () => { + const actionParams = { + body: { + message: 'State: "{{state.value}}", Context: "{{context.value}}"', + }, + }; + const result = injectActionParams({ + actionParams, + ruleId: '1', + spaceId: 'not-the-default', + actionTypeId: '.email', + }); + expect(result).toMatchInlineSnapshot(` + Object { + "body": Object { + "message": "State: \\"{{state.value}}\\", Context: \\"{{context.value}}\\"", + }, + "kibanaFooterLink": Object { + "path": "/s/not-the-default/app/management/insightsAndAlerting/triggersActions/rule/1", + "text": "View rule in Kibana", }, } `); diff --git a/x-pack/plugins/alerting/server/task_runner/inject_action_params.ts b/x-pack/plugins/alerting/server/task_runner/inject_action_params.ts index 177622867565cd..11ac3f92d1071b 100644 --- a/x-pack/plugins/alerting/server/task_runner/inject_action_params.ts +++ b/x-pack/plugins/alerting/server/task_runner/inject_action_params.ts @@ -9,25 +9,29 @@ import { i18n } from '@kbn/i18n'; import { AlertActionParams } from '../types'; export interface InjectActionParamsOpts { - alertId: string; + ruleId: string; + spaceId: string | undefined; actionTypeId: string; actionParams: AlertActionParams; } export function injectActionParams({ - alertId, + ruleId, + spaceId, actionTypeId, actionParams, }: InjectActionParamsOpts) { // Inject kibanaFooterLink if action type is email. This is used by the email action type // to inject a "View alert in Kibana" with a URL in the email's footer. if (actionTypeId === '.email') { + const spacePrefix = + spaceId && spaceId.length > 0 && spaceId !== 'default' ? `/s/${spaceId}` : ''; return { ...actionParams, kibanaFooterLink: { - path: `/app/management/insightsAndAlerting/triggersActions/alert/${alertId}`, + path: `${spacePrefix}/app/management/insightsAndAlerting/triggersActions/rule/${ruleId}`, text: i18n.translate('xpack.alerting.injectActionParams.email.kibanaFooterLinkText', { - defaultMessage: 'View alert in Kibana', + defaultMessage: 'View rule in Kibana', }), }, };