Skip to content

Commit 2459c68

Browse files
[7.x] [Alerting] make actionGroup name's i18n-able (#57404) (#57505)
* [Alerting] make actionGroup name's i18n-able (#57404) We want to make the Action Group i18n-able for display in the AlertDetails page, so instead of just a list of ids, the AlertType now registers an object where key is the id and value is the human readable, and translatable, value. * fixed issue introduced by merge conflict * fixed merge conflict Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent b1e03cc commit 2459c68

File tree

18 files changed

+95
-45
lines changed

18 files changed

+95
-45
lines changed

x-pack/legacy/plugins/alerting/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ The following table describes the properties of the `options` object.
8585
|---|---|---|
8686
|id|Unique identifier for the alert type. For convention purposes, ids starting with `.` are reserved for built in alert types. We recommend using a convention like `<plugin_id>.mySpecialAlert` for your alert types to avoid conflicting with another plugin.|string|
8787
|name|A user-friendly name for the alert type. These will be displayed in dropdowns when choosing alert types.|string|
88-
|actionGroups|An explicit list of groups the alert type may schedule actions for. Alert `actions` validation will use this array to ensure groups are valid.|string[]|
88+
|actionGroups|An explicit list of groups the alert type may schedule actions for, each specifying the ActionGroup's unique ID and human readable name. Alert `actions` validation will use this configuartion to ensure groups are valid. We highly encourage using `kbn-i18n` to translate the names of actionGroup when registering the AlertType. |Array<{id:string, name:string}>|
8989
|validate.params|When developing an alert type, you can choose to accept a series of parameters. You may also have the parameters validated before they are passed to the `executor` function or created as an alert saved object. In order to do this, provide a `@kbn/config-schema` schema that we will use to validate the `params` attribute.|@kbn/config-schema|
9090
|executor|This is where the code of the alert type lives. This is a function to be called when executing an alert on an interval basis. For full details, see executor section below.|Function|
9191

x-pack/legacy/plugins/alerting/server/alert_type_registry.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@ describe('list()', () => {
118118
registry.register({
119119
id: 'test',
120120
name: 'Test',
121-
actionGroups: [],
121+
actionGroups: [
122+
{
123+
id: 'testActionGroup',
124+
name: 'Test Action Group',
125+
},
126+
],
122127
executor: jest.fn(),
123128
});
124129
const result = registry.list();

x-pack/legacy/plugins/alerting/server/alerts_client.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('create()', () => {
8686
alertTypeRegistry.get.mockReturnValue({
8787
id: '123',
8888
name: 'Test',
89-
actionGroups: ['default'],
89+
actionGroups: [{ id: 'default', name: 'Default' }],
9090
async executor() {},
9191
});
9292
});
@@ -1884,7 +1884,7 @@ describe('update()', () => {
18841884
alertTypeRegistry.get.mockReturnValue({
18851885
id: '123',
18861886
name: 'Test',
1887-
actionGroups: ['default'],
1887+
actionGroups: [{ id: 'default', name: 'Default' }],
18881888
async executor() {},
18891889
});
18901890
});
@@ -2414,7 +2414,7 @@ describe('update()', () => {
24142414
alertTypeRegistry.get.mockReturnValueOnce({
24152415
id: '123',
24162416
name: 'Test',
2417-
actionGroups: ['default'],
2417+
actionGroups: [{ id: 'default', name: 'Default' }],
24182418
validate: {
24192419
params: schema.object({
24202420
param1: schema.string(),
@@ -2646,7 +2646,7 @@ describe('update()', () => {
26462646
alertTypeRegistry.get.mockReturnValueOnce({
26472647
id: '123',
26482648
name: 'Test',
2649-
actionGroups: ['default'],
2649+
actionGroups: [{ id: 'default', name: 'Default' }],
26502650
async executor() {},
26512651
});
26522652
savedObjectsClient.bulkGet.mockResolvedValueOnce({

x-pack/legacy/plugins/alerting/server/alerts_client.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import Boom from 'boom';
8-
import { omit, isEqual } from 'lodash';
8+
import { omit, isEqual, pluck } from 'lodash';
99
import { i18n } from '@kbn/i18n';
1010
import {
1111
Logger,
@@ -639,8 +639,9 @@ export class AlertsClient {
639639
private validateActions(alertType: AlertType, actions: NormalizedAlertAction[]): void {
640640
const { actionGroups: alertTypeActionGroups } = alertType;
641641
const usedAlertActionGroups = actions.map(action => action.group);
642+
const availableAlertTypeActionGroups = new Set(pluck(alertTypeActionGroups, 'id'));
642643
const invalidActionGroups = usedAlertActionGroups.filter(
643-
group => !alertTypeActionGroups.includes(group)
644+
group => !availableAlertTypeActionGroups.has(group)
644645
);
645646
if (invalidActionGroups.length) {
646647
throw Boom.badRequest(

x-pack/legacy/plugins/alerting/server/task_runner/create_execution_handler.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import { loggingServiceMock } from '../../../../../../src/core/server/mocks';
1111
const alertType: AlertType = {
1212
id: 'test',
1313
name: 'Test',
14-
actionGroups: ['default', 'other-group'],
14+
actionGroups: [
15+
{ id: 'default', name: 'Default' },
16+
{ id: 'other-group', name: 'Other Group' },
17+
],
1518
executor: jest.fn(),
1619
};
1720

x-pack/legacy/plugins/alerting/server/task_runner/create_execution_handler.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import { pluck } from 'lodash';
78
import { AlertAction, State, Context, AlertType } from '../types';
89
import { Logger } from '../../../../../../src/core/server';
910
import { transformActionParams } from './transform_action_params';
@@ -35,8 +36,9 @@ export function createExecutionHandler({
3536
apiKey,
3637
alertType,
3738
}: CreateExecutionHandlerOptions) {
39+
const alertTypeActionGroups = new Set(pluck(alertType.actionGroups, 'id'));
3840
return async ({ actionGroup, context, state, alertInstanceId }: ExecutionHandlerOptions) => {
39-
if (!alertType.actionGroups.includes(actionGroup)) {
41+
if (!alertTypeActionGroups.has(actionGroup)) {
4042
logger.error(`Invalid action group "${actionGroup}" for alert "${alertType.id}".`);
4143
return;
4244
}

x-pack/legacy/plugins/alerting/server/task_runner/task_runner.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
const alertType = {
2020
id: 'test',
2121
name: 'My test alert',
22-
actionGroups: ['default'],
22+
actionGroups: [{ id: 'default', name: 'Default' }],
2323
executor: jest.fn(),
2424
};
2525
let fakeTimer: sinon.SinonFakeTimers;

x-pack/legacy/plugins/alerting/server/task_runner/task_runner_factory.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
const alertType = {
1717
id: 'test',
1818
name: 'My test alert',
19-
actionGroups: ['default'],
19+
actionGroups: [{ id: 'default', name: 'Default' }],
2020
executor: jest.fn(),
2121
};
2222
let fakeTimer: sinon.SinonFakeTimers;

x-pack/legacy/plugins/alerting/server/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,18 @@ export interface AlertExecutorOptions {
4242
updatedBy: string | null;
4343
}
4444

45+
export interface ActionGroup {
46+
id: string;
47+
name: string;
48+
}
49+
4550
export interface AlertType {
4651
id: string;
4752
name: string;
4853
validate?: {
4954
params?: { validate: (object: any) => any };
5055
};
51-
actionGroups: string[];
56+
actionGroups: ActionGroup[];
5257
executor: ({ services, params, state }: AlertExecutorOptions) => Promise<State | void>;
5358
}
5459

x-pack/legacy/plugins/monitoring/server/alerts/license_expiration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('getLicenseExpiration', () => {
102102
it('should have the right id and actionGroups', () => {
103103
const alert = getLicenseExpiration(server, getMonitoringCluster, getLogger, ccrEnabled);
104104
expect(alert.id).toBe(ALERT_TYPE_LICENSE_EXPIRATION);
105-
expect(alert.actionGroups).toEqual(['default']);
105+
expect(alert.actionGroups).toEqual([{ id: 'default', name: 'Default' }]);
106106
});
107107

108108
it('should return the state if no license is provided', async () => {

0 commit comments

Comments
 (0)