Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Alerting] Use rule.url to generate kibanaFooterLink for email connectors #160196

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 = '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the email connector:

return i18n.translate('xpack.stackConnectors.email.customViewInKibanaMessage', {
defaultMessage: 'This message was sent by Elastic. [{kibanaFooterLinkText}]({link}).',
values: {
kibanaFooterLinkText: kibanaFooterLink.text,
link: `${publicBaseUrl}${kibanaFooterLink.path === '/' ? '' : kibanaFooterLink.path}`,
},
});
}

it does this check link: ${publicBaseUrl}${kibanaFooterLink.path === '/' ? '' : kibanaFooterLink.path}

so should we set path to / in this error scenario?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes i saw it but "/" becomes "" anyway, and if we use it somewhere else without checking the trailing "/" the full URL becomes broken.

}
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