Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devrel 1075/add spaces import #13

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/commands/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { languagesEntity } from "./importExportEntities/entities/languages.js";
import { languageVariantsEntity } from "./importExportEntities/entities/languageVariants.js";
import { previewUrlsExportEntity } from "./importExportEntities/entities/previewUrls.js";
import { rolesExportEntity } from "./importExportEntities/entities/roles.js";
import { spacesExportEntity } from "./importExportEntities/entities/spaces.js";
import { spacesEntity } from "./importExportEntities/entities/spaces.js";
import { taxonomiesEntity } from "./importExportEntities/entities/taxonomies.js";
import { workflowsEntity } from "./importExportEntities/entities/workflows.js";
import { EntityExportDefinition } from "./importExportEntities/entityDefinition.js";
Expand Down Expand Up @@ -51,7 +51,7 @@ export const register: RegisterCommand = yargs =>

const entityDefinitions: ReadonlyArray<EntityExportDefinition<any>> = [
collectionsEntity,
spacesExportEntity,
spacesEntity,
taxonomiesEntity,
languagesEntity,
previewUrlsExportEntity,
Expand Down
4 changes: 4 additions & 0 deletions src/commands/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from "./importExportEntities/entities/contentTypesSnippets.js";
import { languagesEntity } from "./importExportEntities/entities/languages.js";
import { languageVariantsEntity } from "./importExportEntities/entities/languageVariants.js";
import { spacesEntity } from "./importExportEntities/entities/spaces.js";
import { taxonomiesEntity } from "./importExportEntities/entities/taxonomies.js";
import { workflowsEntity } from "./importExportEntities/entities/workflows.js";
import { EntityImportDefinition, ImportContext } from "./importExportEntities/entityDefinition.js";
Expand Down Expand Up @@ -60,6 +61,7 @@ const entityDefinitions: ReadonlyArray<EntityImportDefinition<any>> = [
updateItemAndTypeReferencesInSnippetsImportEntity,
updateItemAndTypeReferencesInTypesImportEntity,
workflowsEntity,
spacesEntity,
languageVariantsEntity,
];

Expand Down Expand Up @@ -112,4 +114,6 @@ const createInitialContext = (): ImportContext => ({
contentTypeContextByOldIds: new Map(),
workflowIdsByOldIds: new Map(),
worfklowStepsIdsWithTransitionsByOldIds: new Map(),
spaceIdsByOldIds: new Map(),
contentTypeIdsWithElementsByOldIds: new Map(),
});
4 changes: 4 additions & 0 deletions src/commands/importExportEntities/entities/assetFolders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export const assetFoldersEntity: EntityDefinition<ReadonlyArray<AssetFolderContr
serializeEntities: JSON.stringify,
deserializeEntities: JSON.parse,
importEntities: async (client, fileFolders, context) => {
if (!fileFolders.length) {
return;
}

const projectFolders = await client
.modifyAssetFolders()
.withData(fileFolders.map(createPatchToAddFolder))
Expand Down
35 changes: 29 additions & 6 deletions src/commands/importExportEntities/entities/spaces.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
import { SpaceContracts } from "@kontent-ai/management-sdk";

import { serially } from "../../../utils/requests.js";
import { EntityDefinition } from "../entityDefinition.js";

export const spacesExportEntity: EntityDefinition<ReadonlyArray<SpaceContracts.ISpaceContract>> = {
export const spacesEntity: EntityDefinition<ReadonlyArray<SpaceContracts.ISpaceContract>> = {
name: "spaces",
fetchEntities: client => client.listSpaces().toPromise().then(res => res.rawData),
serializeEntities: spaces => JSON.stringify(spaces),
importEntities: () => {
throw new Error("Not supported yet.");
},
deserializeEntities: () => {
throw new Error("Not supported yet.");
deserializeEntities: JSON.parse,
importEntities: async (client, entities, context) => {
// does not add web_spotlight_root as it is not possible to activate Web Spotlight with MAPI
const newSpaces = await serially(entities.map(importSpace => () =>
client
.addSpace()
.withData({
name: importSpace.name,
codename: importSpace.codename,
}).toPromise()
));

return {
...context,
spaceIdsByOldIds: new Map(entities
.map(oldSpace => {
const match = newSpaces.find(s => s.data.codename === oldSpace.codename);

if (!match) {
throw new Error(
`Could not find space with codename ${oldSpace.codename} in the project. This should never happen`,
);
}

return [oldSpace.id, match.data.id];
})),
};
},
};
2 changes: 2 additions & 0 deletions src/commands/importExportEntities/entityDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export type ImportContext = Readonly<{
string,
Readonly<{ selfId: string; oldTransitionIds: ReadonlyArray<string> }>
>;
contentTypeIdsWithElementsByOldIds: ReadonlyMap<string, Readonly<{ selfId: string; elementIdsByOldIds: IdsMap }>>;
spaceIdsByOldIds: IdsMap;
}>;

type IdsMap = ReadonlyMap<string, string>;