diff --git a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts index 9107552cf7eee..1c4437905e022 100644 --- a/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts +++ b/x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts @@ -83,6 +83,6 @@ export function extractRefsFromGeoContainmentAlert( ...doc.attributes, params: newParams as SavedObjectAttributes, }, - references, + references: [...(doc.references || []), ...references], }; } diff --git a/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts b/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts index d8c8e3fd3a443..2f4aff9f7d4bb 100644 --- a/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts +++ b/x-pack/plugins/alerting/server/saved_objects/migrations.test.ts @@ -1914,7 +1914,7 @@ describe('successful migrations', () => { }); }); - test('extracts references from geo-containment alerts', () => { + test('geo-containment alert migration extracts boundary and index references', () => { const migration7160 = getMigrations(encryptedSavedObjectsSetup, isPreconfigured)['7.16.0']; const alert = { ...getMockData({ @@ -1941,6 +1941,68 @@ describe('successful migrations', () => { expect(migratedAlert.attributes.params.indexId).toEqual(undefined); expect(migratedAlert.attributes.params.boundaryIndexId).toEqual(undefined); }); + + test('geo-containment alert migration should preserve foreign references', () => { + const migration7160 = getMigrations(encryptedSavedObjectsSetup, isPreconfigured)['7.16.0']; + const alert = { + ...getMockData({ + alertTypeId: '.geo-containment', + params: { + indexId: 'foo', + boundaryIndexId: 'bar', + }, + }), + references: [ + { + name: 'foreign-name', + id: '999', + type: 'foreign-name', + }, + ], + }; + + const migratedAlert = migration7160(alert, migrationContext); + + expect(migratedAlert.references).toEqual([ + { + name: 'foreign-name', + id: '999', + type: 'foreign-name', + }, + { id: 'foo', name: 'tracked_index_foo', type: 'index-pattern' }, + { id: 'bar', name: 'boundary_index_bar', type: 'index-pattern' }, + ]); + + expect(migratedAlert.attributes.params).toEqual({ + boundaryIndexRefName: 'boundary_index_bar', + indexRefName: 'tracked_index_foo', + }); + + expect(migratedAlert.attributes.params.indexId).toEqual(undefined); + expect(migratedAlert.attributes.params.boundaryIndexId).toEqual(undefined); + }); + + test('geo-containment alert migration ignores other alert-types', () => { + const migration7160 = getMigrations(encryptedSavedObjectsSetup, isPreconfigured)['7.16.0']; + const alert = { + ...getMockData({ + alertTypeId: '.foo', + references: [ + { + name: 'foreign-name', + id: '999', + type: 'foreign-name', + }, + ], + }), + }; + + const migratedAlert = migration7160(alert, migrationContext); + + expect(typeof migratedAlert.attributes.legacyId).toEqual('string'); // introduced by setLegacyId migration + delete migratedAlert.attributes.legacyId; + expect(migratedAlert).toEqual(alert); + }); }); describe('8.0.0', () => { diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts index c86b74c751e48..2f5e5d778b838 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/alert_type.test.ts @@ -59,11 +59,21 @@ describe('alertType', () => { indexRefName: 'tracked_index_foobar', }, [ + { + id: 'foreign', + name: 'foobar', + type: 'foreign', + }, { id: 'foobar', name: 'tracked_index_foobar', type: 'index-pattern', }, + { + id: 'foreignToo', + name: 'boundary_index_shouldbeignored', + type: 'index-pattern', + }, { id: 'boundaryid', name: 'boundary_index_boundaryid',