Skip to content

Commit 1a51b15

Browse files
authored
Fix usage system parameters (#243)
1 parent 68402ea commit 1a51b15

File tree

3 files changed

+81
-23
lines changed

3 files changed

+81
-23
lines changed

lib/utils.js

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ class Utils {
191191
const exchangeUrl = jfrogCredentials.jfrogUrl.replace(/\/$/, '') + '/access/api/v1/oidc/token';
192192
core.debug('Exchanging GitHub JSON web token with a JFrog access token...');
193193
let projectKey = process.env.JF_PROJECT || '';
194-
let jobId = process.env.GITHUB_JOB || '';
194+
let jobId = this.getGithubJobId();
195195
let runId = process.env.GITHUB_RUN_ID || '';
196+
let githubRepository = process.env.GITHUB_REPOSITORY || '';
196197
const httpClient = new http_client_1.HttpClient();
197198
const data = `{
198199
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
@@ -202,6 +203,7 @@ class Utils {
202203
"project_key": "${projectKey}",
203204
"gh_job_id": "${jobId}",
204205
"gh_run_id": "${runId}",
206+
"gh_repo": "${githubRepository}",
205207
"application_key": "${applicationKey}"
206208
}`;
207209
const additionalHeaders = {
@@ -825,13 +827,16 @@ class Utils {
825827
}
826828
static getUsageBadge() {
827829
const platformUrl = Utils.getPlatformUrl();
828-
const githubJobId = Utils.encodeForUrl(process.env.GITHUB_JOB || '');
829-
const gitRepo = Utils.encodeForUrl(process.env.GITHUB_REPOSITORY || '');
830+
const githubJobId = this.getGithubJobId();
831+
const gitRepo = process.env.GITHUB_REPOSITORY || '';
830832
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);
833+
const url = new URL(`${platformUrl}ui/api/v1/u`);
834+
url.searchParams.set(Utils.SOURCE_PARAM_KEY, Utils.SOURCE_PARAM_VALUE);
835+
url.searchParams.set(Utils.METRIC_PARAM_KEY, Utils.METRIC_PARAM_VALUE);
836+
url.searchParams.set(Utils.JOB_ID_PARAM_KEY, githubJobId);
837+
url.searchParams.set(Utils.RUN_ID_PARAM_KEY, runId);
838+
url.searchParams.set(Utils.GIT_REPO_PARAM_KEY, gitRepo);
839+
return `![](${url.toString()})`;
835840
}
836841
/**
837842
* Checks if the header image is accessible via the internet.
@@ -872,6 +877,14 @@ class Utils {
872877
}
873878
return tempDir;
874879
}
880+
/**
881+
* Retrieves the GitHub job ID, which in this context refers to the GitHub workflow name.
882+
* Note: We use "job" instead of "workflow" to align with our terminology, where "GitHub job summary"
883+
* refers to the entire workflow summary. Here, "job ID" means the workflow name, not individual jobs within the workflow.
884+
*/
885+
static getGithubJobId() {
886+
return process.env.GITHUB_WORKFLOW || '';
887+
}
875888
}
876889
exports.Utils = Utils;
877890
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -939,3 +952,15 @@ Utils.CUSTOM_SERVER_ID = 'custom-server-id';
939952
Utils.MARKDOWN_HEADER_PNG_URL = 'https://media.jfrog.com/wp-content/uploads/2024/09/02161430/jfrog-job-summary.svg';
940953
// Flag to indicate if the summary header is accessible, can be undefined if not checked yet.
941954
Utils.isSummaryHeaderAccessible = undefined;
955+
// Job ID query parameter key
956+
Utils.JOB_ID_PARAM_KEY = 'job_id';
957+
// Run ID query parameter key
958+
Utils.RUN_ID_PARAM_KEY = 'run_id';
959+
// Git repository query parameter key
960+
Utils.GIT_REPO_PARAM_KEY = 'git_repo';
961+
// Source query parameter indicating the source of the request
962+
Utils.SOURCE_PARAM_KEY = 's';
963+
Utils.SOURCE_PARAM_VALUE = '1';
964+
// Metric query parameter indicating the metric type
965+
Utils.METRIC_PARAM_KEY = 'm';
966+
Utils.METRIC_PARAM_VALUE = '1';

src/utils.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ export class Utils {
8383
private static MARKDOWN_HEADER_PNG_URL: string = 'https://media.jfrog.com/wp-content/uploads/2024/09/02161430/jfrog-job-summary.svg';
8484
// Flag to indicate if the summary header is accessible, can be undefined if not checked yet.
8585
private static isSummaryHeaderAccessible: boolean | undefined = undefined;
86+
// Job ID query parameter key
87+
private static readonly JOB_ID_PARAM_KEY: string = 'job_id';
88+
// Run ID query parameter key
89+
private static readonly RUN_ID_PARAM_KEY: string = 'run_id';
90+
// Git repository query parameter key
91+
private static readonly GIT_REPO_PARAM_KEY: string = 'git_repo';
92+
// Source query parameter indicating the source of the request
93+
private static readonly SOURCE_PARAM_KEY: string = 's';
94+
private static readonly SOURCE_PARAM_VALUE: string = '1';
95+
// Metric query parameter indicating the metric type
96+
private static readonly METRIC_PARAM_KEY: string = 'm';
97+
private static readonly METRIC_PARAM_VALUE: string = '1';
8698

8799
/**
88100
* Retrieves server credentials for accessing JFrog's server
@@ -234,8 +246,9 @@ export class Utils {
234246
core.debug('Exchanging GitHub JSON web token with a JFrog access token...');
235247

236248
let projectKey: string = process.env.JF_PROJECT || '';
237-
let jobId: string = process.env.GITHUB_JOB || '';
249+
let jobId: string = this.getGithubJobId();
238250
let runId: string = process.env.GITHUB_RUN_ID || '';
251+
let githubRepository: string = process.env.GITHUB_REPOSITORY || '';
239252

240253
const httpClient: HttpClient = new HttpClient();
241254
const data: string = `{
@@ -246,6 +259,7 @@ export class Utils {
246259
"project_key": "${projectKey}",
247260
"gh_job_id": "${jobId}",
248261
"gh_run_id": "${runId}",
262+
"gh_repo": "${githubRepository}",
249263
"application_key": "${applicationKey}"
250264
}`;
251265

@@ -912,15 +926,17 @@ export class Utils {
912926

913927
static getUsageBadge(): string {
914928
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 || '');
929+
const githubJobId: string = this.getGithubJobId();
930+
const gitRepo: string = process.env.GITHUB_REPOSITORY || '';
917931
const runId: string = process.env.GITHUB_RUN_ID || '';
932+
const url: URL = new URL(`${platformUrl}ui/api/v1/u`);
918933

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);
934+
url.searchParams.set(Utils.SOURCE_PARAM_KEY, Utils.SOURCE_PARAM_VALUE);
935+
url.searchParams.set(Utils.METRIC_PARAM_KEY, Utils.METRIC_PARAM_VALUE);
936+
url.searchParams.set(Utils.JOB_ID_PARAM_KEY, githubJobId);
937+
url.searchParams.set(Utils.RUN_ID_PARAM_KEY, runId);
938+
url.searchParams.set(Utils.GIT_REPO_PARAM_KEY, gitRepo);
939+
return `![](${url.toString()})`;
924940
}
925941

926942
/**
@@ -959,6 +975,15 @@ export class Utils {
959975
}
960976
return tempDir;
961977
}
978+
979+
/**
980+
* Retrieves the GitHub job ID, which in this context refers to the GitHub workflow name.
981+
* Note: We use "job" instead of "workflow" to align with our terminology, where "GitHub job summary"
982+
* refers to the entire workflow summary. Here, "job ID" means the workflow name, not individual jobs within the workflow.
983+
*/
984+
static getGithubJobId(): string {
985+
return process.env.GITHUB_WORKFLOW || '';
986+
}
962987
}
963988

964989
export interface DownloadDetails {

test/main.spec.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -568,36 +568,44 @@ describe('setUsageEnvVars', () => {
568568
});
569569
});
570570

571-
describe('Utils', () => {
571+
describe('Test correct encoding of badge URL', () => {
572572
describe('getUsageBadge', () => {
573573
beforeEach(() => {
574574
process.env.JF_URL = 'https://example.jfrog.io/';
575-
process.env.GITHUB_JOB = 'test-job';
576-
process.env.GITHUB_REPOSITORY = 'test/repo';
577575
process.env.GITHUB_RUN_ID = '123';
578576
});
579577

580578
afterEach(() => {
581579
delete process.env.JF_URL;
582-
delete process.env.GITHUB_JOB;
580+
delete process.env.GITHUB_WORKFLOW;
583581
delete process.env.GITHUB_REPOSITORY;
584582
delete process.env.GITHUB_RUN_ID;
585583
});
586584

587585
it('should return the correct usage badge URL', () => {
586+
process.env.GITHUB_WORKFLOW = 'test-job';
587+
process.env.GITHUB_REPOSITORY = 'test/repo';
588588
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)';
589589
expect(Utils.getUsageBadge()).toBe(expectedBadge);
590590
});
591591

592-
it('should URL encode the job ID and repository', () => {
593-
process.env.GITHUB_JOB = 'test job';
592+
it('should URL encode the job ID and repository with spaces', () => {
593+
process.env.GITHUB_WORKFLOW = 'test job';
594594
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)';
595+
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+repo)';
596+
expect(Utils.getUsageBadge()).toBe(expectedBadge);
597+
});
598+
599+
it('should URL encode the job ID and repository with special characters', () => {
600+
process.env.GITHUB_WORKFLOW = 'test/job@workflow';
601+
process.env.GITHUB_REPOSITORY = 'test/repo@special';
602+
const expectedBadge: string =
603+
'![](https://example.jfrog.io/ui/api/v1/u?s=1&m=1&job_id=test%2Fjob%40workflow&run_id=123&git_repo=test%2Frepo%40special)';
596604
expect(Utils.getUsageBadge()).toBe(expectedBadge);
597605
});
598606

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

0 commit comments

Comments
 (0)