Skip to content

Commit

Permalink
Internal - Added usage vars to be captured by JFrog CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalb4doc committed Dec 23, 2024
1 parent 1a69118 commit bd6dc39
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ author: "JFrog"
inputs:
version:
description: "JFrog CLI Version"
default: "2.72.2"
default: "2.72.3"
required: false
download-repository:
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."
Expand Down
21 changes: 19 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Utils {
*/
static getApplicationKey() {
return __awaiter(this, void 0, void 0, function* () {
const configFilePath = path.join(this.JF_CONFIG_DIR_NAME, this.JF_CONFIG_FILE);
const configFilePath = path.join(this.JF_CONFIG_DIR_NAME, this.JF_CONFIG_FILE_NAME);
try {
const config = yield this.readConfigFromFileSystem(configFilePath);
if (!config) {
Expand Down Expand Up @@ -445,6 +445,23 @@ class Utils {
if (!core.getBooleanInput(Utils.JOB_SUMMARY_DISABLE)) {
Utils.enableJobSummaries();
}
Utils.setUsageEnvVars();
}
// Set usage variables to be captured by JFrog CLI.
static setUsageEnvVars() {
// Repository name, defaulting to an empty string if undefined.
const repoName = process.env.GITHUB_REPOSITORY || '';
// Workflow name, defaulting to an empty string if undefined.
const jobId = process.env.GITHUB_WORKFLOW || '';
// Run ID, defaulting to an empty string if undefined.
const runId = process.env.GITHUB_RUN_ID || '';
// Boolean flag indicating if JF_GIT_TOKEN is set.
const jfGitTokenSet = !!process.env.JF_GIT_TOKEN;
// Export environment variables for JFrog CLI usage.
core.exportVariable('JFROG_CLI_USAGE_JOB_ID', jobId);
core.exportVariable('JFROG_CLI_USAGE_RUN_ID', runId);
core.exportVariable('JFROG_CLI_USAGE_GIT_REPO', repoName);
core.exportVariable('JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED', jfGitTokenSet);
}
/**
* Enabling job summary is done by setting the output dir for the summaries.
Expand Down Expand Up @@ -885,7 +902,7 @@ Utils.KEY = 'key';
// Config file directory name
Utils.JF_CONFIG_DIR_NAME = '.jfrog';
// Config file name
Utils.JF_CONFIG_FILE = 'config.yml';
Utils.JF_CONFIG_FILE_NAME = 'config.yml';
// Disable Job Summaries feature flag
Utils.JOB_SUMMARY_DISABLE = 'disable-job-summary';
// Disable auto build info publish feature flag
Expand Down
23 changes: 23 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,29 @@ export class Utils {
if (!core.getBooleanInput(Utils.JOB_SUMMARY_DISABLE)) {
Utils.enableJobSummaries();
}

Utils.setUsageEnvVars()
}

// Set usage variables to be captured by JFrog CLI.
public static setUsageEnvVars(): void {
// Repository name, defaulting to an empty string if undefined.
const repoName: string = process.env.GITHUB_REPOSITORY || '';

// Workflow name, defaulting to an empty string if undefined.
const jobId: string = process.env.GITHUB_WORKFLOW || '';

// Run ID, defaulting to an empty string if undefined.
const runId: string = process.env.GITHUB_RUN_ID || '';

// Boolean flag indicating if JF_GIT_TOKEN is set.
const jfGitTokenSet: boolean = !!process.env.JF_GIT_TOKEN;

// Export environment variables for JFrog CLI usage.
core.exportVariable('JFROG_CLI_USAGE_JOB_ID', jobId);
core.exportVariable('JFROG_CLI_USAGE_RUN_ID', runId);
core.exportVariable('JFROG_CLI_USAGE_GIT_REPO', repoName);
core.exportVariable('JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED', jfGitTokenSet);
}

/**
Expand Down
41 changes: 41 additions & 0 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,44 @@ describe('getApplicationKey', () => {
expect(result).toBe('');
});
});

describe('setUsageEnvVars', () => {
beforeEach(() => {
// Clear environment variables before each test
delete process.env.GITHUB_REPOSITORY;
delete process.env.GITHUB_WORKFLOW;
delete process.env.GITHUB_RUN_ID;
delete process.env.JF_GIT_TOKEN;

jest.clearAllMocks();
});

it('should export the correct environment variables when all inputs are set', () => {
// Mock environment variables
process.env.GITHUB_REPOSITORY = 'owner/repo';
process.env.GITHUB_WORKFLOW = 'test-workflow';
process.env.GITHUB_RUN_ID = '12345';
process.env.JF_GIT_TOKEN = 'dummy-token';

// Call the function
Utils.setUsageEnvVars();

// Verify exported variables
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_JOB_ID', 'test-workflow');
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_RUN_ID', '12345');
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_GIT_REPO', 'owner/repo');
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED', true);
});

it('should export empty strings for missing environment variables', () => {
// Call the function with no environment variables set
Utils.setUsageEnvVars();

// Verify exported variables
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_JOB_ID', '');
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_RUN_ID', '');
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_GIT_REPO', '');
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED', false);
});
});

0 comments on commit bd6dc39

Please sign in to comment.