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

Fix usage system parameters #243

Merged
merged 22 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
12 changes: 10 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class Utils {
const exchangeUrl = jfrogCredentials.jfrogUrl.replace(/\/$/, '') + '/access/api/v1/oidc/token';
core.debug('Exchanging GitHub JSON web token with a JFrog access token...');
let projectKey = process.env.JF_PROJECT || '';
let jobId = process.env.GITHUB_JOB || '';
let jobId = Utils.GitHubJobIdEncoded();
let runId = process.env.GITHUB_RUN_ID || '';
const httpClient = new http_client_1.HttpClient();
const data = `{
Expand Down Expand Up @@ -825,7 +825,7 @@ class Utils {
}
static getUsageBadge() {
const platformUrl = Utils.getPlatformUrl();
const githubJobId = Utils.encodeForUrl(process.env.GITHUB_JOB || '');
const githubJobId = this.GitHubJobIdEncoded();
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})`;
Expand Down Expand Up @@ -872,6 +872,14 @@ class Utils {
}
return tempDir;
}
/**
* Retrieves the GitHub workflow name from the environment variables and URL encodes it.
* Note: To avoid confusion, this returns the workflow name, which we consider as job_id,
* whereas GitHub uses job_id to refer to the specific job within a workflow.
*/
static GitHubJobIdEncoded() {
return this.encodeForUrl(process.env.GITHUB_WORKFLOW || '');
}
}
exports.Utils = Utils;
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down
14 changes: 12 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as github from '@actions/github';
import { gzip } from 'zlib';
import { promisify } from 'util';
import { load } from 'js-yaml';
import * as url from 'node:url';
EyalDelarea marked this conversation as resolved.
Show resolved Hide resolved

export class Utils {
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -234,7 +235,7 @@ export class Utils {
core.debug('Exchanging GitHub JSON web token with a JFrog access token...');

let projectKey: string = process.env.JF_PROJECT || '';
let jobId: string = process.env.GITHUB_JOB || '';
let jobId: string = Utils.GitHubJobIdEncoded();
EyalDelarea marked this conversation as resolved.
Show resolved Hide resolved
let runId: string = process.env.GITHUB_RUN_ID || '';

const httpClient: HttpClient = new HttpClient();
Expand Down Expand Up @@ -912,7 +913,7 @@ export class Utils {

static getUsageBadge(): string {
const platformUrl: string = Utils.getPlatformUrl();
const githubJobId: string = Utils.encodeForUrl(process.env.GITHUB_JOB || '');
const githubJobId: string = this.GitHubJobIdEncoded()
const gitRepo: string = Utils.encodeForUrl(process.env.GITHUB_REPOSITORY || '');
const runId: string = process.env.GITHUB_RUN_ID || '';

Expand Down Expand Up @@ -959,6 +960,15 @@ export class Utils {
}
return tempDir;
}

/**
* Retrieves the GitHub workflow name from the environment variables and URL encodes it.
* Note: To avoid confusion, this returns the workflow name, which we consider as job_id,
* whereas GitHub uses job_id to refer to the specific job within a workflow.
*/
static GitHubJobIdEncoded(): string {
return this.encodeForUrl(process.env.GITHUB_WORKFLOW || '');
}
}

export interface DownloadDetails {
Expand Down
31 changes: 28 additions & 3 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ describe('Utils', () => {
describe('getUsageBadge', () => {
beforeEach(() => {
process.env.JF_URL = 'https://example.jfrog.io/';
process.env.GITHUB_JOB = 'test-job';
process.env.GITHUB_WORKFLOW = 'test-job';
process.env.GITHUB_REPOSITORY = 'test/repo';
process.env.GITHUB_RUN_ID = '123';
});
Expand All @@ -590,17 +590,42 @@ describe('Utils', () => {
});

it('should URL encode the job ID and repository', () => {
process.env.GITHUB_JOB = 'test job';
process.env.GITHUB_WORKFLOW = '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_WORKFLOW;
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);
});
});
});
describe('Utils', () => {
describe('getGitHubJobId', () => {
afterEach(() => {
delete process.env.GITHUB_WORKFLOW;
});

it('should return URL encoded job ID with spaces', () => {
process.env.GITHUB_WORKFLOW = 'test workflow';
const expectedJobId:string = 'test%20workflow';
expect(Utils.GitHubJobIdEncoded()).toBe(expectedJobId);
});

it('should return URL encoded job ID with multiple spaces', () => {
process.env.GITHUB_WORKFLOW = 'test workflow with spaces';
const expectedJobId:string = 'test%20workflow%20with%20spaces';
expect(Utils.GitHubJobIdEncoded()).toBe(expectedJobId);
});

it('should return an empty string if GITHUB_WORKFLOW is not set', () => {
const expectedJobId:string = '';
expect(Utils.GitHubJobIdEncoded()).toBe(expectedJobId);
});
});
});

Loading