diff --git a/lib/utils.js b/lib/utils.js index f57c860c3..db6925ab6 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -32,7 +32,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.Utils = exports.MarkdownSection = void 0; +exports.Utils = void 0; const core = __importStar(require("@actions/core")); const exec_1 = require("@actions/exec"); const http_client_1 = require("@actions/http-client"); @@ -42,12 +42,6 @@ const os_1 = require("os"); const path = __importStar(require("path")); const path_1 = require("path"); const semver_1 = require("semver"); -var MarkdownSection; -(function (MarkdownSection) { - MarkdownSection["Upload"] = "upload"; - MarkdownSection["BuildInfo"] = "build-info"; - MarkdownSection["Security"] = "security"; -})(MarkdownSection || (exports.MarkdownSection = MarkdownSection = {})); class Utils { /** * Retrieves server credentials for accessing JFrog's server @@ -175,9 +169,8 @@ class Utils { // Main OIDC user parsing logic if (subject.startsWith('jfrt@') || subject.includes('/users/')) { let lastSlashIndex = subject.lastIndexOf('/'); - let userSubstring = subject.substring(lastSlashIndex + 1); // Return the user extracted from the token - return userSubstring; + return subject.substring(lastSlashIndex + 1); } // No parsing was needed, returning original sub from the token as the user return subject; @@ -336,13 +329,11 @@ class Utils { } /** * Enabling job summary is done by setting the output dir for the summaries. - * If the output dir is not set, the CLI won't generate the summary markdown files. + * If the output dir is not set, the CLI won't generate the summary Markdown files. */ static enableJobSummaries() { - let commandSummariesOutputDir = process.env.RUNNER_TEMP; - if (commandSummariesOutputDir) { - Utils.exportVariableIfNotSet(Utils.JFROG_CLI_COMMAND_SUMMARY_OUTPUT_DIR_ENV, commandSummariesOutputDir); - } + let tempDir = this.getTempDirectory(); + Utils.exportVariableIfNotSet(Utils.JFROG_CLI_COMMAND_SUMMARY_OUTPUT_DIR_ENV, tempDir); } static exportVariableIfNotSet(key, value) { if (!process.env[key]) { @@ -594,6 +585,15 @@ class Utils { } }); } + static getTempDirectory() { + // Determine the temporary directory path, prioritizing RUNNER_TEMP + // Runner_Temp is set on GitHub machines, but on self-hosted it could be unset. + const tempDir = process.env.RUNNER_TEMP || (0, os_1.tmpdir)(); + if (!tempDir) { + throw new Error('Failed to determine the temporary directory'); + } + return tempDir; + } } exports.Utils = Utils; // eslint-disable-next-line @typescript-eslint/no-var-requires @@ -619,12 +619,6 @@ Utils.SETUP_JFROG_CLI_SERVER_ID = 'setup-jfrog-cli-server'; Utils.JOB_SUMMARY_DIR_NAME = 'jfrog-command-summary'; // JFrog CLI command summary output directory environment variable Utils.JFROG_CLI_COMMAND_SUMMARY_OUTPUT_DIR_ENV = 'JFROG_CLI_COMMAND_SUMMARY_OUTPUT_DIR'; -// Workflow summary section files. Order of sections in this array impacts the order in the final markdown. -Utils.JOB_SUMMARY_MARKDOWN_SECTIONS_NAMES = [ - MarkdownSection.Security, - MarkdownSection.BuildInfo, - MarkdownSection.Upload, -]; // Inputs // Version input Utils.CLI_VERSION_ARG = 'version'; diff --git a/src/utils.ts b/src/utils.ts index bf401ed6b..f9a765da9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,19 +2,13 @@ import * as core from '@actions/core'; import { exec, ExecOptions, ExecOutput, getExecOutput } from '@actions/exec'; import { HttpClient, HttpClientResponse } from '@actions/http-client'; import * as toolCache from '@actions/tool-cache'; -import { chmodSync, existsSync, promises as fs } from 'fs'; +import { chmodSync, existsSync, mkdirSync, promises as fs } from 'fs'; import { OutgoingHttpHeaders } from 'http'; -import { arch, platform } from 'os'; +import { arch, platform, tmpdir } from 'os'; import * as path from 'path'; import { join } from 'path'; import { lt } from 'semver'; -export enum MarkdownSection { - Upload = 'upload', - BuildInfo = 'build-info', - Security = 'security', -} - export class Utils { // eslint-disable-next-line @typescript-eslint/no-var-requires public static readonly USER_AGENT: string = 'setup-jfrog-cli-github-action/' + require('../package.json').version; @@ -41,13 +35,6 @@ export class Utils { // JFrog CLI command summary output directory environment variable public static readonly JFROG_CLI_COMMAND_SUMMARY_OUTPUT_DIR_ENV: string = 'JFROG_CLI_COMMAND_SUMMARY_OUTPUT_DIR'; - // Workflow summary section files. Order of sections in this array impacts the order in the final markdown. - public static JOB_SUMMARY_MARKDOWN_SECTIONS_NAMES: MarkdownSection[] = [ - MarkdownSection.Security, - MarkdownSection.BuildInfo, - MarkdownSection.Upload, - ]; - // Inputs // Version input private static readonly CLI_VERSION_ARG: string = 'version'; @@ -204,9 +191,8 @@ export class Utils { // Main OIDC user parsing logic if (subject.startsWith('jfrt@') || subject.includes('/users/')) { let lastSlashIndex: number = subject.lastIndexOf('/'); - let userSubstring: string = subject.substring(lastSlashIndex + 1); // Return the user extracted from the token - return userSubstring; + return subject.substring(lastSlashIndex + 1); } // No parsing was needed, returning original sub from the token as the user return subject; @@ -384,13 +370,11 @@ export class Utils { /** * Enabling job summary is done by setting the output dir for the summaries. - * If the output dir is not set, the CLI won't generate the summary markdown files. + * If the output dir is not set, the CLI won't generate the summary Markdown files. */ private static enableJobSummaries() { - let commandSummariesOutputDir: string | undefined = process.env.RUNNER_TEMP; - if (commandSummariesOutputDir) { - Utils.exportVariableIfNotSet(Utils.JFROG_CLI_COMMAND_SUMMARY_OUTPUT_DIR_ENV, commandSummariesOutputDir); - } + let tempDir: string = this.getTempDirectory(); + Utils.exportVariableIfNotSet(Utils.JFROG_CLI_COMMAND_SUMMARY_OUTPUT_DIR_ENV, tempDir); } private static exportVariableIfNotSet(key: string, value: string) { @@ -645,6 +629,16 @@ export class Utils { httpClient.dispose(); } } + + private static getTempDirectory(): string { + // Determine the temporary directory path, prioritizing RUNNER_TEMP + // Runner_Temp is set on GitHub machines, but on self-hosted it could be unset. + const tempDir: string = process.env.RUNNER_TEMP || tmpdir(); + if (!tempDir) { + throw new Error('Failed to determine the temporary directory'); + } + return tempDir; + } } export interface DownloadDetails { diff --git a/test/main.spec.ts b/test/main.spec.ts index ca864b635..4464fd497 100644 --- a/test/main.spec.ts +++ b/test/main.spec.ts @@ -2,6 +2,7 @@ import * as os from 'os'; import * as core from '@actions/core'; import { Utils, DownloadDetails, JfrogCredentials, JWTTokenData } from '../src/utils'; +import { tmpdir } from 'os'; jest.mock('os'); jest.mock('@actions/core'); @@ -327,5 +328,40 @@ describe('Job Summaries', () => { Utils.setCliEnv(); expect(process.env[Utils.JFROG_CLI_COMMAND_SUMMARY_OUTPUT_DIR_ENV]).toBe('/tmp'); }); + + it('should handle self-hosted machines and set JFROG_CLI_COMMAND_SUMMARY_OUTPUT_DIR based on OS temp dir', () => { + // Mock os.tmpdir() to simulate different OS temp directories + const tempDir: string = '/mocked-temp-dir'; + jest.spyOn(os, 'tmpdir').mockReturnValue(tempDir); + + myCore.getBooleanInput = jest.fn().mockImplementation(() => { + return false; + }); + myCore.exportVariable = jest.fn().mockImplementation((name: string, val: string) => { + process.env[name] = val; + }); + + Utils.setCliEnv(); + + expect(process.env[Utils.JFROG_CLI_COMMAND_SUMMARY_OUTPUT_DIR_ENV]).toBe(tempDir); + }); + + it('Should throw error when failing to get temp dir', () => { + // Mock os.tmpdir() to return an empty string + jest.spyOn(os, 'tmpdir').mockReturnValue(''); + + myCore.getBooleanInput = jest.fn().mockImplementation(() => { + return false; + }); + myCore.exportVariable = jest.fn().mockImplementation((name: string, val: string) => { + process.env[name] = val; + }); + + // Expect the function to throw an error + expect(() => Utils.setCliEnv()).toThrow('Failed to determine the temporary directory'); + + // Restore the mock to avoid affecting other tests + jest.restoreAllMocks(); + }); }); });