Skip to content

Commit

Permalink
[Alerting] Use rule.url to generate kibanaFooterLink for email connec…
Browse files Browse the repository at this point in the history
…tors (#160196)

Resolves: #151355

This PR intends to use `rule.url` (provided by the rule type) in the
email connector's footer link rather than the hardcoded path.
  • Loading branch information
ersin-erdal authored Jun 29, 2023
1 parent 4ee1874 commit e9ad380
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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",
},
]
`);
Expand Down Expand Up @@ -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",
},
]
`);
Expand Down Expand Up @@ -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",
},
]
`);
Expand All @@ -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",
},
]
`);
Expand All @@ -1793,8 +1787,7 @@ describe('Execution Handler', () => {
"val": "rule url: ",
},
"actionTypeId": "test",
"ruleId": "1",
"spaceId": "test1",
"ruleUrl": undefined,
},
]
`);
Expand Down Expand Up @@ -1823,8 +1816,7 @@ describe('Execution Handler', () => {
"val": "rule url: ",
},
"actionTypeId": "test",
"ruleId": "1",
"spaceId": "test1",
"ruleUrl": undefined,
},
]
`);
Expand Down Expand Up @@ -1853,8 +1845,7 @@ describe('Execution Handler', () => {
"val": "rule url: ",
},
"actionTypeId": "test",
"ruleId": "1",
"spaceId": "test1",
"ruleUrl": undefined,
},
]
`);
Expand Down Expand Up @@ -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",
},
]
`);
Expand Down
12 changes: 6 additions & 6 deletions x-pack/plugins/alerting/server/task_runner/execution_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -236,7 +236,7 @@ export class ExecutionHandler<
actionsPlugin,
actionTypeId,
kibanaBaseUrl: this.taskRunnerContext.kibanaBaseUrl,
ruleUrl: this.buildRuleUrl(spaceId, start, end),
ruleUrl,
}),
}),
};
Expand All @@ -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,
Expand All @@ -286,7 +286,7 @@ export class ExecutionHandler<
alertParams: this.rule.params,
actionParams: action.params,
flapping: executableAlert.getFlapping(),
ruleUrl: this.buildRuleUrl(spaceId),
ruleUrl,
}),
}),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ describe('injectActionParams', () => {
};
const result = injectActionParams({
actionParams,
ruleId: '1',
spaceId: 'the-space',
actionTypeId: '.server-log',
});
expect(result).toMatchInlineSnapshot(`
Expand All @@ -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 {
Expand All @@ -50,16 +47,14 @@ 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}}"',
},
};
const result = injectActionParams({
actionParams,
ruleId: '1',
spaceId: undefined,
actionTypeId: '.email',
});
expect(result).toMatchInlineSnapshot(`
Expand All @@ -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",
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}),
Expand Down

0 comments on commit e9ad380

Please sign in to comment.