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 1072/add workflow import #12

Merged
merged 2 commits into from
Dec 13, 2023
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 @@ -17,7 +17,7 @@ import { previewUrlsExportEntity } from "./importExportEntities/entities/preview
import { rolesExportEntity } from "./importExportEntities/entities/roles.js";
import { spacesExportEntity } from "./importExportEntities/entities/spaces.js";
import { taxonomiesEntity } from "./importExportEntities/entities/taxonomies.js";
import { workflowsExportEntity } from "./importExportEntities/entities/workflows.js";
import { workflowsEntity } from "./importExportEntities/entities/workflows.js";
import { EntityExportDefinition } from "./importExportEntities/entityDefinition.js";

const zip = new JSZip();
Expand Down Expand Up @@ -56,7 +56,7 @@ const entityDefinitions: ReadonlyArray<EntityExportDefinition<any>> = [
languagesEntity,
previewUrlsExportEntity,
rolesExportEntity,
workflowsExportEntity,
workflowsEntity,
contentTypesSnippetsEntity,
contentTypesEntity,
contentItemsExportEntity,
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 { taxonomiesEntity } from "./importExportEntities/entities/taxonomies.js";
import { workflowsEntity } from "./importExportEntities/entities/workflows.js";
import { EntityImportDefinition, ImportContext } from "./importExportEntities/entityDefinition.js";

export const register: RegisterCommand = yargs =>
Expand Down Expand Up @@ -57,6 +58,7 @@ const entityDefinitions: ReadonlyArray<EntityImportDefinition<any>> = [
contentItemsExportEntity,
updateItemAndTypeReferencesInSnippetsImportEntity,
updateItemAndTypeReferencesInTypesImportEntity,
workflowsEntity,
];

type ImportEntitiesParams = Readonly<{
Expand Down Expand Up @@ -106,4 +108,6 @@ const createInitialContext = (): ImportContext => ({
contentTypeSnippetIdsWithElementsByOldIds: new Map(),
contentItemIdsByOldIds: new Map(),
contentTypeIdsWithElementsByOldIds: new Map(),
workflowIdsByOldIds: new Map(),
worfklowStepsIdsByOldIds: new Map(),
});
3 changes: 2 additions & 1 deletion src/commands/importExportEntities/entities/languages.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { LanguageContracts, LanguageModels, ManagementClient } from "@kontent-ai/management-sdk";

import { emptyId } from "../../../constants/ids.js";
import { serially } from "../../../utils/requests.js";
import { notNull } from "../../../utils/typeguards.js";
import { EntityDefinition } from "../entityDefinition.js";

const defaultLanguageId = "00000000-0000-0000-0000-000000000000";
const defaultLanguageId = emptyId;

export const languagesEntity: EntityDefinition<ReadonlyArray<LanguageContracts.ILanguageModelContract>> = {
name: "languages",
Expand Down
118 changes: 111 additions & 7 deletions src/commands/importExportEntities/entities/workflows.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,119 @@
import { WorkflowContracts } from "@kontent-ai/management-sdk";
import { ManagementClient, WorkflowContracts, WorkflowModels } from "@kontent-ai/management-sdk";

import { EntityDefinition } from "../entityDefinition.js";
import { emptyId } from "../../../constants/ids.js";
import { zip } from "../../../utils/array.js";
import { serially } from "../../../utils/requests.js";
import { EntityDefinition, ImportContext } from "../entityDefinition.js";

export const workflowsExportEntity: EntityDefinition<WorkflowContracts.IListWorkflowsResponseContract> = {
const defaultWorkflowId = emptyId;

export const workflowsEntity: EntityDefinition<WorkflowContracts.IListWorkflowsResponseContract> = {
name: "workflows",
fetchEntities: client => client.listWorkflows().toPromise().then(res => res.rawData),
serializeEntities: collections => JSON.stringify(collections),
importEntities: () => {
throw new Error("Not supported yet.");
deserializeEntities: JSON.parse,
importEntities: async (client, importWfs, context) => {
const oldProjectDefaultWf = await client.listWorkflows().toPromise()
.then(res => res.data.find(w => w.id === defaultWorkflowId));
const importDefaultWf = importWfs.find(w => w.id === defaultWorkflowId);

if (!importDefaultWf || !oldProjectDefaultWf) {
throw new Error(`The default workflow is missing in the imported file or the project to import into.`);
}

const projectDefaultWf = await updateWorkflow(client, oldProjectDefaultWf, importDefaultWf, context);
const newProjectWfs = await addWorkflows(client, importWfs, context);

const newDefaultWfStepIdEntries = zip(extractAllStepIds(importDefaultWf), extractAllStepIds(projectDefaultWf));

return {
...context,
workflowIdsByOldIds: new Map([...newProjectWfs.workflows, [defaultWorkflowId, defaultWorkflowId]]),
worfklowStepsIdsByOldIds: new Map([...newProjectWfs.workflowSteps, ...newDefaultWfStepIdEntries]),
};
},
};

const createWorkflowData = (importWorkflow: WorkflowContracts.IWorkflowContract, context: ImportContext) => ({
...importWorkflow,
scopes: importWorkflow.scopes.map(scope => ({
content_types: scope.content_types
.map(type => ({ id: context.contentTypeIdsWithElementsByOldIds.get(type.id ?? "")?.selfId })),
collections: scope.collections.map(collection => ({ id: context.collectionIdsByOldIds.get(collection.id ?? "") })),
})),
steps: importWorkflow.steps.map(step => ({
...step,
role_ids: [],
transitions_to: step.transitions_to.map(transition => {
const transitionWorkflow = extractAllSteps(importWorkflow).find(s => s.id === transition.step.id);

if (!transitionWorkflow) {
throw new Error(`Could not find worklow step with id ${transition.step.id}. This should never happen.`);
}

return { step: { codename: transitionWorkflow.codename } };
}),
})),
published_step: {
...importWorkflow,
unpublish_role_ids: [],
create_new_version_role_ids: [],
},
deserializeEntities: () => {
throw new Error("Not supported yet.");
archived_step: {
...importWorkflow.archived_step,
role_ids: [],
},
});

const updateWorkflow = async (
client: ManagementClient,
projectWorkflow: WorkflowModels.Workflow,
importWorkflow: WorkflowContracts.IWorkflowContract,
context: ImportContext,
) =>
client
.updateWorkflow()
.byWorkflowId(projectWorkflow.id)
.withData(createWorkflowData(importWorkflow, context))
.toPromise()
.then(res => res.rawData);

const addWorkflows = async (
client: ManagementClient,
importWorkflows: WorkflowContracts.IListWorkflowsResponseContract,
context: ImportContext,
) => {
const responses = await serially(
importWorkflows
.filter(w => w.id !== defaultWorkflowId)
.map(importWorkflow => async () => {
const response = await client
.addWorkflow()
.withData(createWorkflowData(importWorkflow, context))
.toPromise()
.then(res => res.rawData);

const stepsIds = zip(extractAllStepIds(importWorkflow), extractAllStepIds(response));

return { workflow: [importWorkflow.id, response.id] as const, workflowSteps: stepsIds };
}),
);

return responses
.reduce<ContextWorkflowEntries>((prev, current) => ({
workflows: prev.workflows.concat([current.workflow]),
workflowSteps: prev.workflowSteps.concat(current.workflowSteps),
}), { workflows: [], workflowSteps: [] });
};

type ContextWorkflowEntries = Readonly<{
workflows: ReadonlyArray<readonly [string, string]>;
workflowSteps: ReadonlyArray<readonly [string, string]>;
}>;

type AnyStep = Readonly<{ id: string; name: string; codename: string }>;
const extractAllSteps = (wf: WorkflowContracts.IWorkflowContract): ReadonlyArray<AnyStep> =>
(wf.steps as AnyStep[]).concat([wf.scheduled_step, wf.published_step, wf.archived_step]);

const extractAllStepIds = (wf: WorkflowContracts.IWorkflowContract): ReadonlyArray<string> =>
extractAllSteps(wf).map(s => s.id);
2 changes: 2 additions & 0 deletions src/commands/importExportEntities/entityDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export type ImportContext = Readonly<{
>;
contentTypeIdsWithElementsByOldIds: ReadonlyMap<string, Readonly<{ selfId: string; elementIdsByOldIds: IdsMap }>>;
contentItemIdsByOldIds: IdsMap;
workflowIdsByOldIds: IdsMap;
worfklowStepsIdsByOldIds: IdsMap;
}>;

type IdsMap = ReadonlyMap<string, string>;
1 change: 1 addition & 0 deletions src/constants/ids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const emptyId = "00000000-0000-0000-0000-000000000000";