Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,57 @@ describe('successful migrations', () => {
});
});
});

describe('8.0.0', () => {
test('resolveSavedObjectIdsInActionTaskParams in default namespace', () => {
const migration800 = getActionTaskParamsMigrations(encryptedSavedObjectsSetup, [])['8.0.0'];
const action = getMockData({
relatedSavedObjects: [
{
id: '1234',
type: 'action',
typeId: 'test',
namespace: 'some-namespace',
},
],
});
const migratedAction = migration800(action, context);
expect(migratedAction).toEqual({
...action,
attributes: {
...action.attributes,
relatedSavedObjects: [
{
id: '1234',
namespace: 'some-namespace',
type: 'action',
typeId: 'test',
},
],
},
});
});

test('resolveSavedObjectIdsInActionTaskParams in non default namespace', () => {
const migration800 = getActionTaskParamsMigrations(encryptedSavedObjectsSetup, [])['8.0.0'];
const action = getMockData(
{
relatedSavedObjects: [
{
id: '1234',
type: 'action',
typeId: 'test',
namespace: 'some-namespace',
},
],
},
undefined,
['custom']
);
const migratedAction = migration800(action, context);
expect(migratedAction.attributes.actionId).not.toEqual(action.attributes.actionId);
});
});
});

describe('handles errors during migrations', () => {
Expand Down Expand Up @@ -402,7 +453,8 @@ describe('isPreconfiguredAction()', () => {

function getMockData(
overwrites: Record<string, unknown> = {},
referencesOverwrites: SavedObjectReference[] = []
referencesOverwrites: SavedObjectReference[] = [],
namespaces: string[] = ['default']
): SavedObjectUnsanitizedDoc<ActionTaskParams> {
return {
attributes: {
Expand All @@ -412,6 +464,7 @@ function getMockData(
},
references: [...referencesOverwrites],
id: uuid.v4(),
namespaces,
type: 'action_task_param',
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
SavedObjectMigrationFn,
SavedObjectMigrationContext,
SavedObjectReference,
SavedObjectsUtils,
} from '../../../../../src/core/server';
import { ActionTaskParams, PreConfiguredAction } from '../types';
import { EncryptedSavedObjectsPluginSetup } from '../../../encrypted_saved_objects/server';
Expand Down Expand Up @@ -48,8 +49,18 @@ export function getActionTaskParamsMigrations(
pipeMigrations(getUseSavedObjectReferencesFn(preconfiguredActions))
);

const migrationResolveSavedObjectsIdsInActionTaskParams = createEsoMigration(
encryptedSavedObjects,
(doc): doc is SavedObjectUnsanitizedDoc<ActionTaskParams> => doc.type === 'action_task_params',
pipeMigrations(resolveSavedObjectIdsInActionTaskParams)
);

return {
'7.16.0': executeMigrationWithErrorHandling(migrationActionTaskParamsSixteen, '7.16.0'),
'8.0.0': executeMigrationWithErrorHandling(
migrationResolveSavedObjectsIdsInActionTaskParams,
'8.0.0'
),
};
}

Expand Down Expand Up @@ -133,6 +144,23 @@ function useSavedObjectReferences(
};
}

function resolveSavedObjectIdsInActionTaskParams(
Comment thread
YulNaumenko marked this conversation as resolved.
Outdated
doc: SavedObjectUnsanitizedDoc<ActionTaskParams>
): SavedObjectUnsanitizedDoc<ActionTaskParams> {
const namespace = doc.namespaces && doc.namespaces.length ? doc.namespaces[0] : undefined;
const newId =
namespace && namespace !== 'default'
? SavedObjectsUtils.getConvertedObjectId(namespace, 'action', doc.attributes.actionId)
: doc.attributes.actionId;
return {
...doc,
attributes: {
...doc.attributes,
actionId: newId,
},
};
}

function pipeMigrations(...migrations: ActionTaskParamMigration[]): ActionTaskParamMigration {
return (doc: SavedObjectUnsanitizedDoc<ActionTaskParams>) =>
migrations.reduce((migratedDoc, nextMigration) => nextMigration(migratedDoc), doc);
Expand Down
6 changes: 4 additions & 2 deletions x-pack/plugins/actions/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export function setupSavedObjects(
savedObjects.registerType({
name: ACTION_SAVED_OBJECT_TYPE,
hidden: true,
namespaceType: 'single',
namespaceType: 'multiple-isolated',
convertToMultiNamespaceTypeVersion: '8.0.0',
mappings: mappings.action as SavedObjectsTypeMappingDefinition,
migrations: getActionsMigrations(encryptedSavedObjects),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to add a no-op migration for the ACTION_SAVED_OBJECT_TYPE type too, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah.., I forgot about this. Thank you for pointing!

management: {
Expand Down Expand Up @@ -71,7 +72,8 @@ export function setupSavedObjects(
savedObjects.registerType({
name: ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE,
hidden: true,
namespaceType: 'single',
namespaceType: 'multiple-isolated',
convertToMultiNamespaceTypeVersion: '8.0.0',
mappings: mappings.action_task_params as SavedObjectsTypeMappingDefinition,
migrations: getActionTaskParamsMigrations(encryptedSavedObjects, preconfiguredActions),
excludeOnUpgrade: async ({ readonlyEsClient }) => {
Expand Down