Skip to content

Commit 4871dbe

Browse files
authored
Upgrade JFrog CLI and set usage vars to be captured by JFrog CLI (#237)
1 parent 1a69118 commit 4871dbe

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: "JFrog"
44
inputs:
55
version:
66
description: "JFrog CLI Version"
7-
default: "2.72.2"
7+
default: "2.72.3"
88
required: false
99
download-repository:
1010
description: "Remote repository in Artifactory pointing to 'https://releases.jfrog.io/artifactory/jfrog-cli'. Use this parameter in case you don't have an Internet access."

lib/utils.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class Utils {
5959
let jfrogCredentials = this.collectJfrogCredentialsFromEnvVars();
6060
const oidcProviderName = core.getInput(Utils.OIDC_INTEGRATION_PROVIDER_NAME);
6161
if (!oidcProviderName) {
62+
// Set environment variable to track OIDC usage.
63+
core.exportVariable('JFROG_CLI_USAGE_CONFIG_OIDC', '');
64+
core.exportVariable('JFROG_CLI_USAGE_OIDC_USED', 'FALSE');
6265
// Use JF_ENV or the credentials found in the environment variables
6366
return jfrogCredentials;
6467
}
@@ -78,8 +81,9 @@ class Utils {
7881
const applicationKey = yield this.getApplicationKey();
7982
try {
8083
jfrogCredentials = yield this.getJfrogAccessTokenThroughOidcProtocol(jfrogCredentials, jsonWebToken, oidcProviderName, applicationKey);
81-
// Set environment variable to track OIDC logins in the usage report.
84+
// Set environment variable to track OIDC usage.
8285
core.exportVariable('JFROG_CLI_USAGE_CONFIG_OIDC', 'TRUE');
86+
core.exportVariable('JFROG_CLI_USAGE_OIDC_USED', 'TRUE');
8387
return jfrogCredentials;
8488
}
8589
catch (error) {
@@ -98,7 +102,7 @@ class Utils {
98102
*/
99103
static getApplicationKey() {
100104
return __awaiter(this, void 0, void 0, function* () {
101-
const configFilePath = path.join(this.JF_CONFIG_DIR_NAME, this.JF_CONFIG_FILE);
105+
const configFilePath = path.join(this.JF_CONFIG_DIR_NAME, this.JF_CONFIG_FILE_NAME);
102106
try {
103107
const config = yield this.readConfigFromFileSystem(configFilePath);
104108
if (!config) {
@@ -445,6 +449,23 @@ class Utils {
445449
if (!core.getBooleanInput(Utils.JOB_SUMMARY_DISABLE)) {
446450
Utils.enableJobSummaries();
447451
}
452+
Utils.setUsageEnvVars();
453+
}
454+
// Set usage variables to be captured by JFrog CLI.
455+
static setUsageEnvVars() {
456+
// Repository name, defaulting to an empty string if undefined.
457+
const repoName = process.env.GITHUB_REPOSITORY || '';
458+
// Workflow name, defaulting to an empty string if undefined.
459+
const jobId = process.env.GITHUB_WORKFLOW || '';
460+
// Run ID, defaulting to an empty string if undefined.
461+
const runId = process.env.GITHUB_RUN_ID || '';
462+
// Boolean flag indicating if JF_GIT_TOKEN is set.
463+
const jfGitTokenSet = !!process.env.JF_GIT_TOKEN;
464+
// Export environment variables for JFrog CLI usage.
465+
core.exportVariable('JFROG_CLI_USAGE_JOB_ID', jobId);
466+
core.exportVariable('JFROG_CLI_USAGE_RUN_ID', runId);
467+
core.exportVariable('JFROG_CLI_USAGE_GIT_REPO', repoName);
468+
core.exportVariable('JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED', jfGitTokenSet);
448469
}
449470
/**
450471
* Enabling job summary is done by setting the output dir for the summaries.
@@ -885,7 +906,7 @@ Utils.KEY = 'key';
885906
// Config file directory name
886907
Utils.JF_CONFIG_DIR_NAME = '.jfrog';
887908
// Config file name
888-
Utils.JF_CONFIG_FILE = 'config.yml';
909+
Utils.JF_CONFIG_FILE_NAME = 'config.yml';
889910
// Disable Job Summaries feature flag
890911
Utils.JOB_SUMMARY_DISABLE = 'disable-job-summary';
891912
// Disable auto build info publish feature flag

src/utils.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ export class Utils {
9393
let jfrogCredentials: JfrogCredentials = this.collectJfrogCredentialsFromEnvVars();
9494
const oidcProviderName: string = core.getInput(Utils.OIDC_INTEGRATION_PROVIDER_NAME);
9595
if (!oidcProviderName) {
96+
// Set environment variable to track OIDC usage.
97+
core.exportVariable('JFROG_CLI_USAGE_CONFIG_OIDC', '');
98+
core.exportVariable('JFROG_CLI_USAGE_OIDC_USED', 'FALSE');
99+
96100
// Use JF_ENV or the credentials found in the environment variables
97101
return jfrogCredentials;
98102
}
@@ -113,8 +117,10 @@ export class Utils {
113117
const applicationKey: string = await this.getApplicationKey();
114118
try {
115119
jfrogCredentials = await this.getJfrogAccessTokenThroughOidcProtocol(jfrogCredentials, jsonWebToken, oidcProviderName, applicationKey);
116-
// Set environment variable to track OIDC logins in the usage report.
120+
121+
// Set environment variable to track OIDC usage.
117122
core.exportVariable('JFROG_CLI_USAGE_CONFIG_OIDC', 'TRUE');
123+
core.exportVariable('JFROG_CLI_USAGE_OIDC_USED', 'TRUE');
118124
return jfrogCredentials;
119125
} catch (error: any) {
120126
throw new Error(`Exchanging JSON web token with an access token failed: ${error.message}`);
@@ -513,6 +519,20 @@ export class Utils {
513519
if (!core.getBooleanInput(Utils.JOB_SUMMARY_DISABLE)) {
514520
Utils.enableJobSummaries();
515521
}
522+
523+
Utils.setUsageEnvVars()
524+
}
525+
526+
// Set usage variables to be captured by JFrog CLI visibility metric service.
527+
public static setUsageEnvVars(): void {
528+
// Set the GitHub repository name or default to an empty string.
529+
core.exportVariable('JFROG_CLI_USAGE_GIT_REPO', process.env.GITHUB_REPOSITORY ?? '');
530+
// Set the GitHub workflow name or default to an empty string.
531+
core.exportVariable('JFROG_CLI_USAGE_JOB_ID', process.env.GITHUB_WORKFLOW ?? '');
532+
// Set the GitHub run ID or default to an empty string.
533+
core.exportVariable('JFROG_CLI_USAGE_RUN_ID', process.env.GITHUB_RUN_ID ?? '');
534+
// Indicate if JF_GIT_TOKEN is provided as an environment variable.
535+
core.exportVariable('JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED', !!process.env.JF_GIT_TOKEN);
516536
}
517537

518538
/**

test/main.spec.ts

+41
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,44 @@ describe('getApplicationKey', () => {
528528
expect(result).toBe('');
529529
});
530530
});
531+
532+
describe('setUsageEnvVars', () => {
533+
beforeEach(() => {
534+
// Clear environment variables before each test
535+
delete process.env.GITHUB_REPOSITORY;
536+
delete process.env.GITHUB_WORKFLOW;
537+
delete process.env.GITHUB_RUN_ID;
538+
delete process.env.JF_GIT_TOKEN;
539+
540+
jest.clearAllMocks();
541+
});
542+
543+
it('should export the correct environment variables when all inputs are set', () => {
544+
// Mock environment variables
545+
process.env.GITHUB_REPOSITORY = 'owner/repo';
546+
process.env.GITHUB_WORKFLOW = 'test-workflow';
547+
process.env.GITHUB_RUN_ID = '12345';
548+
process.env.JF_GIT_TOKEN = 'dummy-token';
549+
550+
// Call the function
551+
Utils.setUsageEnvVars();
552+
553+
// Verify exported variables
554+
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_JOB_ID', 'test-workflow');
555+
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_RUN_ID', '12345');
556+
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_GIT_REPO', 'owner/repo');
557+
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED', true);
558+
});
559+
560+
it('should export empty strings for missing environment variables', () => {
561+
// Call the function with no environment variables set
562+
Utils.setUsageEnvVars();
563+
564+
// Verify exported variables
565+
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_JOB_ID', '');
566+
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_RUN_ID', '');
567+
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_GIT_REPO', '');
568+
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED', false);
569+
});
570+
});
571+

0 commit comments

Comments
 (0)