Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate badge url #241

Merged
merged 2 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});
Loading