Skip to content

Commit

Permalink
[Actions] Taking space id into account when creating email footer link (
Browse files Browse the repository at this point in the history
#100734) (#100913)

* 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 <[email protected]>

Co-authored-by: ymao1 <[email protected]>
  • Loading branch information
kibanamachine and ymao1 authored May 28, 2021
1 parent 3fa7834 commit ce51457
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ export function createExecutionHandler<
.map((action) => ({
...action,
params: injectActionParams({
alertId,
ruleId: alertId,
spaceId,
actionParams: action.params,
actionTypeId: action.actionTypeId,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ describe('injectActionParams', () => {
};
const result = injectActionParams({
actionParams,
alertId: '1',
ruleId: '1',
spaceId: 'the-space',
actionTypeId: '.server-log',
});
expect(result).toMatchInlineSnapshot(`
Expand All @@ -32,7 +33,8 @@ describe('injectActionParams', () => {
};
const result = injectActionParams({
actionParams,
alertId: '1',
ruleId: '1',
spaceId: 'default',
actionTypeId: '.email',
});
expect(result).toMatchInlineSnapshot(`
Expand All @@ -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",
},
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}),
},
};
Expand Down

0 comments on commit ce51457

Please sign in to comment.