From 53f30cdbe05a34d19818458ea56b1e86f8fe5baa Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Wed, 21 Jun 2023 20:58:00 +0200 Subject: [PATCH] use rule.url to generate kibanaFooterLink --- .../task_runner/execution_handler.test.ts | 26 +++++--------- .../server/task_runner/execution_handler.ts | 12 +++---- .../task_runner/inject_action_params.test.ts | 36 ++----------------- .../task_runner/inject_action_params.ts | 16 +++++---- 4 files changed, 26 insertions(+), 64 deletions(-) diff --git a/x-pack/plugins/alerting/server/task_runner/execution_handler.test.ts b/x-pack/plugins/alerting/server/task_runner/execution_handler.test.ts index 85fbb547408b1..2eacd29a39db6 100644 --- a/x-pack/plugins/alerting/server/task_runner/execution_handler.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/execution_handler.test.ts @@ -274,8 +274,6 @@ describe('Execution Handler', () => { }); expect(jest.requireMock('./inject_action_params').injectActionParams).toHaveBeenCalledWith({ - ruleId: '1', - spaceId: 'test1', actionTypeId: 'test', actionParams: { alertVal: 'My 1 name-of-alert test1 tag-A,tag-B 1 goes here', @@ -1642,8 +1640,7 @@ describe('Execution Handler', () => { "val": "rule url: http://localhost:12345/s/test1/app/management/insightsAndAlerting/triggersActions/rule/1", }, "actionTypeId": "test", - "ruleId": "1", - "spaceId": "test1", + "ruleUrl": "http://localhost:12345/s/test1/app/management/insightsAndAlerting/triggersActions/rule/1", }, ] `); @@ -1709,8 +1706,7 @@ describe('Execution Handler', () => { "val": "rule url: http://localhost:12345/s/test1/app/test/rule/1?start=30000&end=90000", }, "actionTypeId": "test", - "ruleId": "1", - "spaceId": "test1", + "ruleUrl": "http://localhost:12345/s/test1/app/test/rule/1?start=30000&end=90000", }, ] `); @@ -1739,8 +1735,7 @@ describe('Execution Handler', () => { "val": "rule url: http://localhost:12345/app/management/insightsAndAlerting/triggersActions/rule/1", }, "actionTypeId": "test", - "ruleId": "1", - "spaceId": "default", + "ruleUrl": "http://localhost:12345/app/management/insightsAndAlerting/triggersActions/rule/1", }, ] `); @@ -1766,8 +1761,7 @@ describe('Execution Handler', () => { "val": "rule url: http://localhost:12345/s/test1/app/management/insightsAndAlerting/triggersActions/rule/1", }, "actionTypeId": "test", - "ruleId": "1", - "spaceId": "test1", + "ruleUrl": "http://localhost:12345/s/test1/app/management/insightsAndAlerting/triggersActions/rule/1", }, ] `); @@ -1793,8 +1787,7 @@ describe('Execution Handler', () => { "val": "rule url: ", }, "actionTypeId": "test", - "ruleId": "1", - "spaceId": "test1", + "ruleUrl": undefined, }, ] `); @@ -1823,8 +1816,7 @@ describe('Execution Handler', () => { "val": "rule url: ", }, "actionTypeId": "test", - "ruleId": "1", - "spaceId": "test1", + "ruleUrl": undefined, }, ] `); @@ -1853,8 +1845,7 @@ describe('Execution Handler', () => { "val": "rule url: ", }, "actionTypeId": "test", - "ruleId": "1", - "spaceId": "test1", + "ruleUrl": undefined, }, ] `); @@ -1886,8 +1877,7 @@ describe('Execution Handler', () => { "val": "rule url: http://localhost:12345/s/test1/app/management/some/other/place", }, "actionTypeId": "test", - "ruleId": "1", - "spaceId": "test1", + "ruleUrl": "http://localhost:12345/s/test1/app/management/some/other/place", }, ] `); diff --git a/x-pack/plugins/alerting/server/task_runner/execution_handler.ts b/x-pack/plugins/alerting/server/task_runner/execution_handler.ts index b19e31b0c97d2..d091608c10f4f 100644 --- a/x-pack/plugins/alerting/server/task_runner/execution_handler.ts +++ b/x-pack/plugins/alerting/server/task_runner/execution_handler.ts @@ -220,12 +220,12 @@ export class ExecutionHandler< this.rule.schedule, this.previousStartedAt ); + const ruleUrl = this.buildRuleUrl(spaceId, start, end); const actionToRun = { ...action, params: injectActionParams({ - ruleId, - spaceId, actionTypeId, + ruleUrl, actionParams: transformSummaryActionParams({ alerts: summarizedAlerts, rule: this.rule, @@ -236,7 +236,7 @@ export class ExecutionHandler< actionsPlugin, actionTypeId, kibanaBaseUrl: this.taskRunnerContext.kibanaBaseUrl, - ruleUrl: this.buildRuleUrl(spaceId, start, end), + ruleUrl, }), }), }; @@ -261,12 +261,12 @@ export class ExecutionHandler< }); } else { const executableAlert = alert!; + const ruleUrl = this.buildRuleUrl(spaceId); const actionToRun = { ...action, params: injectActionParams({ - ruleId, - spaceId, actionTypeId, + ruleUrl, actionParams: transformActionParams({ actionsPlugin, alertId: ruleId, @@ -286,7 +286,7 @@ export class ExecutionHandler< alertParams: this.rule.params, actionParams: action.params, flapping: executableAlert.getFlapping(), - ruleUrl: this.buildRuleUrl(spaceId), + ruleUrl, }), }), }; 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 0416a3c4d1214..f34a5b0204357 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,8 +14,6 @@ describe('injectActionParams', () => { }; const result = injectActionParams({ actionParams, - ruleId: '1', - spaceId: 'the-space', actionTypeId: '.server-log', }); expect(result).toMatchInlineSnapshot(` @@ -33,9 +31,8 @@ describe('injectActionParams', () => { }; const result = injectActionParams({ actionParams, - ruleId: '1', - spaceId: 'default', actionTypeId: '.email', + ruleUrl: 'http://localhost:5601/app/management/insightsAndAlerting/triggersActions/rule/1', }); expect(result).toMatchInlineSnapshot(` Object { @@ -50,7 +47,7 @@ describe('injectActionParams', () => { `); }); - test('injects viewInKibanaPath and viewInKibanaText when actionTypeId is .email and spaceId is undefined', () => { + test('injects viewInKibanaPath as empty string when the ruleUrl is undefined', () => { const actionParams = { body: { message: 'State: "{{state.value}}", Context: "{{context.value}}"', @@ -58,8 +55,6 @@ describe('injectActionParams', () => { }; const result = injectActionParams({ actionParams, - ruleId: '1', - spaceId: undefined, actionTypeId: '.email', }); expect(result).toMatchInlineSnapshot(` @@ -68,32 +63,7 @@ describe('injectActionParams', () => { "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", + "path": "", "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 f2a41038e257e..e6f7e889a926c 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,27 +9,29 @@ import { i18n } from '@kbn/i18n'; import { RuleActionParams } from '../types'; export interface InjectActionParamsOpts { - ruleId: string; - spaceId: string | undefined; actionTypeId: string; actionParams: RuleActionParams; + ruleUrl?: string; } export function injectActionParams({ - ruleId, - spaceId, actionTypeId, actionParams, + ruleUrl = '', }: 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}` : ''; + let path; + try { + path = new URL(ruleUrl).pathname; + } catch (e) { + path = ''; + } return { ...actionParams, kibanaFooterLink: { - path: `${spacePrefix}/app/management/insightsAndAlerting/triggersActions/rule/${ruleId}`, + path, text: i18n.translate('xpack.alerting.injectActionParams.email.kibanaFooterLinkText', { defaultMessage: 'View rule in Kibana', }),