Skip to content

Commit

Permalink
Validate badge url (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea authored Jan 5, 2025
1 parent f0a84f3 commit 68402ea
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
9 changes: 8 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,14 @@ class Utils {
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>`;
}
static getUsageBadge() {
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})`;
const platformUrl = Utils.getPlatformUrl();
const githubJobId = Utils.encodeForUrl(process.env.GITHUB_JOB || '');
const gitRepo = Utils.encodeForUrl(process.env.GITHUB_REPOSITORY || '');
const runId = process.env.GITHUB_RUN_ID || '';
return `![](${platformUrl}ui/api/v1/u?s=1&m=1&job_id=${githubJobId}&run_id=${runId}&git_repo=${gitRepo})`;
}
static encodeForUrl(value) {
return encodeURIComponent(value);
}
/**
* Checks if the header image is accessible via the internet.
Expand Down
13 changes: 11 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -910,8 +910,17 @@ export class Utils {
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>`;
}

private static getUsageBadge(): string {
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})`;
static getUsageBadge(): string {
const platformUrl: string = Utils.getPlatformUrl();
const githubJobId: string = Utils.encodeForUrl(process.env.GITHUB_JOB || '');
const gitRepo: string = Utils.encodeForUrl(process.env.GITHUB_REPOSITORY || '');
const runId: string = process.env.GITHUB_RUN_ID || '';

return `![](${platformUrl}ui/api/v1/u?s=1&m=1&job_id=${githubJobId}&run_id=${runId}&git_repo=${gitRepo})`;
}

private static encodeForUrl(value: string): string {
return encodeURIComponent(value);
}

/**
Expand Down
37 changes: 37 additions & 0 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,40 @@ describe('setUsageEnvVars', () => {
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED', false);
});
});

describe('Utils', () => {
describe('getUsageBadge', () => {
beforeEach(() => {
process.env.JF_URL = 'https://example.jfrog.io/';
process.env.GITHUB_JOB = 'test-job';
process.env.GITHUB_REPOSITORY = 'test/repo';
process.env.GITHUB_RUN_ID = '123';
});

afterEach(() => {
delete process.env.JF_URL;
delete process.env.GITHUB_JOB;
delete process.env.GITHUB_REPOSITORY;
delete process.env.GITHUB_RUN_ID;
});

it('should return the correct usage badge URL', () => {
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)';
expect(Utils.getUsageBadge()).toBe(expectedBadge);
});

it('should URL encode the job ID and repository', () => {
process.env.GITHUB_JOB = 'test job';
process.env.GITHUB_REPOSITORY = 'test repo';
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)';
expect(Utils.getUsageBadge()).toBe(expectedBadge);
});

it('should handle missing environment variables gracefully', () => {
delete process.env.GITHUB_JOB;
delete process.env.GITHUB_REPOSITORY;
const expectedBadge: string = '![](https://example.jfrog.io/ui/api/v1/u?s=1&m=1&job_id=&run_id=123&git_repo=)';
expect(Utils.getUsageBadge()).toBe(expectedBadge);
});
});
});

0 comments on commit 68402ea

Please sign in to comment.