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 Job Summary disabled on self hosted machines #194

Merged
merged 3 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 14 additions & 20 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -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
Expand All @@ -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';
Expand Down
38 changes: 16 additions & 22 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove un used code as the sections logic was moved to the CLI

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;
Expand All @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
36 changes: 36 additions & 0 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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();
});
});
});
Loading