Skip to content

Commit dd03ef5

Browse files
committed
SP-160: added branch awareness for listing packages by introducing --branches option
1 parent 57aa3f7 commit dd03ef5

5 files changed

Lines changed: 99 additions & 30 deletions

File tree

src/commands/configuration-management/batch-import-export.service.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@ export class BatchImportExportService {
3535
this.gitService = new GitService(context);
3636
}
3737

38-
public async listActivePackages(flavors: string[]): Promise<void> {
39-
const activePackages = await this.batchImportExportApi.findAllActivePackages(flavors);
38+
public async listActivePackages(flavors: string[], includeBranches: boolean): Promise<void> {
39+
const activePackages = await this.batchImportExportApi.findAllActivePackages(flavors, false, includeBranches);
4040
activePackages.forEach(pkg => {
4141
logger.info(`${pkg.name} - Key: "${pkg.key}"`)
4242
});
4343
}
4444

45-
public async findAndExportListOfPackages(flavors: string[], packageKeys: string[], keysByVersion: string[], withDependencies: boolean): Promise<void> {
45+
public async findAndExportListOfPackages(flavors: string[], packageKeys: string[], keysByVersion: string[], withDependencies: boolean, includeBranches: boolean): Promise<void> {
4646
let packagesToExport: PackageExportTransport[];
4747

4848
if (keysByVersion.length) {
4949
packagesToExport = await this.batchImportExportApi.findPackagesByKeysAndVersion(keysByVersion, withDependencies);
5050
} else if (packageKeys.length) {
5151
packagesToExport = await this.batchImportExportApi.findActivePackagesByKeys(packageKeys, withDependencies);
5252
} else {
53-
packagesToExport = await this.batchImportExportApi.findAllActivePackages(flavors, withDependencies);
53+
packagesToExport = await this.batchImportExportApi.findAllActivePackages(flavors, withDependencies, includeBranches);
5454
}
5555

5656
packagesToExport = await this.studioService.getExportPackagesWithStudioData(packagesToExport, withDependencies);
@@ -159,16 +159,16 @@ export class BatchImportExportService {
159159
logger.info("Config import report file: " + reportFileName);
160160
}
161161

162-
public async findAndExportListOfActivePackagesByVariableValue(flavors: string[], variableValue: string, variableType: string): Promise<void> {
163-
let packagesToExport = await this.batchImportExportApi.findActivePackagesByVariableValue(flavors, variableValue, variableType);
162+
public async findAndExportListOfActivePackagesByVariableValue(flavors: string[], variableValue: string, variableType: string, includeBranches: boolean): Promise<void> {
163+
let packagesToExport = await this.batchImportExportApi.findActivePackagesByVariableValue(flavors, variableValue, variableType, includeBranches);
164164

165165
packagesToExport = await this.studioService.getExportPackagesWithStudioData(packagesToExport, false);
166166

167167
this.exportListOfPackages(packagesToExport);
168168
}
169169

170-
public async listActivePackagesByVariableValue(flavors: string[], variableValue: string, variableType: string) : Promise<void> {
171-
const packagesByVariableValue = await this.batchImportExportApi.findActivePackagesByVariableValue(flavors, variableValue, variableType);
170+
public async listActivePackagesByVariableValue(flavors: string[], variableValue: string, variableType: string, includeBranches: boolean) : Promise<void> {
171+
const packagesByVariableValue = await this.batchImportExportApi.findActivePackagesByVariableValue(flavors, variableValue, variableType, includeBranches);
172172
packagesByVariableValue.forEach(pkg => {
173173
logger.info(`${pkg.name} - Key: "${pkg.key}"`)
174174
});

src/commands/configuration-management/config-command.service.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ export class ConfigCommandService {
1616
this.diffService = new DiffService(context);
1717
}
1818

19-
public async listPackages(jsonResponse: boolean, flavors: string[], withDependencies: boolean, packageKeys: string[], keysByVersion: string[], variableValue: string, variableType: string): Promise<void> {
19+
public async listPackages(jsonResponse: boolean, flavors: string[], withDependencies: boolean, packageKeys: string[], keysByVersion: string[], variableValue: string, variableType: string, includeBranches: boolean): Promise<void> {
2020
if (variableValue) {
21-
await this.listPackagesByVariableValue(jsonResponse, flavors, variableValue, variableType);
21+
await this.listPackagesByVariableValue(jsonResponse, flavors, variableValue, variableType, includeBranches);
2222
return;
2323
}
2424

2525
if (jsonResponse) {
26-
await this.batchImportExportService.findAndExportListOfPackages(flavors ?? [], packageKeys ?? [], keysByVersion ?? [], withDependencies);
26+
await this.batchImportExportService.findAndExportListOfPackages(flavors ?? [], packageKeys ?? [], keysByVersion ?? [], withDependencies, includeBranches);
2727
} else if (keysByVersion) {
2828
await this.batchImportExportService.listPackagesByKeysWithVersion(keysByVersion, withDependencies);
2929
} else {
30-
await this.batchImportExportService.listActivePackages(flavors ?? []);
30+
await this.batchImportExportService.listActivePackages(flavors ?? [], includeBranches);
3131
}
3232
}
3333

@@ -82,11 +82,11 @@ export class ConfigCommandService {
8282
return this.diffService.diffPackages(file, hasChanges, jsonResponse);
8383
}
8484

85-
private async listPackagesByVariableValue(jsonResponse: boolean, flavors: string[], variableValue: string, variableType: string): Promise<void> {
85+
private async listPackagesByVariableValue(jsonResponse: boolean, flavors: string[], variableValue: string, variableType: string, includeBranches: boolean): Promise<void> {
8686
if (jsonResponse) {
87-
await this.batchImportExportService.findAndExportListOfActivePackagesByVariableValue(flavors ?? [], variableValue, variableType )
87+
await this.batchImportExportService.findAndExportListOfActivePackagesByVariableValue(flavors ?? [], variableValue, variableType, includeBranches)
8888
} else {
89-
await this.batchImportExportService.listActivePackagesByVariableValue(flavors ?? [], variableValue, variableType);
89+
await this.batchImportExportService.listActivePackagesByVariableValue(flavors ?? [], variableValue, variableType, includeBranches);
9090
}
9191
}
9292
}

src/commands/configuration-management/module.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Module extends IModule {
2626
.option("--keysByVersion <keysByVersion...>", "Lists packages by given key and version [packageKey.version]")
2727
.option("--variableValue <variableValue>", "Variable value for filtering packages by.")
2828
.option("--variableType <variableType>", "Variable type for filtering packages by.")
29+
.option("--branches", "Include branches", false)
2930
.action(this.listPackages);
3031

3132
configCommand.command("export")
@@ -185,7 +186,26 @@ class Module extends IModule {
185186
if (options.packageKeys && options.keysByVersion) {
186187
throw new Error("Please provide either --packageKeys or --keysByVersion, but not both.");
187188
}
188-
await new ConfigCommandService(context).listPackages(options.json, options.flavors, options.withDependencies, options.packageKeys, options.keysByVersion, options.variableValue, options.variableType);
189+
190+
if (options.branches) {
191+
if (options.packageKeys) {
192+
throw new Error("Please provide either --packageKeys or --branches, but not both.");
193+
}
194+
195+
if (options.keysByVersion) {
196+
throw new Error("Please provide either --keysByVersion or --branches, but not both.");
197+
}
198+
}
199+
200+
await new ConfigCommandService(context).listPackages(
201+
options.json,
202+
options.flavors,
203+
options.withDependencies,
204+
options.packageKeys,
205+
options.keysByVersion,
206+
options.variableValue,
207+
options.variableType,
208+
options.branches);
189209
}
190210

191211
private async batchExportPackages(context: Context, command: Command, options: OptionValues): Promise<void> {

tests/commands/configuration-management/config-list.spec.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ describe("Config list", () => {
2828

2929
const urlParams = new URLSearchParams();
3030
urlParams.set("withDependencies", "false");
31+
urlParams.set("includeBranches", "false");
3132
flavorsArray.forEach(flavor => urlParams.append("flavors", flavor));
3233

3334
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/core/packages/export/list?" + urlParams.toString(), [firstPackage, secondPackage]);
3435

35-
await new ConfigCommandService(testContext).listPackages(false, flavorsArray, false, [], undefined, null, null);
36+
await new ConfigCommandService(testContext).listPackages(false, flavorsArray, false, [], undefined, null, null, false);
3637

3738
expect(loggingTestTransport.logMessages.length).toBe(2);
3839
expect(loggingTestTransport.logMessages[0].message).toContain(`${firstPackage.name} - Key: "${firstPackage.key}"`);
@@ -46,10 +47,10 @@ describe("Config list", () => {
4647

4748
const studioPackage: ContentNodeTransport = PacmanApiUtils.buildContentNodeTransport("key-1", "spaceId-1");
4849

49-
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/core/packages/export/list?withDependencies=false", [{...firstPackage}, {...secondPackage}]);
50+
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/core/packages/export/list?withDependencies=false&includeBranches=false", [{...firstPackage}, {...secondPackage}]);
5051
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/packages/with-variable-assignments?type=DATA_MODEL", [studioPackage]);
5152

52-
await new ConfigCommandService(testContext).listPackages(true, [], false, [], undefined, null, null);
53+
await new ConfigCommandService(testContext).listPackages(true, [], false, [], undefined, null, null, false);
5354

5455
const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1];
5556

@@ -69,7 +70,7 @@ describe("Config list", () => {
6970
const firstPackage = PacmanApiUtils.buildPackageExportTransport("key-1", "name-1");
7071
const secondPackage = PacmanApiUtils.buildPackageExportTransport("key-2", "name-2");
7172

72-
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/core/packages/export/list?withDependencies=true", [{...firstPackage}, {...secondPackage}]);
73+
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/core/packages/export/list?withDependencies=true&includeBranches=false", [{...firstPackage}, {...secondPackage}]);
7374
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/packages/with-variable-assignments?type=DATA_MODEL", []);
7475

7576
const dataModelVariableAssignmentResponse: PackageWithVariableAssignments = {
@@ -95,7 +96,7 @@ describe("Config list", () => {
9596
};
9697
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/compute-pools/data-models/details", [dataModelDetailResponse]);
9798

98-
await new ConfigCommandService(testContext).listPackages(true, [], true, [], undefined, null, null);
99+
await new ConfigCommandService(testContext).listPackages(true, [], true, [], undefined, null, null, false);
99100

100101
const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1];
101102

@@ -120,7 +121,7 @@ describe("Config list", () => {
120121
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/core/packages/export/list-by-keys?packageKeys=key-1&packageKeys=key-2&withDependencies=false", [{...firstPackage}, {...secondPackage}]);
121122
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/packages/with-variable-assignments?type=DATA_MODEL", [studioPackage]);
122123

123-
await new ConfigCommandService(testContext).listPackages(true, [], false, [firstPackage.key, secondPackage.key], undefined, null, null);
124+
await new ConfigCommandService(testContext).listPackages(true, [], false, [firstPackage.key, secondPackage.key], undefined, null, null, false);
124125

125126
const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1];
126127

@@ -166,7 +167,7 @@ describe("Config list", () => {
166167
};
167168
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/compute-pools/data-models/details", [dataModelDetailResponse]);
168169

169-
await new ConfigCommandService(testContext).listPackages(true, [], true, [firstPackage.key, secondPackage.key], undefined, null, null);
170+
await new ConfigCommandService(testContext).listPackages(true, [], true, [firstPackage.key, secondPackage.key], undefined, null, null, false);
170171

171172
const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1];
172173

@@ -187,10 +188,10 @@ describe("Config list", () => {
187188
const secondPackage = PacmanApiUtils.buildPackageExportTransport("key-2", "name-2");
188189

189190

190-
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/core/packages/export/list-by-variable-value?variableValue=1", [{...firstPackage}, {...secondPackage}]);
191+
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/core/packages/export/list-by-variable-value?variableValue=1&includeBranches=false", [{...firstPackage}, {...secondPackage}]);
191192
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/packages/with-variable-assignments?type=DATA_MODEL", []);
192193

193-
await new ConfigCommandService(testContext).listPackages(false, [], false, [], undefined, "1", null);
194+
await new ConfigCommandService(testContext).listPackages(false, [], false, [], undefined, "1", null, false);
194195

195196
expect(loggingTestTransport.logMessages.length).toBe(2);
196197
expect(loggingTestTransport.logMessages[0].message).toContain(`${firstPackage.name} - Key: "${firstPackage.key}"`);
@@ -201,10 +202,10 @@ describe("Config list", () => {
201202
const firstPackage = PacmanApiUtils.buildPackageExportTransport("key-1", "name-1");
202203
const secondPackage = PacmanApiUtils.buildPackageExportTransport("key-2", "name-2");
203204

204-
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/core/packages/export/list-by-variable-value?variableValue=1", [{...firstPackage}, {...secondPackage}]);
205+
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/core/packages/export/list-by-variable-value?variableValue=1&includeBranches=false", [{...firstPackage}, {...secondPackage}]);
205206
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/packages/with-variable-assignments?type=DATA_MODEL", []);
206207

207-
await new ConfigCommandService(testContext).listPackages(true, [], false, [], undefined, "1", null);
208+
await new ConfigCommandService(testContext).listPackages(true, [], false, [], undefined, "1", null, false);
208209

209210
const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1];
210211

@@ -224,7 +225,7 @@ describe("Config list", () => {
224225
[firstPackage, secondPackage]
225226
);
226227

227-
await new ConfigCommandService(testContext).listPackages(false, [], false, undefined, keysByVersion, null, null);
228+
await new ConfigCommandService(testContext).listPackages(false, [], false, undefined, keysByVersion, null, null, false);
228229

229230
expect(loggingTestTransport.logMessages.length).toBe(2);
230231
expect(loggingTestTransport.logMessages[0].message).toContain(`${firstPackage.name} - Key: "${firstPackage.key}"`);
@@ -241,7 +242,7 @@ describe("Config list", () => {
241242
[firstPackage, secondPackage]
242243
);
243244

244-
await new ConfigCommandService(testContext).listPackages(false, [], true, undefined, keysByVersion, null, null);
245+
await new ConfigCommandService(testContext).listPackages(false, [], true, undefined, keysByVersion, null, null, false);
245246

246247
expect(loggingTestTransport.logMessages.length).toBe(2);
247248
})
@@ -259,7 +260,7 @@ describe("Config list", () => {
259260
);
260261
mockAxiosGet("https://myTeam.celonis.cloud/package-manager/api/packages/with-variable-assignments?type=DATA_MODEL", [studioPackage]);
261262

262-
await new ConfigCommandService(testContext).listPackages(true, [], false, [], keysByVersion, null, null);
263+
await new ConfigCommandService(testContext).listPackages(true, [], false, [], keysByVersion, null, null, false);
263264

264265
const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1];
265266

tests/commands/configuration-management/module.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,32 @@ describe("Configuration Management Module - Action Validations", () => {
5757
expect(mockConfigCommandService.listPackages).not.toHaveBeenCalled();
5858
});
5959

60+
it("should throw error when both packageKeys and branches are provided", async () => {
61+
const options: OptionValues = {
62+
packageKeys: ["package1", "package2"],
63+
branches: true
64+
};
65+
66+
await expect((module as any).listPackages(testContext, mockCommand, options)).rejects.toThrow(
67+
"Please provide either --packageKeys or --branches, but not both."
68+
);
69+
70+
expect(mockConfigCommandService.listPackages).not.toHaveBeenCalled();
71+
});
72+
73+
it("should throw error when both keysByVersion and branches are provided", async () => {
74+
const options: OptionValues = {
75+
keysByVersion: ["package3.1.0.0", "package4.1.0.0"],
76+
branches: true
77+
};
78+
79+
await expect((module as any).listPackages(testContext, mockCommand, options)).rejects.toThrow(
80+
"Please provide either --keysByVersion or --branches, but not both."
81+
);
82+
83+
expect(mockConfigCommandService.listPackages).not.toHaveBeenCalled();
84+
});
85+
6086
it("should pass validation when only packageKeys is provided", async () => {
6187
const options: OptionValues = {
6288
packageKeys: ["package1", "package2"],
@@ -72,6 +98,7 @@ describe("Configuration Management Module - Action Validations", () => {
7298
["package1", "package2"],
7399
undefined,
74100
undefined,
101+
undefined,
75102
undefined
76103
);
77104
});
@@ -91,9 +118,30 @@ describe("Configuration Management Module - Action Validations", () => {
91118
undefined,
92119
["package3.1.0.0", "package4.1.0.0"],
93120
undefined,
121+
undefined,
94122
undefined
95123
);
96124
});
125+
126+
it("should pass validation when only branches is provided", async () => {
127+
const options: OptionValues = {
128+
branches: true,
129+
json: true,
130+
};
131+
132+
await (module as any).listPackages(testContext, mockCommand, options);
133+
134+
expect(mockConfigCommandService.listPackages).toHaveBeenCalledWith(
135+
true,
136+
undefined,
137+
undefined,
138+
undefined,
139+
undefined,
140+
undefined,
141+
undefined,
142+
true
143+
);
144+
});
97145
});
98146
});
99147

0 commit comments

Comments
 (0)