From 85df764d6df3d7d5a511290bcaf92d64762b92b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cjeramysoucy=E2=80=9D?= Date: Thu, 22 Jun 2023 10:57:06 +0200 Subject: [PATCH 1/7] Initial commit of versioned common space interface --- x-pack/plugins/spaces/common/index.ts | 8 +++- x-pack/plugins/spaces/common/types/index.ts | 8 ++++ x-pack/plugins/spaces/common/types/latest.ts | 8 ++++ .../common/{types.ts => types/space/v1.ts} | 0 .../spaces_client/spaces_client.test.ts | 39 +++++++++++----- .../server/spaces_client/spaces_client.ts | 44 ++++++++++++------- 6 files changed, 79 insertions(+), 28 deletions(-) create mode 100644 x-pack/plugins/spaces/common/types/index.ts create mode 100644 x-pack/plugins/spaces/common/types/latest.ts rename x-pack/plugins/spaces/common/{types.ts => types/space/v1.ts} (100%) diff --git a/x-pack/plugins/spaces/common/index.ts b/x-pack/plugins/spaces/common/index.ts index 18bc6bbaed8be..4a767fb403ee2 100644 --- a/x-pack/plugins/spaces/common/index.ts +++ b/x-pack/plugins/spaces/common/index.ts @@ -13,4 +13,10 @@ export { DEFAULT_SPACE_ID, } from './constants'; export { addSpaceIdToPath, getSpaceIdFromPath } from './lib/spaces_url_parser'; -export type { Space, GetAllSpacesOptions, GetAllSpacesPurpose, GetSpaceResult } from './types'; +export type { + Space, + GetAllSpacesOptions, + GetAllSpacesPurpose, + GetSpaceResult, +} from './types/latest'; +export { spaceV1 } from './types'; diff --git a/x-pack/plugins/spaces/common/types/index.ts b/x-pack/plugins/spaces/common/types/index.ts new file mode 100644 index 0000000000000..6a45180e186cd --- /dev/null +++ b/x-pack/plugins/spaces/common/types/index.ts @@ -0,0 +1,8 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export * as spaceV1 from './space/v1'; diff --git a/x-pack/plugins/spaces/common/types/latest.ts b/x-pack/plugins/spaces/common/types/latest.ts new file mode 100644 index 0000000000000..12924244d8591 --- /dev/null +++ b/x-pack/plugins/spaces/common/types/latest.ts @@ -0,0 +1,8 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export * from './space/v1'; diff --git a/x-pack/plugins/spaces/common/types.ts b/x-pack/plugins/spaces/common/types/space/v1.ts similarity index 100% rename from x-pack/plugins/spaces/common/types.ts rename to x-pack/plugins/spaces/common/types/space/v1.ts diff --git a/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts b/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts index c79c9d778a096..d42da10d4b326 100644 --- a/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts +++ b/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts @@ -27,7 +27,7 @@ describe('#getAll', () => { attributes: { name: 'foo-name', description: 'foo-description', - bar: 'foo-bar', + // bar: 'foo-bar', // what is this for? }, }, { @@ -35,7 +35,7 @@ describe('#getAll', () => { attributes: { name: 'bar-name', description: 'bar-description', - bar: 'bar-bar', + // bar: 'bar-bar', // what is this for? }, }, { @@ -43,7 +43,7 @@ describe('#getAll', () => { attributes: { name: 'baz-name', description: 'baz-description', - bar: 'baz-bar', + // bar: 'baz-bar', // what is this for? }, }, ]; @@ -53,19 +53,34 @@ describe('#getAll', () => { id: 'foo', name: 'foo-name', description: 'foo-description', - bar: 'foo-bar', + // color: undefined, + // initials: undefined, + // imageUrl: undefined, + // disabledFeatures: undefined, + // _reserved: undefined, + // bar: 'foo-bar', }, { id: 'bar', name: 'bar-name', description: 'bar-description', - bar: 'bar-bar', + // color: undefined, + // initials: undefined, + // imageUrl: undefined, + // disabledFeatures: undefined, + // _reserved: undefined, + // bar: 'bar-bar', }, { id: 'baz', name: 'baz-name', description: 'baz-description', - bar: 'baz-bar', + // color: undefined, + // initials: undefined, + // imageUrl: undefined, + // disabledFeatures: undefined, + // _reserved: undefined, + // bar: 'baz-bar', }, ]; @@ -108,7 +123,7 @@ describe('#get', () => { attributes: { name: 'foo-name', description: 'foo-description', - bar: 'foo-bar', + // bar: 'foo-bar', // what is this for? }, }; @@ -116,7 +131,7 @@ describe('#get', () => { id: 'foo', name: 'foo-name', description: 'foo-description', - bar: 'foo-bar', + // bar: 'foo-bar', }; test(`gets space using callWithRequestRepository`, async () => { @@ -160,7 +175,7 @@ describe('#create', () => { attributes: { name: 'foo-name', description: 'foo-description', - bar: 'foo-bar', + // bar: 'foo-bar', // what is this for? disabledFeatures: [], }, }; @@ -169,7 +184,7 @@ describe('#create', () => { id, name: 'foo-name', description: 'foo-description', - bar: 'foo-bar', + // bar: 'foo-bar', disabledFeatures: [], }; @@ -249,7 +264,7 @@ describe('#update', () => { attributes: { name: 'foo-name', description: 'foo-description', - bar: 'foo-bar', + // bar: 'foo-bar', _reserved: true, disabledFeatures: [], }, @@ -259,7 +274,7 @@ describe('#update', () => { id: 'foo', name: 'foo-name', description: 'foo-description', - bar: 'foo-bar', + // bar: 'foo-bar', _reserved: true, disabledFeatures: [], }; diff --git a/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts b/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts index b06187c032662..5092f9574aba0 100644 --- a/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts +++ b/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts @@ -15,11 +15,11 @@ import type { SavedObject, } from '@kbn/core/server'; -import type { GetAllSpacesOptions, GetAllSpacesPurpose, GetSpaceResult, Space } from '../../common'; import { isReservedSpace } from '../../common'; +import type { spaceV1 as v1 } from '../../common'; import type { ConfigType } from '../config'; -const SUPPORTED_GET_SPACE_PURPOSES: GetAllSpacesPurpose[] = [ +const SUPPORTED_GET_SPACE_PURPOSES: v1.GetAllSpacesPurpose[] = [ 'any', 'copySavedObjectsIntoSpace', 'findSavedObjects', @@ -36,26 +36,26 @@ export interface ISpacesClient { * Retrieve all available spaces. * @param options controls which spaces are retrieved. */ - getAll(options?: GetAllSpacesOptions): Promise; + getAll(options?: v1.GetAllSpacesOptions): Promise; /** * Retrieve a space by its id. * @param id the space id. */ - get(id: string): Promise; + get(id: string): Promise; /** * Creates a space. * @param space the space to create. */ - create(space: Space): Promise; + create(space: v1.Space): Promise; /** * Updates a space. * @param id the id of the space to update. * @param space the updated space. */ - update(id: string, space: Space): Promise; + update(id: string, space: v1.Space): Promise; /** * Returns a {@link ISavedObjectsPointInTimeFinder} to help page through @@ -88,7 +88,7 @@ export class SpacesClient implements ISpacesClient { private readonly nonGlobalTypeNames: string[] ) {} - public async getAll(options: GetAllSpacesOptions = {}): Promise { + public async getAll(options: v1.GetAllSpacesOptions = {}): Promise { const { purpose = DEFAULT_PURPOSE } = options; if (!SUPPORTED_GET_SPACE_PURPOSES.includes(purpose)) { throw Boom.badRequest(`unsupported space purpose: ${purpose}`); @@ -113,7 +113,7 @@ export class SpacesClient implements ISpacesClient { return this.transformSavedObjectToSpace(savedObject); } - public async create(space: Space) { + public async create(space: v1.Space) { const { total } = await this.repository.find({ type: 'space', page: 1, @@ -127,18 +127,22 @@ export class SpacesClient implements ISpacesClient { this.debugLogger(`SpacesClient.create(), using RBAC. Attempting to create space`); - const attributes = omit(space, ['id', '_reserved']); + // const attributes = omit(space, ['id', '_reserved']); const id = space.id; - const createdSavedObject = await this.repository.create('space', attributes, { id }); + const createdSavedObject = await this.repository.create( + 'space', + this.generateSpaceAttributes(space), + { id } + ); this.debugLogger(`SpacesClient.create(), created space object`); return this.transformSavedObjectToSpace(createdSavedObject); } - public async update(id: string, space: Space) { - const attributes = omit(space, 'id', '_reserved'); - await this.repository.update('space', id, attributes); + public async update(id: string, space: v1.Space) { + // const attributes = omit(space, 'id', '_reserved'); + await this.repository.update('space', id, this.generateSpaceAttributes(space)); const updatedSavedObject = await this.repository.get('space', id); return this.transformSavedObjectToSpace(updatedSavedObject); } @@ -173,7 +177,17 @@ export class SpacesClient implements ISpacesClient { private transformSavedObjectToSpace(savedObject: SavedObject) { return { id: savedObject.id, - ...savedObject.attributes, - } as Space; + name: savedObject.attributes.name, + description: savedObject.attributes.description, + color: savedObject.attributes.color, + initials: savedObject.attributes.initials, + imageUrl: savedObject.attributes.imageUrl, + disabledFeatures: savedObject.attributes.disabledFeatures, + _reserved: savedObject.attributes._reserved, + } as v1.Space; + } + + private generateSpaceAttributes(space: v1.Space) { + return omit(space, ['id', '_reserved']); } } From 3123626e242b200dbf7ae955173bdcbad9a2250b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cjeramysoucy=E2=80=9D?= Date: Thu, 22 Jun 2023 12:16:14 +0200 Subject: [PATCH 2/7] Fixes local imports --- x-pack/plugins/spaces/common/is_reserved_space.test.ts | 2 +- x-pack/plugins/spaces/common/is_reserved_space.ts | 2 +- x-pack/plugins/spaces/server/spaces_client/spaces_client.ts | 2 -- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/spaces/common/is_reserved_space.test.ts b/x-pack/plugins/spaces/common/is_reserved_space.test.ts index 630d4a000f3e5..1ec83b9ce3487 100644 --- a/x-pack/plugins/spaces/common/is_reserved_space.test.ts +++ b/x-pack/plugins/spaces/common/is_reserved_space.test.ts @@ -5,8 +5,8 @@ * 2.0. */ +import type { Space } from '.'; import { isReservedSpace } from './is_reserved_space'; -import type { Space } from './types'; test('it returns true for reserved spaces', () => { const space: Space = { diff --git a/x-pack/plugins/spaces/common/is_reserved_space.ts b/x-pack/plugins/spaces/common/is_reserved_space.ts index 92b9a0a99ddbb..32413e1c015f2 100644 --- a/x-pack/plugins/spaces/common/is_reserved_space.ts +++ b/x-pack/plugins/spaces/common/is_reserved_space.ts @@ -7,7 +7,7 @@ import { get } from 'lodash'; -import type { Space } from './types'; +import type { Space } from '.'; /** * Returns whether the given Space is reserved or not. diff --git a/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts b/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts index 5092f9574aba0..0018cf505fe67 100644 --- a/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts +++ b/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts @@ -127,7 +127,6 @@ export class SpacesClient implements ISpacesClient { this.debugLogger(`SpacesClient.create(), using RBAC. Attempting to create space`); - // const attributes = omit(space, ['id', '_reserved']); const id = space.id; const createdSavedObject = await this.repository.create( 'space', @@ -141,7 +140,6 @@ export class SpacesClient implements ISpacesClient { } public async update(id: string, space: v1.Space) { - // const attributes = omit(space, 'id', '_reserved'); await this.repository.update('space', id, this.generateSpaceAttributes(space)); const updatedSavedObject = await this.repository.get('space', id); return this.transformSavedObjectToSpace(updatedSavedObject); From a22aaf79a2747ef72e02759785742d06c731a0a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cjeramysoucy=E2=80=9D?= Date: Thu, 22 Jun 2023 17:30:35 +0200 Subject: [PATCH 3/7] Fixes mock return to work with new explicit conversion. --- .../api/__fixtures__/create_mock_so_repository.ts | 7 ++++--- .../spaces/server/spaces_client/spaces_client.ts | 10 ++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/x-pack/plugins/spaces/server/routes/api/__fixtures__/create_mock_so_repository.ts b/x-pack/plugins/spaces/server/routes/api/__fixtures__/create_mock_so_repository.ts index d1e26143de906..fe59de4fa98ac 100644 --- a/x-pack/plugins/spaces/server/routes/api/__fixtures__/create_mock_so_repository.ts +++ b/x-pack/plugins/spaces/server/routes/api/__fixtures__/create_mock_so_repository.ts @@ -27,13 +27,14 @@ export const createMockSavedObjectsRepository = (spaces: any[] = []) => { if (spaces.find((s) => s.id === id)) { throw SavedObjectsErrorHelpers.decorateConflictError(new Error(), 'space conflict'); } - return {}; + return { id, attributes }; }), update: jest.fn((type, id) => { - if (!spaces.find((s) => s.id === id)) { + const result = spaces.find((s) => s.id === id); + if (!result) { throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id); } - return {}; + return result[0]; }), bulkUpdate: jest.fn(), delete: jest.fn((type: string, id: string) => { diff --git a/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts b/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts index 0018cf505fe67..b6fe8d0489d83 100644 --- a/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts +++ b/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts @@ -128,11 +128,8 @@ export class SpacesClient implements ISpacesClient { this.debugLogger(`SpacesClient.create(), using RBAC. Attempting to create space`); const id = space.id; - const createdSavedObject = await this.repository.create( - 'space', - this.generateSpaceAttributes(space), - { id } - ); + const attributes = this.generateSpaceAttributes(space); + const createdSavedObject = await this.repository.create('space', attributes, { id }); this.debugLogger(`SpacesClient.create(), created space object`); @@ -140,7 +137,8 @@ export class SpacesClient implements ISpacesClient { } public async update(id: string, space: v1.Space) { - await this.repository.update('space', id, this.generateSpaceAttributes(space)); + const attributes = this.generateSpaceAttributes(space); + await this.repository.update('space', id, attributes); const updatedSavedObject = await this.repository.get('space', id); return this.transformSavedObjectToSpace(updatedSavedObject); } From eed39cf952fd4812eb8271cc19267827cb838f6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cjeramysoucy=E2=80=9D?= Date: Mon, 26 Jun 2023 07:44:00 -0400 Subject: [PATCH 4/7] Removing commented code. --- .../server/spaces_client/spaces_client.test.ts | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts b/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts index d42da10d4b326..28291a5fd26b2 100644 --- a/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts +++ b/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts @@ -53,34 +53,16 @@ describe('#getAll', () => { id: 'foo', name: 'foo-name', description: 'foo-description', - // color: undefined, - // initials: undefined, - // imageUrl: undefined, - // disabledFeatures: undefined, - // _reserved: undefined, - // bar: 'foo-bar', }, { id: 'bar', name: 'bar-name', description: 'bar-description', - // color: undefined, - // initials: undefined, - // imageUrl: undefined, - // disabledFeatures: undefined, - // _reserved: undefined, - // bar: 'bar-bar', }, { id: 'baz', name: 'baz-name', description: 'baz-description', - // color: undefined, - // initials: undefined, - // imageUrl: undefined, - // disabledFeatures: undefined, - // _reserved: undefined, - // bar: 'baz-bar', }, ]; From 0afcbfb4d5f5644b271cb6da11195231ce2eecc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cjeramysoucy=E2=80=9D?= Date: Wed, 28 Jun 2023 10:20:54 -0400 Subject: [PATCH 5/7] Implements explicit generation of space SO attributes, updates test suite --- .../spaces_client/spaces_client.test.ts | 191 ++++++++++-------- .../server/spaces_client/spaces_client.ts | 16 +- 2 files changed, 118 insertions(+), 89 deletions(-) diff --git a/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts b/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts index 28291a5fd26b2..86f648693387b 100644 --- a/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts +++ b/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts @@ -5,9 +5,10 @@ * 2.0. */ +import { SavedObject } from '@kbn/core-saved-objects-server'; import { savedObjectsRepositoryMock } from '@kbn/core/server/mocks'; -import type { GetAllSpacesPurpose } from '../../common'; +import type { GetAllSpacesPurpose, Space } from '../../common'; import type { ConfigType } from '../config'; import { ConfigSchema } from '../config'; import { SpacesClient } from './spaces_client'; @@ -21,48 +22,72 @@ const createMockConfig = (mockConfig: ConfigType = { enabled: true, maxSpaces: 1 }; describe('#getAll', () => { - const savedObjects = [ + const savedObjects:SavedObject[] = [ { + // foo has all of the attributes expected by the space interface id: 'foo', + type: 'space', + references: [], attributes: { name: 'foo-name', description: 'foo-description', - // bar: 'foo-bar', // what is this for? + color: '#FFFFFF', + initials: 'FB', + imageUrl: 'go-bots/predates/transformers', + disabledFeatures: [], + _reserved: true, + bar: 'foo-bar', // an extra attribute that will be ignored during conversion }, }, { + // bar his missing attributes of color and image url id: 'bar', + type: 'space', + references: [], attributes: { name: 'bar-name', description: 'bar-description', - // bar: 'bar-bar', // what is this for? + initials: 'BA', + disabledFeatures: [], + bar: 'bar-bar', // an extra attribute that will be ignored during conversion }, }, { + // baz only has the bare minumum atributes id: 'baz', + type: 'space', + references: [], attributes: { name: 'baz-name', description: 'baz-description', - // bar: 'baz-bar', // what is this for? + bar: 'baz-bar', // an extra attribute that will be ignored during conversion }, }, ]; - const expectedSpaces = [ + const expectedSpaces:Space[] = [ { id: 'foo', name: 'foo-name', description: 'foo-description', + color: "#FFFFFF", + initials: "FB", + imageUrl: "go-bots/predates/transformers", + disabledFeatures: [], + _reserved: true, }, { id: 'bar', name: 'bar-name', description: 'bar-description', + initials: "BA", + disabledFeatures: [], }, { id: 'baz', name: 'baz-name', description: 'baz-description', + disabledFeatures: [], }, ]; @@ -98,22 +123,31 @@ describe('#getAll', () => { }); describe('#get', () => { - const savedObject = { + const savedObject:SavedObject = { id: 'foo', - type: 'foo', + type: 'space', references: [], attributes: { name: 'foo-name', description: 'foo-description', - // bar: 'foo-bar', // what is this for? + color: '#FFFFFF', + initials: 'FB', + imageUrl: 'go-bots/predates/transformers', + disabledFeatures: [], + _reserved: true, + bar: 'foo-bar', // an extra attribute that will be ignored during conversion }, }; - const expectedSpace = { + const expectedSpace:Space = { id: 'foo', name: 'foo-name', description: 'foo-description', - // bar: 'foo-bar', + color: "#FFFFFF", + initials: "FB", + imageUrl: "go-bots/predates/transformers", + disabledFeatures: [], + _reserved: true, }; test(`gets space using callWithRequestRepository`, async () => { @@ -133,41 +167,35 @@ describe('#get', () => { describe('#create', () => { const id = 'foo'; + const attributes = { + name: 'foo-name', + description: 'foo-description', + color: '#FFFFFF', + initials: 'FB', + imageUrl: "go-bots/predates/transformers", + disabledFeatures: [], + }; const spaceToCreate = { id, - name: 'foo-name', - description: 'foo-description', - bar: 'foo-bar', + ...attributes, _reserved: true, - disabledFeatures: [], + bar: 'foo-bar', // will not make it to the saved object attributes }; - const attributes = { - name: 'foo-name', - description: 'foo-description', - bar: 'foo-bar', - disabledFeatures: [], - }; - - const savedObject = { + const savedObject:SavedObject = { id, - type: 'foo', + type: 'space', references: [], attributes: { - name: 'foo-name', - description: 'foo-description', - // bar: 'foo-bar', // what is this for? - disabledFeatures: [], + ...attributes, + foo: 'bar', // will get stripped in conversion }, }; - const expectedReturnedSpace = { + const expectedReturnedSpace:Space = { id, - name: 'foo-name', - description: 'foo-description', - // bar: 'foo-bar', - disabledFeatures: [], + ...attributes, }; test(`creates space using callWithRequestRepository when we're under the max`, async () => { @@ -223,42 +251,37 @@ describe('#create', () => { }); describe('#update', () => { - const spaceToUpdate = { - id: 'foo', - name: 'foo-name', - description: 'foo-description', - bar: 'foo-bar', - _reserved: false, - disabledFeatures: [], + const attributes = { + name: 'foo-name', + description: 'foo-description', + color: '#FFFFFF', + initials: 'FB', + imageUrl: "go-bots/predates/transformers", + disabledFeatures: [], }; - const attributes = { - name: 'foo-name', - description: 'foo-description', - bar: 'foo-bar', - disabledFeatures: [], + const spaceToUpdate = { + id: 'foo', + ...attributes, + _reserved: false, // will have no affect + bar: 'foo-bar', // will not make it to the saved object attributes }; - const savedObject = { + const savedObject:SavedObject = { id: 'foo', - type: 'foo', + type: 'space', references: [], attributes: { - name: 'foo-name', - description: 'foo-description', - // bar: 'foo-bar', + ...attributes, _reserved: true, - disabledFeatures: [], + foo: 'bar', // will get stripped in conversion }, }; - const expectedReturnedSpace = { + const expectedReturnedSpace:Space = { id: 'foo', - name: 'foo-name', - description: 'foo-description', - // bar: 'foo-bar', + ...attributes, _reserved: true, - disabledFeatures: [], }; test(`updates space using callWithRequestRepository`, async () => { @@ -280,9 +303,9 @@ describe('#update', () => { describe('#delete', () => { const id = 'foo'; - const reservedSavedObject = { + const reservedSavedObject:SavedObject = { id, - type: 'foo', + type: 'space', references: [], attributes: { name: 'foo-name', @@ -292,9 +315,9 @@ describe('#delete', () => { }, }; - const notReservedSavedObject = { + const notReservedSavedObject:SavedObject = { id, - type: 'foo', + type: 'space', references: [], attributes: { name: 'foo-name', @@ -332,30 +355,30 @@ describe('#delete', () => { expect(mockCallWithRequestRepository.delete).toHaveBeenCalledWith('space', id); expect(mockCallWithRequestRepository.deleteByNamespace).toHaveBeenCalledWith(id); }); +}); - describe('#disableLegacyUrlAliases', () => { - test(`updates legacy URL aliases using callWithRequestRepository`, async () => { - const mockDebugLogger = createMockDebugLogger(); - const mockConfig = createMockConfig(); - const mockCallWithRequestRepository = savedObjectsRepositoryMock.create(); - - const client = new SpacesClient( - mockDebugLogger, - mockConfig, - mockCallWithRequestRepository, - [] - ); - const aliases = [ - { targetSpace: 'space1', targetType: 'foo', sourceId: '123' }, - { targetSpace: 'space2', targetType: 'bar', sourceId: '456' }, - ]; - await client.disableLegacyUrlAliases(aliases); - - expect(mockCallWithRequestRepository.bulkUpdate).toHaveBeenCalledTimes(1); - expect(mockCallWithRequestRepository.bulkUpdate).toHaveBeenCalledWith([ - { type: 'legacy-url-alias', id: 'space1:foo:123', attributes: { disabled: true } }, - { type: 'legacy-url-alias', id: 'space2:bar:456', attributes: { disabled: true } }, - ]); - }); +describe('#disableLegacyUrlAliases', () => { + test(`updates legacy URL aliases using callWithRequestRepository`, async () => { + const mockDebugLogger = createMockDebugLogger(); + const mockConfig = createMockConfig(); + const mockCallWithRequestRepository = savedObjectsRepositoryMock.create(); + + const client = new SpacesClient( + mockDebugLogger, + mockConfig, + mockCallWithRequestRepository, + [] + ); + const aliases = [ + { targetSpace: 'space1', targetType: 'foo', sourceId: '123' }, + { targetSpace: 'space2', targetType: 'bar', sourceId: '456' }, + ]; + await client.disableLegacyUrlAliases(aliases); + + expect(mockCallWithRequestRepository.bulkUpdate).toHaveBeenCalledTimes(1); + expect(mockCallWithRequestRepository.bulkUpdate).toHaveBeenCalledWith([ + { type: 'legacy-url-alias', id: 'space1:foo:123', attributes: { disabled: true } }, + { type: 'legacy-url-alias', id: 'space2:bar:456', attributes: { disabled: true } }, + ]); }); }); diff --git a/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts b/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts index b6fe8d0489d83..62f9b554b3be8 100644 --- a/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts +++ b/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts @@ -6,7 +6,6 @@ */ import Boom from '@hapi/boom'; -import { omit } from 'lodash'; import type { LegacyUrlAliasTarget } from '@kbn/core-saved-objects-common'; import type { @@ -170,20 +169,27 @@ export class SpacesClient implements ISpacesClient { await this.repository.bulkUpdate(objectsToUpdate); } - private transformSavedObjectToSpace(savedObject: SavedObject) { + private transformSavedObjectToSpace(savedObject: SavedObject):v1.Space { return { id: savedObject.id, - name: savedObject.attributes.name, + name: savedObject.attributes.name??'', description: savedObject.attributes.description, color: savedObject.attributes.color, initials: savedObject.attributes.initials, imageUrl: savedObject.attributes.imageUrl, - disabledFeatures: savedObject.attributes.disabledFeatures, + disabledFeatures: savedObject.attributes.disabledFeatures??[], _reserved: savedObject.attributes._reserved, } as v1.Space; } private generateSpaceAttributes(space: v1.Space) { - return omit(space, ['id', '_reserved']); + return { + name: space.name, + description: space.description, + color: space.color, + initials: space.initials, + imageUrl: space.imageUrl, + disabledFeatures: space.disabledFeatures + } } } From e5c4d08fc4e8837ddb38af5f75151f2b52f8c671 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 28 Jun 2023 15:01:23 +0000 Subject: [PATCH 6/7] [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' --- .../spaces_client/spaces_client.test.ts | 65 +++++++++---------- .../server/spaces_client/spaces_client.ts | 10 +-- 2 files changed, 35 insertions(+), 40 deletions(-) diff --git a/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts b/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts index 86f648693387b..a1fe2e13ab897 100644 --- a/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts +++ b/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts @@ -22,7 +22,7 @@ const createMockConfig = (mockConfig: ConfigType = { enabled: true, maxSpaces: 1 }; describe('#getAll', () => { - const savedObjects:SavedObject[] = [ + const savedObjects: Array> = [ { // foo has all of the attributes expected by the space interface id: 'foo', @@ -65,14 +65,14 @@ describe('#getAll', () => { }, ]; - const expectedSpaces:Space[] = [ + const expectedSpaces: Space[] = [ { id: 'foo', name: 'foo-name', description: 'foo-description', - color: "#FFFFFF", - initials: "FB", - imageUrl: "go-bots/predates/transformers", + color: '#FFFFFF', + initials: 'FB', + imageUrl: 'go-bots/predates/transformers', disabledFeatures: [], _reserved: true, }, @@ -80,7 +80,7 @@ describe('#getAll', () => { id: 'bar', name: 'bar-name', description: 'bar-description', - initials: "BA", + initials: 'BA', disabledFeatures: [], }, { @@ -123,7 +123,7 @@ describe('#getAll', () => { }); describe('#get', () => { - const savedObject:SavedObject = { + const savedObject: SavedObject = { id: 'foo', type: 'space', references: [], @@ -139,13 +139,13 @@ describe('#get', () => { }, }; - const expectedSpace:Space = { + const expectedSpace: Space = { id: 'foo', name: 'foo-name', description: 'foo-description', - color: "#FFFFFF", - initials: "FB", - imageUrl: "go-bots/predates/transformers", + color: '#FFFFFF', + initials: 'FB', + imageUrl: 'go-bots/predates/transformers', disabledFeatures: [], _reserved: true, }; @@ -168,12 +168,12 @@ describe('#get', () => { describe('#create', () => { const id = 'foo'; const attributes = { - name: 'foo-name', - description: 'foo-description', - color: '#FFFFFF', - initials: 'FB', - imageUrl: "go-bots/predates/transformers", - disabledFeatures: [], + name: 'foo-name', + description: 'foo-description', + color: '#FFFFFF', + initials: 'FB', + imageUrl: 'go-bots/predates/transformers', + disabledFeatures: [], }; const spaceToCreate = { @@ -183,7 +183,7 @@ describe('#create', () => { bar: 'foo-bar', // will not make it to the saved object attributes }; - const savedObject:SavedObject = { + const savedObject: SavedObject = { id, type: 'space', references: [], @@ -193,7 +193,7 @@ describe('#create', () => { }, }; - const expectedReturnedSpace:Space = { + const expectedReturnedSpace: Space = { id, ...attributes, }; @@ -252,12 +252,12 @@ describe('#create', () => { describe('#update', () => { const attributes = { - name: 'foo-name', - description: 'foo-description', - color: '#FFFFFF', - initials: 'FB', - imageUrl: "go-bots/predates/transformers", - disabledFeatures: [], + name: 'foo-name', + description: 'foo-description', + color: '#FFFFFF', + initials: 'FB', + imageUrl: 'go-bots/predates/transformers', + disabledFeatures: [], }; const spaceToUpdate = { @@ -267,7 +267,7 @@ describe('#update', () => { bar: 'foo-bar', // will not make it to the saved object attributes }; - const savedObject:SavedObject = { + const savedObject: SavedObject = { id: 'foo', type: 'space', references: [], @@ -278,7 +278,7 @@ describe('#update', () => { }, }; - const expectedReturnedSpace:Space = { + const expectedReturnedSpace: Space = { id: 'foo', ...attributes, _reserved: true, @@ -303,7 +303,7 @@ describe('#update', () => { describe('#delete', () => { const id = 'foo'; - const reservedSavedObject:SavedObject = { + const reservedSavedObject: SavedObject = { id, type: 'space', references: [], @@ -315,7 +315,7 @@ describe('#delete', () => { }, }; - const notReservedSavedObject:SavedObject = { + const notReservedSavedObject: SavedObject = { id, type: 'space', references: [], @@ -363,12 +363,7 @@ describe('#disableLegacyUrlAliases', () => { const mockConfig = createMockConfig(); const mockCallWithRequestRepository = savedObjectsRepositoryMock.create(); - const client = new SpacesClient( - mockDebugLogger, - mockConfig, - mockCallWithRequestRepository, - [] - ); + const client = new SpacesClient(mockDebugLogger, mockConfig, mockCallWithRequestRepository, []); const aliases = [ { targetSpace: 'space1', targetType: 'foo', sourceId: '123' }, { targetSpace: 'space2', targetType: 'bar', sourceId: '456' }, diff --git a/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts b/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts index 62f9b554b3be8..f1d7c271353a2 100644 --- a/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts +++ b/x-pack/plugins/spaces/server/spaces_client/spaces_client.ts @@ -169,15 +169,15 @@ export class SpacesClient implements ISpacesClient { await this.repository.bulkUpdate(objectsToUpdate); } - private transformSavedObjectToSpace(savedObject: SavedObject):v1.Space { + private transformSavedObjectToSpace(savedObject: SavedObject): v1.Space { return { id: savedObject.id, - name: savedObject.attributes.name??'', + name: savedObject.attributes.name ?? '', description: savedObject.attributes.description, color: savedObject.attributes.color, initials: savedObject.attributes.initials, imageUrl: savedObject.attributes.imageUrl, - disabledFeatures: savedObject.attributes.disabledFeatures??[], + disabledFeatures: savedObject.attributes.disabledFeatures ?? [], _reserved: savedObject.attributes._reserved, } as v1.Space; } @@ -189,7 +189,7 @@ export class SpacesClient implements ISpacesClient { color: space.color, initials: space.initials, imageUrl: space.imageUrl, - disabledFeatures: space.disabledFeatures - } + disabledFeatures: space.disabledFeatures, + }; } } From c36ebb2394e99b06f526ecda62b097a8dd113fd3 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 28 Jun 2023 15:06:22 +0000 Subject: [PATCH 7/7] [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix' --- .../plugins/spaces/server/spaces_client/spaces_client.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts b/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts index a1fe2e13ab897..ff63f71e613df 100644 --- a/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts +++ b/x-pack/plugins/spaces/server/spaces_client/spaces_client.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { SavedObject } from '@kbn/core-saved-objects-server'; +import type { SavedObject } from '@kbn/core-saved-objects-server'; import { savedObjectsRepositoryMock } from '@kbn/core/server/mocks'; import type { GetAllSpacesPurpose, Space } from '../../common';