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

TA-3148: Update NodeExportTransport and export/import logic to work with JSON files #186

Merged
merged 7 commits into from
Jul 19, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@celonis/content-cli",
"version": "0.9.0",
"version": "0.10.0",
"description": "CLI Tool to help manage content in Celonis EMS",
"main": "content-cli.js",
"bin": {
Expand Down
8 changes: 4 additions & 4 deletions src/interfaces/batch-export-import-constants.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export enum BatchExportImportConstants {
STUDIO_FILE_NAME = "studio.yml",
VARIABLES_FILE_NAME = "variables.yml",
MANIFEST_FILE_NAME = "manifest.yml",
STUDIO_FILE_NAME = "studio.json",
VARIABLES_FILE_NAME = "variables.json",
MANIFEST_FILE_NAME = "manifest.json",
STUDIO = "STUDIO",
APP_MODE_VIEWER = "VIEWER",
ZIP_EXTENSION = ".zip",
YAML_EXTENSION = ".yml",
JSON_EXTENSION = ".json",
NODES_FOLDER_NAME = "nodes/",
SCENARIO_NODE = "SCENARIO"
}
7 changes: 4 additions & 3 deletions src/interfaces/package-export-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface NodeExportTransport {
name: string;
type: string;
exportSerializationType: string;
configuration: string;
configuration: NodeConfiguration;
schemaVersion: number;

spaceId: string;
Expand All @@ -60,8 +60,9 @@ export interface NodeExportTransport {
serializedDocument?: Buffer;
}

export interface NodeSerializedContent {
variables: VariableDefinition[]
ksalihu marked this conversation as resolved.
Show resolved Hide resolved
export interface NodeConfiguration {
variables?: VariableDefinition[];
[key: string]: any;
}

export interface StudioPackageManifest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "../../interfaces/package-export-transport";
import {FileService, fileService} from "../file-service";
import {studioService} from "../studio/studio.service";
import {parse, stringify} from "../../util/yaml"
import {parse, stringify} from "../../util/json"
import * as FormData from "form-data";
import {BatchExportImportConstants} from "../../interfaces/batch-export-import-constants";
import {packageApi} from "../../api/package-api";
Expand Down
15 changes: 7 additions & 8 deletions src/services/studio/studio.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
NodeConfiguration,
NodeExportTransport,
NodeSerializedContent,
PackageExportTransport,
PackageKeyAndVersionPair,
StudioPackageManifest,
VariableExportTransport,
VariableManifestTransport
VariableManifestTransport,
} from "../../interfaces/package-export-transport";

import {packageApi} from "../../api/package-api";
Expand All @@ -17,7 +17,7 @@ import {
} from "../../interfaces/package-manager.interfaces";
import {dataModelService} from "../package-manager/datamodel-service";
import {IZipEntry} from "adm-zip";
import {parse, stringify} from "../../util/yaml";
import {parse, stringify} from "../../util/json";
import {nodeApi} from "../../api/node-api";
import {variablesApi} from "../../api/variables-api";
import {spaceApi} from "../../api/space-api";
Expand Down Expand Up @@ -140,7 +140,7 @@ class StudioService {
}

private deleteScenarioAssets(packageZip: AdmZip): void {
packageZip.getEntries().filter(entry => entry.entryName.startsWith(BatchExportImportConstants.NODES_FOLDER_NAME) && entry.entryName.endsWith(BatchExportImportConstants.YAML_EXTENSION))
packageZip.getEntries().filter(entry => entry.entryName.startsWith(BatchExportImportConstants.NODES_FOLDER_NAME) && entry.entryName.endsWith(BatchExportImportConstants.JSON_EXTENSION))
.forEach(entry => {
const node: NodeExportTransport = parse(entry.getData().toString());
if (node.type === BatchExportImportConstants.SCENARIO_NODE) {
Expand All @@ -157,18 +157,17 @@ class StudioService {
return;
}

const packageEntry = packageZip.getEntry("package.yml");
const packageEntry = packageZip.getEntry("package.json");

const exportedNode: NodeExportTransport = parse(packageEntry.getData().toString());
const nodeContent: NodeSerializedContent = parse(exportedNode.configuration);
const nodeContent: NodeConfiguration = exportedNode.configuration;

nodeContent.variables = nodeContent.variables.map(variable => ({
...variable,
metadata: variable.type === PackageManagerVariableType.CONNECTION ?
connectionVariablesByKey.get(variable.key).metadata : variable.metadata
}));

exportedNode.configuration = stringify(nodeContent);
packageZip.updateFile(packageEntry, Buffer.from(stringify(exportedNode)));
}

Expand Down Expand Up @@ -212,7 +211,7 @@ class StudioService {

const packageZip = new AdmZip(file.getData());
packageZip.getEntries().forEach(nodeFile => {
if (nodeFile.entryName.endsWith(BatchExportImportConstants.YAML_EXTENSION)) {
if (nodeFile.entryName.endsWith(BatchExportImportConstants.JSON_EXTENSION)) {
const updatedNodeFile = this.updateSpaceIdForNode(nodeFile.getData().toString(), spaceId);
packageZip.updateFile(nodeFile, Buffer.from(updatedNodeFile));
}
Expand Down
15 changes: 15 additions & 0 deletions src/util/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

export function stringify(data: any): string {
return JSON.stringify(data, replacer, 2);
}

export function parse<T>(data: string): T {
return JSON.parse(data);
}

const replacer = (key, value) => {
if (value instanceof Map) {
return Object.fromEntries(value);
}
return value;
};
9 changes: 4 additions & 5 deletions tests/config/config-diff.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {PackageManifestTransport} from "../../src/interfaces/package-export-transport";
import {ConfigUtils} from "../utls/config-utils";
import * as path from "path";
import {stringify} from "../../src/util/yaml";
import {mockCreateReadStream, mockExistsSync, mockReadFileSync} from "../utls/fs-mock-utils";
import {
PackageDiffMetadata,
Expand All @@ -22,7 +21,7 @@ describe("Config diff", () => {
const manifest: PackageManifestTransport[] = [];
manifest.push(ConfigUtils.buildManifestForKeyAndFlavor("package-key", "STUDIO"));

const firstPackageNode = ConfigUtils.buildPackageNode("package-key", stringify({metadata: {description: "test"}, variables: [], dependencies: []}));
const firstPackageNode = ConfigUtils.buildPackageNode("package-key", {metadata: {description: "test"}, variables: [], dependencies: []});
const firstChildNode = ConfigUtils.buildChildNode("key-1", "package-key", "TEST");
const firstPackageZip = ConfigUtils.buildExportPackageZip(firstPackageNode, [firstChildNode], "1.0.0");
const exportedPackagesZip = ConfigUtils.buildBatchExportZip(manifest, [firstPackageZip]);
Expand All @@ -49,7 +48,7 @@ describe("Config diff", () => {
const manifest: PackageManifestTransport[] = [];
manifest.push(ConfigUtils.buildManifestForKeyAndFlavor("package-key", "STUDIO"));

const firstPackageNode = ConfigUtils.buildPackageNode("package-key", stringify({metadata: {description: "test"}, variables: [], dependencies: []}));
const firstPackageNode = ConfigUtils.buildPackageNode("package-key", {metadata: {description: "test"}, variables: [], dependencies: []});
const firstChildNode = ConfigUtils.buildChildNode("key-1", "package-key", "TEST");
const firstPackageZip = ConfigUtils.buildExportPackageZip(firstPackageNode, [firstChildNode], "1.0.0");
const exportedPackagesZip = ConfigUtils.buildBatchExportZip(manifest, [firstPackageZip]);
Expand Down Expand Up @@ -91,7 +90,7 @@ describe("Config diff", () => {
const manifest: PackageManifestTransport[] = [];
manifest.push(ConfigUtils.buildManifestForKeyAndFlavor("package-key", "STUDIO"));

const firstPackageNode = ConfigUtils.buildPackageNode("package-key", stringify({metadata: {description: "test"}, variables: [], dependencies: []}));
const firstPackageNode = ConfigUtils.buildPackageNode("package-key", {metadata: {description: "test"}, variables: [], dependencies: []});
const firstChildNode = ConfigUtils.buildChildNode("key-1", "package-key", "TEST");
const firstPackageZip = ConfigUtils.buildExportPackageZip(firstPackageNode, [firstChildNode], "1.0.0");
const exportedPackagesZip = ConfigUtils.buildBatchExportZip(manifest, [firstPackageZip]);
Expand Down Expand Up @@ -137,7 +136,7 @@ describe("Config diff", () => {
const manifest: PackageManifestTransport[] = [];
manifest.push(ConfigUtils.buildManifestForKeyAndFlavor("package-key", "STUDIO"));

const firstPackageNode = ConfigUtils.buildPackageNode("package-key", stringify({metadata: {description: "test"}, variables: [], dependencies: []}));
const firstPackageNode = ConfigUtils.buildPackageNode("package-key", {metadata: {description: "test"}, variables: [], dependencies: []});
const firstChildNode = ConfigUtils.buildChildNode("key-1", "package-key", "TEST");
const firstPackageZip = ConfigUtils.buildExportPackageZip(firstPackageNode, [firstChildNode], "1.0.0");
const exportedPackagesZip = ConfigUtils.buildBatchExportZip(manifest, [firstPackageZip]);
Expand Down
44 changes: 22 additions & 22 deletions tests/config/config-export.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {ConfigUtils} from "../utls/config-utils";
import {
DependencyTransport, NodeExportTransport, NodeSerializedContent,
DependencyTransport, NodeConfiguration, NodeExportTransport,
PackageManifestTransport,
StudioPackageManifest,
VariableManifestTransport
VariableManifestTransport,
} from "../../src/interfaces/package-export-transport";
import {mockAxiosGet, mockAxiosPost, mockedPostRequestBodyByUrl} from "../utls/http-requests-mock";
import {ConfigCommand} from "../../src/commands/config.command";
Expand All @@ -13,7 +13,7 @@ import {FileService} from "../../src/services/file-service";
import * as fs from "fs";
import AdmZip = require("adm-zip");

import { parse, stringify } from "../../src/util/yaml";
import { parse, stringify } from "../../src/util/json";
import {
PackageManagerVariableType,
VariableDefinition,
Expand Down Expand Up @@ -109,7 +109,7 @@ describe("Config export", () => {
}
];

const firstPackageNode = ConfigUtils.buildPackageNode("key-1", stringify({variables: firstPackageVariableDefinition}));
const firstPackageNode = ConfigUtils.buildPackageNode("key-1", {variables: firstPackageVariableDefinition});
const firstPackageZip = ConfigUtils.buildExportPackageZip(firstPackageNode, [], "1.0.0");

const secondPackageVariableDefinition: VariableDefinition[] = [
Expand All @@ -125,7 +125,7 @@ describe("Config export", () => {
}
];

const secondPackageNode = ConfigUtils.buildPackageNode("key-2", stringify({variables: secondPackageVariableDefinition}));
const secondPackageNode = ConfigUtils.buildPackageNode("key-2", {variables: secondPackageVariableDefinition});
const secondPackageZip = ConfigUtils.buildExportPackageZip(secondPackageNode, [], "1.0.0");

const exportedPackagesZip = ConfigUtils.buildBatchExportZip(manifest, [firstPackageZip, secondPackageZip]);
Expand Down Expand Up @@ -258,12 +258,12 @@ describe("Config export", () => {
manifest.push(ConfigUtils.buildManifestForKeyAndFlavor("key-1", BatchExportImportConstants.STUDIO));
manifest.push(ConfigUtils.buildManifestForKeyAndFlavor("key-2", BatchExportImportConstants.STUDIO));

const firstPackageNode = ConfigUtils.buildPackageNode("key-1", "");
const firstPackageNode = ConfigUtils.buildPackageNode("key-1", {});
const firstPackageScenarioChild = ConfigUtils.buildChildNode("child-1-scenario", firstPackageNode.key, "SCENARIO");
const firstPackageTestChild = ConfigUtils.buildChildNode("child-2", firstPackageNode.key, "TEST");
const firstPackageZip = ConfigUtils.buildExportPackageZip(firstPackageNode, [firstPackageScenarioChild, firstPackageTestChild], "1.0.0");

const secondPackageNode = ConfigUtils.buildPackageNode("key-2", "");
const secondPackageNode = ConfigUtils.buildPackageNode("key-2", {});
const secondPackageScenarioChild = ConfigUtils.buildChildNode("child-3-scenario", secondPackageNode.key, "SCENARIO");
const secondPackageTestChild = ConfigUtils.buildChildNode("child-4", secondPackageNode.key, "TEST");
const secondPackageZip = ConfigUtils.buildExportPackageZip(secondPackageNode, [secondPackageScenarioChild, secondPackageTestChild], "1.0.0");
Expand All @@ -287,15 +287,15 @@ describe("Config export", () => {
const actualZip = new AdmZip(fileBuffer);

const firstPackageExportedZip = new AdmZip(actualZip.getEntry("key-1_1.0.0.zip").getData());
expect(firstPackageExportedZip.getEntry("nodes/child-1-scenario.yml")).toBeNull();
expect(firstPackageExportedZip.getEntry("nodes/child-2.yml").getData().toString()).toEqual(stringify(firstPackageTestChild));
expect(firstPackageExportedZip.getEntry("nodes/child-1-scenario.json")).toBeNull();
expect(firstPackageExportedZip.getEntry("nodes/child-2.json").getData().toString()).toEqual(stringify(firstPackageTestChild));

const secondPackageExportedZip = new AdmZip(actualZip.getEntry("key-2_1.0.0.zip").getData());
expect(secondPackageExportedZip.getEntry("nodes/child-3-scenario.yml")).toBeNull();
expect(secondPackageExportedZip.getEntry("nodes/child-4.yml").getData().toString()).toEqual(stringify(secondPackageTestChild));
expect(secondPackageExportedZip.getEntry("nodes/child-3-scenario.json")).toBeNull();
expect(secondPackageExportedZip.getEntry("nodes/child-4.json").getData().toString()).toEqual(stringify(secondPackageTestChild));
})

it("Should add appName to metadata for CONNECTION variables of package.yml files", async () => {
it("Should add appName to metadata for CONNECTION variables of package.json files", async () => {
const manifest: PackageManifestTransport[] = [];
manifest.push(ConfigUtils.buildManifestForKeyAndFlavor("key-1", BatchExportImportConstants.STUDIO));
manifest.push(ConfigUtils.buildManifestForKeyAndFlavor("key-2", BatchExportImportConstants.STUDIO));
Expand All @@ -313,7 +313,7 @@ describe("Config export", () => {
}
];

const firstPackageNode = ConfigUtils.buildPackageNode("key-1", stringify({variables: firstPackageVariableDefinition}));
const firstPackageNode = ConfigUtils.buildPackageNode("key-1", {variables: firstPackageVariableDefinition});
const firstPackageZip = ConfigUtils.buildExportPackageZip(firstPackageNode, [], "1.0.0");

const secondPackageVariableDefinition: VariableDefinition[] = [
Expand All @@ -332,7 +332,7 @@ describe("Config export", () => {
}
];

const secondPackageNode = ConfigUtils.buildPackageNode("key-2", stringify({variables: secondPackageVariableDefinition}));
const secondPackageNode = ConfigUtils.buildPackageNode("key-2", {variables: secondPackageVariableDefinition});
const secondPackageZip = ConfigUtils.buildExportPackageZip(secondPackageNode, [], "1.0.0");

const exportedPackagesZip = ConfigUtils.buildBatchExportZip(manifest, [firstPackageZip, secondPackageZip]);
Expand Down Expand Up @@ -403,9 +403,9 @@ describe("Config export", () => {
const actualZip = new AdmZip(fileBuffer);

const firstPackageExportedZip = new AdmZip(actualZip.getEntry("key-1_1.0.0.zip").getData());
const firstPackageExportedNode: NodeExportTransport = parse(firstPackageExportedZip.getEntry("package.yml").getData().toString());
const firstPackageExportedNode: NodeExportTransport = parse(firstPackageExportedZip.getEntry("package.json").getData().toString());
expect(firstPackageExportedNode).toBeTruthy();
const firstPackageContent: NodeSerializedContent = parse(firstPackageExportedNode.configuration);
const firstPackageContent: NodeConfiguration = firstPackageExportedNode.configuration;
expect(firstPackageContent.variables).toHaveLength(2);
expect(firstPackageContent.variables).toEqual([
{
Expand All @@ -420,9 +420,9 @@ describe("Config export", () => {
]);

const secondPackageExportedZip = new AdmZip(actualZip.getEntry("key-2_1.0.0.zip").getData());
const secondPackageExportedNode: NodeExportTransport = parse(secondPackageExportedZip.getEntry("package.yml").getData().toString());
const secondPackageExportedNode: NodeExportTransport = parse(secondPackageExportedZip.getEntry("package.json").getData().toString());
expect(secondPackageExportedNode).toBeTruthy();
const secondPackageContent: NodeSerializedContent = parse(secondPackageExportedNode.configuration);
const secondPackageContent: NodeConfiguration = secondPackageExportedNode.configuration;
expect(secondPackageContent.variables).toHaveLength(2);
expect(secondPackageContent.variables).toEqual([{
...secondPackageVariableDefinition[0],
Expand Down Expand Up @@ -458,7 +458,7 @@ describe("Config export", () => {
}
];

const firstPackageNode = ConfigUtils.buildPackageNode("key_with_underscores_1", stringify({variables: firstPackageVariableDefinition}));
const firstPackageNode = ConfigUtils.buildPackageNode("key_with_underscores_1", {variables: firstPackageVariableDefinition});
const firstPackageScenarioChild = ConfigUtils.buildChildNode("child-1-scenario", firstPackageNode.key, "SCENARIO");
const firstPackageZip = ConfigUtils.buildExportPackageZip(firstPackageNode, [firstPackageScenarioChild], "1.0.0");

Expand Down Expand Up @@ -511,9 +511,9 @@ describe("Config export", () => {
const actualZip = new AdmZip(fileBuffer);

const firstPackageExportedZip = new AdmZip(actualZip.getEntry("key_with_underscores_1_1.0.0.zip").getData());
const firstPackageExportedNode: NodeExportTransport = parse(firstPackageExportedZip.getEntry("package.yml").getData().toString());
const firstPackageExportedNode: NodeExportTransport = parse(firstPackageExportedZip.getEntry("package.json").getData().toString());
expect(firstPackageExportedNode).toBeTruthy();
const firstPackageContent: NodeSerializedContent = parse(firstPackageExportedNode.configuration);
const firstPackageContent: NodeConfiguration = firstPackageExportedNode.configuration;
expect(firstPackageContent.variables).toHaveLength(3);
expect(firstPackageContent.variables).toEqual([
{
Expand All @@ -532,7 +532,7 @@ describe("Config export", () => {
}
]);

expect(firstPackageExportedZip.getEntry("nodes/child-1-scenario.yml")).toBeNull();
expect(firstPackageExportedZip.getEntry("nodes/child-1-scenario.json")).toBeNull();
})

it("Should export by packageKeys without dependencies", async () => {
Expand Down
Loading
Loading