[Actions] Taking space id into account when creating email footer link (#100734)

* 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>
This commit is contained in:
ymao1 2021-05-28 11:56:51 -04:00 committed by GitHub
parent 8fba2d93a6
commit b4e8cfe0d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 10 deletions

View file

@ -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',

View file

@ -138,7 +138,8 @@ export function createExecutionHandler<
.map((action) => ({
...action,
params: injectActionParams({
alertId,
ruleId: alertId,
spaceId,
actionParams: action.params,
actionTypeId: action.actionTypeId,
}),

View file

@ -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",
},
}
`);

View file

@ -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',
}),
},
};