diff --git a/x-pack/plugins/alerting/server/lib/build_view_in_app_url.test.ts b/x-pack/plugins/alerting/server/lib/build_view_in_app_url.test.ts deleted file mode 100644 index 59137e0cf47e6..0000000000000 --- a/x-pack/plugins/alerting/server/lib/build_view_in_app_url.test.ts +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { loggingSystemMock } from '@kbn/core/server/mocks'; -import { SanitizedRule } from '../../common'; -import { buildViewInAppUrl } from './build_view_in_app_url'; - -const mockedRule: SanitizedRule<{}> = { - id: '1', - alertTypeId: '1', - schedule: { interval: '10s' }, - params: { - bar: true, - }, - createdAt: new Date(), - updatedAt: new Date(), - actions: [ - { - group: 'default', - id: '2', - actionTypeId: 'test', - params: { - foo: true, - }, - }, - ], - consumer: 'bar', - name: 'abc', - tags: ['foo'], - enabled: true, - muteAll: false, - notifyWhen: 'onActionGroupChange', - createdBy: '', - updatedBy: '', - apiKeyOwner: '', - throttle: '30s', - mutedInstanceIds: [], - executionStatus: { - status: 'unknown', - lastExecutionDate: new Date('2020-08-20T19:23:38Z'), - }, -}; - -describe('buildViewInAppUrl', () => { - const defaultOpts = { - kibanaBaseUrl: 'http://localhost:5601', - spaceId: 'default', - getViewInAppRelativeUrl: jest.fn(), - opts: { - rule: mockedRule, - }, - logger: loggingSystemMock.create().get(), - }; - - beforeEach(() => { - defaultOpts.getViewInAppRelativeUrl.mockReset(); - defaultOpts.getViewInAppRelativeUrl.mockReturnValue('/app/foo/123'); - }); - - it(`should return undefined if kibanaBaseUrl isn't defined`, () => { - const result = buildViewInAppUrl({ ...defaultOpts, kibanaBaseUrl: undefined }); - expect(result).toBeUndefined(); - }); - - it(`should return undefined when getViewInAppRelativeUrl function isn't passed in`, () => { - const result = buildViewInAppUrl({ ...defaultOpts, getViewInAppRelativeUrl: undefined }); - expect(result).toBeUndefined(); - }); - - it(`should call getViewInAppRelativeUrl() to get the relative URL`, () => { - buildViewInAppUrl(defaultOpts); - expect(defaultOpts.getViewInAppRelativeUrl).toHaveBeenCalled(); - }); - - it(`should build a URL in the default space properly`, () => { - const result = buildViewInAppUrl(defaultOpts); - expect(result).toEqual('http://localhost:5601/app/foo/123'); - }); - - it(`should build a URL in a specific space properly`, () => { - const result = buildViewInAppUrl({ ...defaultOpts, spaceId: 'my-space' }); - expect(result).toEqual('http://localhost:5601/s/my-space/app/foo/123'); - }); -}); diff --git a/x-pack/plugins/alerting/server/lib/build_view_in_app_url.ts b/x-pack/plugins/alerting/server/lib/build_view_in_app_url.ts deleted file mode 100644 index 60896552e7759..0000000000000 --- a/x-pack/plugins/alerting/server/lib/build_view_in_app_url.ts +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { Logger } from '@kbn/core/server'; -import { SanitizedRule, RuleTypeParams } from '../../common'; - -export interface GetViewInAppRelativeUrlFnOpts { - rule: Omit, 'viewInAppRelativeUrl'>; -} -export type GetViewInAppRelativeUrlFn = ( - opts: GetViewInAppRelativeUrlFnOpts -) => string; - -export interface BuildViewInAppUrlOpts { - kibanaBaseUrl: string | undefined; - spaceId: string; - getViewInAppRelativeUrl: GetViewInAppRelativeUrlFn | undefined; - opts: GetViewInAppRelativeUrlFnOpts; - logger: Logger; -} - -export function buildViewInAppUrl({ - kibanaBaseUrl, - spaceId, - getViewInAppRelativeUrl, - opts, - logger, -}: BuildViewInAppUrlOpts): string | undefined { - if (!kibanaBaseUrl || !getViewInAppRelativeUrl) { - return; - } - - const relativeUrl = getViewInAppRelativeUrl(opts); - - try { - const viewInAppUrl = new URL( - `${kibanaBaseUrl}${spaceId !== 'default' ? `/s/${spaceId}` : ''}${relativeUrl}` - ); - return viewInAppUrl.toString(); - } catch (e) { - logger.debug( - `Rule "${opts.rule.id}" encountered an error while constructing the viewInAppUrl variable: ${e.message}` - ); - return; - } -} diff --git a/x-pack/plugins/alerting/server/lib/index.ts b/x-pack/plugins/alerting/server/lib/index.ts index f794ea8cb0006..8f8dccadaaa77 100644 --- a/x-pack/plugins/alerting/server/lib/index.ts +++ b/x-pack/plugins/alerting/server/lib/index.ts @@ -43,9 +43,3 @@ export { determineAlertsToReturn } from './determine_alerts_to_return'; export { updateFlappingHistory, isFlapping } from './flapping_utils'; export { getAlertsForNotification } from './get_alerts_for_notification'; export { trimRecoveredAlerts } from './trim_recovered_alerts'; -export { buildViewInAppUrl } from './build_view_in_app_url'; -export type { - GetViewInAppRelativeUrlFnOpts, - GetViewInAppRelativeUrlFn, - BuildViewInAppUrlOpts, -} from './build_view_in_app_url'; 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 b41db1147f3b7..cc1741fe722fd 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 @@ -1472,5 +1472,38 @@ describe('Execution Handler', () => { ] `); }); + + it('sets the rule.url to the value from getViewInAppRelativeUrl when the rule type has it defined', async () => { + const execParams = { + ...defaultExecutionParams, + rule: ruleWithUrl, + taskRunnerContext: { + ...defaultExecutionParams.taskRunnerContext, + kibanaBaseUrl: 'http://localhost:12345', + }, + ruleType: { + ...ruleType, + getViewInAppRelativeUrl() { + return '/app/management/some/other/place'; + }, + }, + }; + + const executionHandler = new ExecutionHandler(generateExecutionParams(execParams)); + await executionHandler.run(generateAlert({ id: 1 })); + + expect(injectActionParamsMock.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + Object { + "actionParams": Object { + "val": "rule url: http://localhost:12345/s/test1/app/management/some/other/place", + }, + "actionTypeId": "test", + "ruleId": "1", + "spaceId": "test1", + }, + ] + `); + }); }); }); 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 8a9055477cb1e..9975c8f9e6923 100644 --- a/x-pack/plugins/alerting/server/task_runner/execution_handler.ts +++ b/x-pack/plugins/alerting/server/task_runner/execution_handler.ts @@ -16,7 +16,6 @@ import { chunk } from 'lodash'; import { AlertingEventLogger } from '../lib/alerting_event_logger/alerting_event_logger'; import { parseDuration, RawRule, ThrottledActions } from '../types'; import { RuleRunMetricsStore } from '../lib/rule_run_metrics_store'; -import { buildViewInAppUrl } from '../lib'; import { injectActionParams } from './inject_action_params'; import { ExecutionHandlerOptions, RuleTaskInstance } from './types'; import { TaskRunnerContext } from './task_runner_factory'; @@ -223,14 +222,7 @@ export class ExecutionHandler< actionsPlugin, actionTypeId, kibanaBaseUrl: this.taskRunnerContext.kibanaBaseUrl, - ruleUrl: - buildViewInAppUrl({ - kibanaBaseUrl: this.taskRunnerContext.kibanaBaseUrl, - spaceId, - getViewInAppRelativeUrl: this.ruleType.getViewInAppRelativeUrl, - opts: { rule: this.rule }, - logger: this.logger, - }) || this.buildRuleUrl(spaceId), + ruleUrl: this.buildRuleUrl(spaceId), }), }), }; @@ -279,14 +271,7 @@ export class ExecutionHandler< alertParams: this.rule.params, actionParams: action.params, flapping: executableAlert.getFlapping(), - ruleUrl: - buildViewInAppUrl({ - kibanaBaseUrl: this.taskRunnerContext.kibanaBaseUrl, - spaceId, - getViewInAppRelativeUrl: this.ruleType.getViewInAppRelativeUrl, - opts: { rule: this.rule }, - logger: this.logger, - }) || this.buildRuleUrl(spaceId), + ruleUrl: this.buildRuleUrl(spaceId), }), }), }; @@ -424,11 +409,13 @@ export class ExecutionHandler< return; } + const relativePath = this.ruleType.getViewInAppRelativeUrl + ? this.ruleType.getViewInAppRelativeUrl({ rule: this.rule }) + : `${triggersActionsRoute}${getRuleDetailsRoute(this.rule.id)}`; + try { const ruleUrl = new URL( - `${ - spaceId !== 'default' ? `/s/${spaceId}` : '' - }${triggersActionsRoute}${getRuleDetailsRoute(this.rule.id)}`, + `${spaceId !== 'default' ? `/s/${spaceId}` : ''}${relativePath}`, this.taskRunnerContext.kibanaBaseUrl ); diff --git a/x-pack/plugins/alerting/server/types.ts b/x-pack/plugins/alerting/server/types.ts index f13722c403d18..1ecff391be4af 100644 --- a/x-pack/plugins/alerting/server/types.ts +++ b/x-pack/plugins/alerting/server/types.ts @@ -48,11 +48,11 @@ import { RuleSnooze, IntervalSchedule, RuleLastRun, + SanitizedRule, } from '../common'; import { PublicAlertFactory } from './alert/create_alert_factory'; import { FieldMap } from '../common/alert_schema/field_maps/types'; import { RulesSettingsFlappingProperties } from '../common/rules_settings'; -import { GetViewInAppRelativeUrlFn } from './lib'; export type WithoutQueryAndParams = Pick>; export type SpaceIdToNamespaceFunction = (spaceId?: string) => string | undefined; export type { RuleTypeParams }; @@ -162,6 +162,12 @@ export interface SummarizedAlerts { }; } export type GetSummarizedAlertsFn = (opts: GetSummarizedAlertsFnOpts) => Promise; +export interface GetViewInAppRelativeUrlFnOpts { + rule: Omit, 'viewInAppRelativeUrl'>; +} +export type GetViewInAppRelativeUrlFn = ( + opts: GetViewInAppRelativeUrlFnOpts +) => string; export interface IRuleTypeAlerts { context: string; namespace?: string;