Skip to content

Commit

Permalink
Merge pull request #165 from celonis/TA-2747-listing-across-flavors-c…
Browse files Browse the repository at this point in the history
…ommands

[TA-2747] Command for listing active packages across flavors
  • Loading branch information
LaberionAjvazi authored Jan 18, 2024
2 parents c35bf3a + d94dfcc commit 6996e46
Show file tree
Hide file tree
Showing 13 changed files with 450 additions and 7 deletions.
31 changes: 31 additions & 0 deletions src/api/batch-import-export-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {PackageExportTransport} from "../interfaces/package-export-transport";
import {httpClientV2} from "../services/http-client-service.v2";
import {FatalError} from "../util/logger";

class BatchImportExportApi {
public static readonly INSTANCE = new BatchImportExportApi();

public findAllActivePackages(flavors: string[], withDependencies: boolean = false): Promise<PackageExportTransport[]> {
const queryParams = new URLSearchParams();

queryParams.set("withDependencies", withDependencies.toString());
flavors.forEach(flavor => queryParams.append("flavors", flavor))

return httpClientV2.get(`/package-manager/api/core/packages/export/list?${queryParams.toString()}`).catch(e => {
throw new FatalError(`Problem getting active packages: ${e}`);
});
}

public findActivePackagesByKeys(packageKeys: string[], withDependencies: boolean = false): Promise<PackageExportTransport[]> {
const queryParams = new URLSearchParams();

packageKeys.forEach(key => queryParams.append("packageKeys", key))
queryParams.set("withDependencies", withDependencies.toString());

return httpClientV2.get(`/package-manager/api/core/packages/export/list-by-keys?${queryParams.toString()}`).catch(e => {
throw new FatalError(`Problem getting active packages by keys: ${e}`);
});
}
}

export const batchImportExportApi = BatchImportExportApi.INSTANCE;
12 changes: 12 additions & 0 deletions src/commands/config.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {batchImportExportService} from "../services/package-manager/batch-import-export-service";

export class ConfigCommand {

public async listActivePackages(jsonResponse: boolean, flavors: string[], withDependencies: boolean, packageKeys:string[]): Promise<void> {
if (jsonResponse) {
await batchImportExportService.findAndExportListOfActivePackages(flavors ?? [], packageKeys ?? [], withDependencies)
} else {
await batchImportExportService.listActivePackages(flavors ?? []);
}
}
}
45 changes: 45 additions & 0 deletions src/content-cli-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {ConfigCommand} from "./commands/config.command";
import commander = require("commander");
import {logger} from "./util/logger";
import {ContextInitializer} from "./util/context-initializer";

type CommanderStatic = commander.CommanderStatic;

export class Config {
public static list(program: CommanderStatic): CommanderStatic {
program
.command("list")
.description("Command to list active packages that can be exported")
.option("-p, --profile <profile>", "Profile which you want to use to list possible variable assignments")
.option("--json", "Return response as json type", "")
.option("--flavors <flavors...>", "Lists only active packages of the given flavors")
.option("--withDependencies", "Include dependencies", "")
.option("--packageKeys <packageKeys...>", "Lists only given package keys")
.action(async cmd => {
await new ConfigCommand().listActivePackages(cmd.json, cmd.flavors, cmd.withDependencies, cmd.packageKeys);
process.exit();
});

return program;
}
}

process.on("unhandledRejection", (e, promise) => {
logger.error(e.toString());
});

const loadAllCommands = () => {
Config.list(commander);
commander.parse(process.argv);
};

ContextInitializer.initContext()
.then(loadAllCommands)
.catch(e => {
logger.error(e);
});

if (!process.argv.slice(2).length) {
commander.outputHelp();
process.exit(1);
}
2 changes: 2 additions & 0 deletions src/content-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ program.command("get", "Commands to get configuration properties.");

program.command("set", "Commands to set configuration properties.");

program.command("config", "Commands related to config management")

program.version(VersionUtils.getCurrentCliVersion());
program.parse(process.argv);

Expand Down
20 changes: 20 additions & 0 deletions src/interfaces/package-export-transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {StudioComputeNodeDescriptor} from "./package-manager.interfaces";

export interface DependencyTransport {
key: string;
version: string;
}

export interface PackageExportTransport {
id: string;
key: string;
name: string;
changeDate: string;
activatedDraftId: string;
workingDraftId: string;
flavor: string;
version: string;
dependencies: DependencyTransport[];
spaceId?: string;
datamodels?: StudioComputeNodeDescriptor[];
}
1 change: 1 addition & 0 deletions src/interfaces/package-manager.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface PackageWithVariableAssignments {
key: string;
name: string;
createdBy: string;
spaceId: string;
variableAssignments: VariablesAssignments[]
}

Expand Down
38 changes: 38 additions & 0 deletions src/services/package-manager/batch-import-export-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {batchImportExportApi} from "../../api/batch-import-export-api";
import {logger} from "../../util/logger";
import {v4 as uuidv4} from "uuid";
import {PackageExportTransport} from "../../interfaces/package-export-transport";
import {FileService, fileService} from "../file-service";
import {studioService} from "../studio/studio.service";

class BatchImportExportService {

public async listActivePackages(flavors: string[]): Promise<void> {
const activePackages = await batchImportExportApi.findAllActivePackages(flavors);
activePackages.forEach(pkg => {
logger.info(`${pkg.name} - Key: "${pkg.key}"`)
});
}

public async findAndExportListOfActivePackages(flavors: string[], packageKeys: string[], withDependencies: boolean): Promise<void> {
let packagesToExport: PackageExportTransport[];

if (packageKeys.length) {
packagesToExport = await batchImportExportApi.findActivePackagesByKeys(packageKeys, withDependencies);
} else {
packagesToExport = await batchImportExportApi.findAllActivePackages(flavors, withDependencies);
}

packagesToExport = await studioService.getExportPackagesWithStudioData(packagesToExport, withDependencies);

this.exportListOfPackages(packagesToExport);
}

private exportListOfPackages(packages: PackageExportTransport[]): void {
const filename = uuidv4() + ".json";
fileService.writeToFileWithGivenName(JSON.stringify(packages), filename);
logger.info(FileService.fileDownloadedMessage + filename);
}
}

export const batchImportExportService = new BatchImportExportService();
57 changes: 57 additions & 0 deletions src/services/studio/studio.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {PackageExportTransport} from "../../interfaces/package-export-transport";
import {packageApi} from "../../api/package-api";
import {
PackageManagerVariableType,
PackageWithVariableAssignments,
StudioComputeNodeDescriptor
} from "../../interfaces/package-manager.interfaces";
import {dataModelService} from "../package-manager/datamodel-service";

class StudioService {

public async getExportPackagesWithStudioData(packagesToExport: PackageExportTransport[], withDependencies: boolean): Promise<PackageExportTransport[]> {
const studioPackagesWithDataModels = await packageApi.findAllPackagesWithVariableAssignments(PackageManagerVariableType.DATA_MODEL);

packagesToExport = studioService.setSpaceIdForStudioPackages(packagesToExport, studioPackagesWithDataModels);

if (withDependencies) {
const dataModelDetailsByNode = await dataModelService.getDataModelDetailsForPackages(studioPackagesWithDataModels);
packagesToExport = studioService.setDataModelsForStudioPackages(packagesToExport, studioPackagesWithDataModels, dataModelDetailsByNode);
}

return packagesToExport;
}

private setSpaceIdForStudioPackages(packages: PackageExportTransport[], studioPackages: PackageWithVariableAssignments[]): PackageExportTransport[] {
const studioPackageByKey = new Map<string, PackageWithVariableAssignments>();
studioPackages.forEach(pkg => studioPackageByKey.set(pkg.key, pkg));

return packages.map(pkg => {
return studioPackageByKey.has(pkg.key) ? {
...pkg,
spaceId: studioPackageByKey.get(pkg.key).spaceId
} : pkg;
});
}

private setDataModelsForStudioPackages(packages: PackageExportTransport[],
studioPackageWithDataModels: PackageWithVariableAssignments[],
dataModelDetailsByNode: Map<string, StudioComputeNodeDescriptor[]>): PackageExportTransport[] {
const studioPackageByKey = new Map<string, PackageWithVariableAssignments>();
studioPackageWithDataModels.forEach(pkg => studioPackageByKey.set(pkg.key, pkg));

return packages.map(pkg => {
return studioPackageByKey.has(pkg.key) ? {
...pkg,
datamodels: dataModelDetailsByNode.get(pkg.key)
.map(dataModel => ({
name: dataModel.name,
poolId: dataModel.poolId,
dataModelId: dataModel.dataModelId
}))
} : pkg;
});
}
}

export const studioService = new StudioService();
Loading

0 comments on commit 6996e46

Please sign in to comment.