Skip to content

Commit dff217c

Browse files
authored
Fix JFrog servers configurations cleanup (#236)
1 parent 9615397 commit dff217c

File tree

3 files changed

+79
-15
lines changed

3 files changed

+79
-15
lines changed

lib/utils.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,14 +340,18 @@ class Utils {
340340
return serverId;
341341
}
342342
/**
343-
* Return custom server ID if provided, or default server ID otherwise.
343+
* Returns the custom server ID if provided, otherwise returns the default server ID.
344344
*/
345345
static getCustomOrDefaultServerId() {
346+
const customServerId = this.getInputtedCustomId();
347+
return customServerId || this.getRunDefaultServerId();
348+
}
349+
static getInputtedCustomId() {
346350
let customServerId = core.getInput(Utils.CUSTOM_SERVER_ID);
347351
if (customServerId) {
348352
return customServerId;
349353
}
350-
return Utils.getRunDefaultServerId();
354+
return undefined;
351355
}
352356
/**
353357
* Return the default server ID for JFrog CLI server configuration.
@@ -407,15 +411,26 @@ class Utils {
407411
});
408412
}
409413
/**
410-
* Removed configured JFrog CLI servers that are saved in the servers env var, and unset the env var.
414+
* Removes configured JFrog CLI servers saved in the environment variable.
415+
* If a custom server ID is defined, only remove the custom server ID.
411416
*/
412417
static removeJFrogServers() {
413418
return __awaiter(this, void 0, void 0, function* () {
414-
for (const serverId of Utils.getConfiguredJFrogServers()) {
415-
core.debug(`Removing server ID: '${serverId}'...`);
416-
yield Utils.runCli(['c', 'rm', serverId, '--quiet']);
419+
const customServerId = this.getInputtedCustomId();
420+
core.info(`The value of custom is: '${customServerId}'`);
421+
if (customServerId) {
422+
// Remove only the custom server ID
423+
core.debug(`Removing custom server ID: '${customServerId}'...`);
424+
yield Utils.runCli(['c', 'rm', customServerId, '--quiet']);
425+
}
426+
else {
427+
// Remove all configured server IDs
428+
for (const serverId of Utils.getConfiguredJFrogServers()) {
429+
core.debug(`Removing server ID: '${serverId}'...`);
430+
yield Utils.runCli(['c', 'rm', serverId, '--quiet']);
431+
}
432+
core.exportVariable(Utils.JFROG_CLI_SERVER_IDS_ENV_VAR, '');
417433
}
418-
core.exportVariable(Utils.JFROG_CLI_SERVER_IDS_ENV_VAR, '');
419434
});
420435
}
421436
/**

src/utils.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,19 @@ export class Utils {
387387
}
388388

389389
/**
390-
* Return custom server ID if provided, or default server ID otherwise.
390+
* Returns the custom server ID if provided, otherwise returns the default server ID.
391391
*/
392392
private static getCustomOrDefaultServerId(): string {
393+
const customServerId: string | undefined = this.getInputtedCustomId();
394+
return customServerId || this.getRunDefaultServerId();
395+
}
396+
397+
private static getInputtedCustomId(): string | undefined {
393398
let customServerId: string = core.getInput(Utils.CUSTOM_SERVER_ID);
394399
if (customServerId) {
395400
return customServerId;
396401
}
397-
return Utils.getRunDefaultServerId();
402+
return undefined;
398403
}
399404

400405
/**
@@ -467,14 +472,25 @@ export class Utils {
467472
}
468473

469474
/**
470-
* Removed configured JFrog CLI servers that are saved in the servers env var, and unset the env var.
475+
* Removes configured JFrog CLI servers saved in the environment variable.
476+
* If a custom server ID is defined, only remove the custom server ID.
471477
*/
472478
public static async removeJFrogServers() {
473-
for (const serverId of Utils.getConfiguredJFrogServers()) {
474-
core.debug(`Removing server ID: '${serverId}'...`);
475-
await Utils.runCli(['c', 'rm', serverId, '--quiet']);
479+
const customServerId: string | undefined = this.getInputtedCustomId();
480+
core.info(`The value of custom is: '${customServerId}'`);
481+
482+
if (customServerId) {
483+
// Remove only the custom server ID
484+
core.debug(`Removing custom server ID: '${customServerId}'...`);
485+
await Utils.runCli(['c', 'rm', customServerId, '--quiet']);
486+
} else {
487+
// Remove all configured server IDs
488+
for (const serverId of Utils.getConfiguredJFrogServers()) {
489+
core.debug(`Removing server ID: '${serverId}'...`);
490+
await Utils.runCli(['c', 'rm', serverId, '--quiet']);
491+
}
492+
core.exportVariable(Utils.JFROG_CLI_SERVER_IDS_ENV_VAR, '');
476493
}
477-
core.exportVariable(Utils.JFROG_CLI_SERVER_IDS_ENV_VAR, '');
478494
}
479495

480496
/**

test/main.spec.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import semver = require('semver/preload');
66
jest.mock('os');
77
jest.mock('@actions/core');
88
jest.mock('semver');
9-
109
const DEFAULT_CLI_URL: string = 'https://releases.jfrog.io/artifactory/jfrog-cli/';
1110
const CUSTOM_CLI_URL: string = 'http://127.0.0.1:8081/artifactory/jfrog-cli-remote/';
1211
// Config in JFrog CLI 1.46.3 and below
@@ -425,3 +424,37 @@ describe('isJobSummarySupported', () => {
425424
expect(semver.gte).toHaveBeenCalledWith(version, MIN_CLI_VERSION_JOB_SUMMARY);
426425
});
427426
});
427+
428+
describe('Utils.removeJFrogServers', () => {
429+
beforeEach(() => {
430+
jest.clearAllMocks();
431+
});
432+
433+
it('should remove only the custom server ID if defined', async () => {
434+
const customServerId: string = 'custom-server-id';
435+
jest.spyOn(Utils as any, 'getInputtedCustomId').mockReturnValue(customServerId);
436+
jest.spyOn(Utils as any, 'runCli').mockResolvedValue(undefined);
437+
438+
await Utils.removeJFrogServers();
439+
440+
expect(core.info).toHaveBeenCalledWith(`The value of custom is: '${customServerId}'`);
441+
expect(core.debug).toHaveBeenCalledWith(`Removing custom server ID: '${customServerId}'...`);
442+
expect(Utils.runCli).toHaveBeenCalledWith(['c', 'rm', customServerId, '--quiet']);
443+
});
444+
445+
it('should remove all configured server IDs if no custom server ID is defined', async () => {
446+
jest.spyOn(Utils as any, 'getInputtedCustomId').mockReturnValue(undefined);
447+
const serverIds: string[] = ['server1', 'server2'];
448+
jest.spyOn(Utils as any, 'getConfiguredJFrogServers').mockReturnValue(serverIds);
449+
jest.spyOn(Utils as any, 'runCli').mockResolvedValue(undefined);
450+
451+
await Utils.removeJFrogServers();
452+
453+
expect(core.info).toHaveBeenCalledWith(`The value of custom is: 'undefined'`);
454+
for (const serverId of serverIds) {
455+
expect(core.debug).toHaveBeenCalledWith(`Removing server ID: '${serverId}'...`);
456+
expect(Utils.runCli).toHaveBeenCalledWith(['c', 'rm', serverId, '--quiet']);
457+
}
458+
expect(core.exportVariable).toHaveBeenCalledWith(Utils.JFROG_CLI_SERVER_IDS_ENV_VAR, '');
459+
});
460+
});

0 commit comments

Comments
 (0)