Skip to content

Commit 68402ea

Browse files
authored
Validate badge url (#241)
1 parent f0a84f3 commit 68402ea

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

lib/utils.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,14 @@ class Utils {
824824
return `${this.getUsageBadge()} \n\n # \n\n The above Job Summary was generated by the <a href="https://github.com/marketplace/actions/setup-jfrog-cli"> Setup JFrog CLI GitHub Action </a>`;
825825
}
826826
static getUsageBadge() {
827-
return `![](${process.env.JF_URL}/ui/api/v1/u?s=1&m=1&job_id=${process.env.GITHUB_JOB}&run_id=${process.env.GITHUB_RUN_ID}&git_repo=${process.env.GITHUB_REPOSITORY})`;
827+
const platformUrl = Utils.getPlatformUrl();
828+
const githubJobId = Utils.encodeForUrl(process.env.GITHUB_JOB || '');
829+
const gitRepo = Utils.encodeForUrl(process.env.GITHUB_REPOSITORY || '');
830+
const runId = process.env.GITHUB_RUN_ID || '';
831+
return `![](${platformUrl}ui/api/v1/u?s=1&m=1&job_id=${githubJobId}&run_id=${runId}&git_repo=${gitRepo})`;
832+
}
833+
static encodeForUrl(value) {
834+
return encodeURIComponent(value);
828835
}
829836
/**
830837
* Checks if the header image is accessible via the internet.

src/utils.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,8 +910,17 @@ export class Utils {
910910
return `${this.getUsageBadge()} \n\n # \n\n The above Job Summary was generated by the <a href="https://github.com/marketplace/actions/setup-jfrog-cli"> Setup JFrog CLI GitHub Action </a>`;
911911
}
912912

913-
private static getUsageBadge(): string {
914-
return `![](${process.env.JF_URL}/ui/api/v1/u?s=1&m=1&job_id=${process.env.GITHUB_JOB}&run_id=${process.env.GITHUB_RUN_ID}&git_repo=${process.env.GITHUB_REPOSITORY})`;
913+
static getUsageBadge(): string {
914+
const platformUrl: string = Utils.getPlatformUrl();
915+
const githubJobId: string = Utils.encodeForUrl(process.env.GITHUB_JOB || '');
916+
const gitRepo: string = Utils.encodeForUrl(process.env.GITHUB_REPOSITORY || '');
917+
const runId: string = process.env.GITHUB_RUN_ID || '';
918+
919+
return `![](${platformUrl}ui/api/v1/u?s=1&m=1&job_id=${githubJobId}&run_id=${runId}&git_repo=${gitRepo})`;
920+
}
921+
922+
private static encodeForUrl(value: string): string {
923+
return encodeURIComponent(value);
915924
}
916925

917926
/**

test/main.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,40 @@ describe('setUsageEnvVars', () => {
567567
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED', false);
568568
});
569569
});
570+
571+
describe('Utils', () => {
572+
describe('getUsageBadge', () => {
573+
beforeEach(() => {
574+
process.env.JF_URL = 'https://example.jfrog.io/';
575+
process.env.GITHUB_JOB = 'test-job';
576+
process.env.GITHUB_REPOSITORY = 'test/repo';
577+
process.env.GITHUB_RUN_ID = '123';
578+
});
579+
580+
afterEach(() => {
581+
delete process.env.JF_URL;
582+
delete process.env.GITHUB_JOB;
583+
delete process.env.GITHUB_REPOSITORY;
584+
delete process.env.GITHUB_RUN_ID;
585+
});
586+
587+
it('should return the correct usage badge URL', () => {
588+
const expectedBadge: string = '![](https://example.jfrog.io/ui/api/v1/u?s=1&m=1&job_id=test-job&run_id=123&git_repo=test%2Frepo)';
589+
expect(Utils.getUsageBadge()).toBe(expectedBadge);
590+
});
591+
592+
it('should URL encode the job ID and repository', () => {
593+
process.env.GITHUB_JOB = 'test job';
594+
process.env.GITHUB_REPOSITORY = 'test repo';
595+
const expectedBadge: string = '![](https://example.jfrog.io/ui/api/v1/u?s=1&m=1&job_id=test%20job&run_id=123&git_repo=test%20repo)';
596+
expect(Utils.getUsageBadge()).toBe(expectedBadge);
597+
});
598+
599+
it('should handle missing environment variables gracefully', () => {
600+
delete process.env.GITHUB_JOB;
601+
delete process.env.GITHUB_REPOSITORY;
602+
const expectedBadge: string = '![](https://example.jfrog.io/ui/api/v1/u?s=1&m=1&job_id=&run_id=123&git_repo=)';
603+
expect(Utils.getUsageBadge()).toBe(expectedBadge);
604+
});
605+
});
606+
});

0 commit comments

Comments
 (0)