diff --git a/x-pack/plugins/alerts/server/task_runner/create_execution_handler.ts b/x-pack/plugins/alerts/server/task_runner/create_execution_handler.ts index f49310c42c247..ccd1f6c20ba52 100644 --- a/x-pack/plugins/alerts/server/task_runner/create_execution_handler.ts +++ b/x-pack/plugins/alerts/server/task_runner/create_execution_handler.ts @@ -75,6 +75,7 @@ export function createExecutionHandler({ spaceId, tags, alertInstanceId, + alertActionGroup: actionGroup, context, actionParams: action.params, state, diff --git a/x-pack/plugins/alerts/server/task_runner/transform_action_params.test.ts b/x-pack/plugins/alerts/server/task_runner/transform_action_params.test.ts index ddbef8e32e708..9a4cfbbca792d 100644 --- a/x-pack/plugins/alerts/server/task_runner/transform_action_params.test.ts +++ b/x-pack/plugins/alerts/server/task_runner/transform_action_params.test.ts @@ -24,6 +24,7 @@ test('skips non string parameters', () => { tags: ['tag-A', 'tag-B'], spaceId: 'spaceId-A', alertInstanceId: '2', + alertActionGroup: 'action-group', alertParams: { foo: 'test', }, @@ -54,6 +55,7 @@ test('missing parameters get emptied out', () => { tags: ['tag-A', 'tag-B'], spaceId: 'spaceId-A', alertInstanceId: '2', + alertActionGroup: 'action-group', alertParams: {}, }); expect(result).toMatchInlineSnapshot(` @@ -77,6 +79,7 @@ test('context parameters are passed to templates', () => { tags: ['tag-A', 'tag-B'], spaceId: 'spaceId-A', alertInstanceId: '2', + alertActionGroup: 'action-group', alertParams: {}, }); expect(result).toMatchInlineSnapshot(` @@ -99,6 +102,7 @@ test('state parameters are passed to templates', () => { tags: ['tag-A', 'tag-B'], spaceId: 'spaceId-A', alertInstanceId: '2', + alertActionGroup: 'action-group', alertParams: {}, }); expect(result).toMatchInlineSnapshot(` @@ -121,6 +125,7 @@ test('alertId is passed to templates', () => { tags: ['tag-A', 'tag-B'], spaceId: 'spaceId-A', alertInstanceId: '2', + alertActionGroup: 'action-group', alertParams: {}, }); expect(result).toMatchInlineSnapshot(` @@ -143,6 +148,7 @@ test('alertName is passed to templates', () => { tags: ['tag-A', 'tag-B'], spaceId: 'spaceId-A', alertInstanceId: '2', + alertActionGroup: 'action-group', alertParams: {}, }); expect(result).toMatchInlineSnapshot(` @@ -165,6 +171,7 @@ test('tags is passed to templates', () => { tags: ['tag-A', 'tag-B'], spaceId: 'spaceId-A', alertInstanceId: '2', + alertActionGroup: 'action-group', alertParams: {}, }); expect(result).toMatchInlineSnapshot(` @@ -186,6 +193,7 @@ test('undefined tags is passed to templates', () => { alertName: 'alert-name', spaceId: 'spaceId-A', alertInstanceId: '2', + alertActionGroup: 'action-group', alertParams: {}, }); expect(result).toMatchInlineSnapshot(` @@ -208,6 +216,7 @@ test('empty tags is passed to templates', () => { tags: [], spaceId: 'spaceId-A', alertInstanceId: '2', + alertActionGroup: 'action-group', alertParams: {}, }); expect(result).toMatchInlineSnapshot(` @@ -230,6 +239,7 @@ test('spaceId is passed to templates', () => { tags: ['tag-A', 'tag-B'], spaceId: 'spaceId-A', alertInstanceId: '2', + alertActionGroup: 'action-group', alertParams: {}, }); expect(result).toMatchInlineSnapshot(` @@ -252,6 +262,7 @@ test('alertInstanceId is passed to templates', () => { tags: ['tag-A', 'tag-B'], spaceId: 'spaceId-A', alertInstanceId: '2', + alertActionGroup: 'action-group', alertParams: {}, }); expect(result).toMatchInlineSnapshot(` @@ -261,6 +272,53 @@ test('alertInstanceId is passed to templates', () => { `); }); +test('alertActionGroup is passed to templates', () => { + const actionParams = { + message: 'Value "{{alertActionGroup}}" exists', + }; + const result = transformActionParams({ + actionParams, + state: {}, + context: {}, + alertId: '1', + alertName: 'alert-name', + tags: ['tag-A', 'tag-B'], + spaceId: 'spaceId-A', + alertInstanceId: '2', + alertActionGroup: 'action-group', + alertParams: {}, + }); + expect(result).toMatchInlineSnapshot(` + Object { + "message": "Value \\"action-group\\" exists", + } + `); +}); + +test('date is passed to templates', () => { + const actionParams = { + message: '{{date}}', + }; + const dateBefore = Date.now(); + const result = transformActionParams({ + actionParams, + state: {}, + context: {}, + alertId: '1', + alertName: 'alert-name', + tags: ['tag-A', 'tag-B'], + spaceId: 'spaceId-A', + alertInstanceId: '2', + alertActionGroup: 'action-group', + alertParams: {}, + }); + const dateAfter = Date.now(); + const dateVariable = new Date(`${result.message}`).valueOf(); + + expect(dateVariable).toBeGreaterThanOrEqual(dateBefore); + expect(dateVariable).toBeLessThanOrEqual(dateAfter); +}); + test('works recursively', () => { const actionParams = { body: { @@ -276,6 +334,7 @@ test('works recursively', () => { tags: ['tag-A', 'tag-B'], spaceId: 'spaceId-A', alertInstanceId: '2', + alertActionGroup: 'action-group', alertParams: {}, }); expect(result).toMatchInlineSnapshot(` @@ -302,6 +361,7 @@ test('works recursively with arrays', () => { tags: ['tag-A', 'tag-B'], spaceId: 'spaceId-A', alertInstanceId: '2', + alertActionGroup: 'action-group', alertParams: {}, }); expect(result).toMatchInlineSnapshot(` diff --git a/x-pack/plugins/alerts/server/task_runner/transform_action_params.ts b/x-pack/plugins/alerts/server/task_runner/transform_action_params.ts index 913fc51cb0f6e..b02285d56aa9a 100644 --- a/x-pack/plugins/alerts/server/task_runner/transform_action_params.ts +++ b/x-pack/plugins/alerts/server/task_runner/transform_action_params.ts @@ -19,6 +19,7 @@ interface TransformActionParamsOptions { spaceId: string; tags?: string[]; alertInstanceId: string; + alertActionGroup: string; actionParams: AlertActionParams; alertParams: AlertTypeParams; state: AlertInstanceState; @@ -31,6 +32,7 @@ export function transformActionParams({ spaceId, tags, alertInstanceId, + alertActionGroup, context, actionParams, state, @@ -48,7 +50,9 @@ export function transformActionParams({ spaceId, tags, alertInstanceId, + alertActionGroup, context, + date: new Date().toISOString(), state, params: alertParams, }; diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_variables.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_variables.test.ts index 6106ba60d994b..6317896a5ecd2 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_variables.test.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_variables.test.ts @@ -31,10 +31,18 @@ describe('transformActionVariables', () => { "description": "The tags of the alert.", "name": "tags", }, + Object { + "description": "The date the alert scheduled the action.", + "name": "date", + }, Object { "description": "The alert instance id that scheduled actions for the alert.", "name": "alertInstanceId", }, + Object { + "description": "The alert action group that was used to scheduled actions for the alert.", + "name": "alertActionGroup", + }, ] `); }); @@ -66,10 +74,18 @@ describe('transformActionVariables', () => { "description": "The tags of the alert.", "name": "tags", }, + Object { + "description": "The date the alert scheduled the action.", + "name": "date", + }, Object { "description": "The alert instance id that scheduled actions for the alert.", "name": "alertInstanceId", }, + Object { + "description": "The alert action group that was used to scheduled actions for the alert.", + "name": "alertActionGroup", + }, Object { "description": "foo-description", "name": "context.foo", @@ -109,10 +125,18 @@ describe('transformActionVariables', () => { "description": "The tags of the alert.", "name": "tags", }, + Object { + "description": "The date the alert scheduled the action.", + "name": "date", + }, Object { "description": "The alert instance id that scheduled actions for the alert.", "name": "alertInstanceId", }, + Object { + "description": "The alert action group that was used to scheduled actions for the alert.", + "name": "alertActionGroup", + }, Object { "description": "foo-description", "name": "state.foo", @@ -155,10 +179,18 @@ describe('transformActionVariables', () => { "description": "The tags of the alert.", "name": "tags", }, + Object { + "description": "The date the alert scheduled the action.", + "name": "date", + }, Object { "description": "The alert instance id that scheduled actions for the alert.", "name": "alertInstanceId", }, + Object { + "description": "The alert action group that was used to scheduled actions for the alert.", + "name": "alertActionGroup", + }, Object { "description": "fooC-description", "name": "context.fooC", diff --git a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_variables.ts b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_variables.ts index 2bdec1bea0c1d..296185211d043 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/lib/action_variables.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/lib/action_variables.ts @@ -58,6 +58,13 @@ function getAlwaysProvidedActionVariables(): ActionVariable[] { }), }); + result.push({ + name: 'date', + description: i18n.translate('xpack.triggersActionsUI.actionVariables.dateLabel', { + defaultMessage: 'The date the alert scheduled the action.', + }), + }); + result.push({ name: 'alertInstanceId', description: i18n.translate('xpack.triggersActionsUI.actionVariables.alertInstanceIdLabel', { @@ -65,5 +72,12 @@ function getAlwaysProvidedActionVariables(): ActionVariable[] { }), }); + result.push({ + name: 'alertActionGroup', + description: i18n.translate('xpack.triggersActionsUI.actionVariables.alertActionGroupLabel', { + defaultMessage: 'The alert action group that was used to scheduled actions for the alert.', + }), + }); + return result; } diff --git a/x-pack/test/alerting_api_integration/common/lib/alert_utils.ts b/x-pack/test/alerting_api_integration/common/lib/alert_utils.ts index 797769fd64471..d1b8e61ff7f8a 100644 --- a/x-pack/test/alerting_api_integration/common/lib/alert_utils.ts +++ b/x-pack/test/alerting_api_integration/common/lib/alert_utils.ts @@ -328,6 +328,7 @@ alertName: {{alertName}}, spaceId: {{spaceId}}, tags: {{tags}}, alertInstanceId: {{alertInstanceId}}, +alertActionGroup: {{alertActionGroup}}, instanceContextValue: {{context.instanceContextValue}}, instanceStateValue: {{state.instanceStateValue}} `.trim(); diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/alerts.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/alerts.ts index 8d8bc066a9b1a..0820b7642e99e 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/alerts.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/alerts.ts @@ -168,6 +168,7 @@ alertName: abc, spaceId: ${space.id}, tags: tag-A,tag-B, alertInstanceId: 1, +alertActionGroup: default, instanceContextValue: true, instanceStateValue: true `.trim(), @@ -282,6 +283,7 @@ alertName: abc, spaceId: ${space.id}, tags: tag-A,tag-B, alertInstanceId: 1, +alertActionGroup: default, instanceContextValue: true, instanceStateValue: true `.trim(), diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/alerts_base.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/alerts_base.ts index 26f52475a2d4e..64e99190e183a 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/alerts_base.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/alerts_base.ts @@ -125,6 +125,7 @@ alertName: abc, spaceId: ${space.id}, tags: tag-A,tag-B, alertInstanceId: 1, +alertActionGroup: default, instanceContextValue: true, instanceStateValue: true `.trim(), diff --git a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/alert_create_flyout.ts b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/alert_create_flyout.ts index 0f6da936f8644..7bcfca50e3c12 100644 --- a/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/alert_create_flyout.ts +++ b/x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/alert_create_flyout.ts @@ -86,14 +86,16 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { await testSubjects.setValue('messageTextArea', 'test message '); await testSubjects.click('messageAddVariableButton'); await testSubjects.click('variableMenuButton-0'); - expect(await messageTextArea.getAttribute('value')).to.eql('test message {{alertId}}'); + expect(await messageTextArea.getAttribute('value')).to.eql( + 'test message {{alertActionGroup}}' + ); await messageTextArea.type(' some additional text '); await testSubjects.click('messageAddVariableButton'); await testSubjects.click('variableMenuButton-1'); expect(await messageTextArea.getAttribute('value')).to.eql( - 'test message {{alertId}} some additional text {{alertInstanceId}}' + 'test message {{alertActionGroup}} some additional text {{alertId}}' ); await testSubjects.click('saveAlertButton');