Skip to content

Commit

Permalink
fix: windows support
Browse files Browse the repository at this point in the history
Signed-off-by: Ilona Shishov <[email protected]>
  • Loading branch information
IlonaShishov committed Jul 15, 2024
1 parent 7ac4384 commit 26bd5af
Show file tree
Hide file tree
Showing 20 changed files with 61 additions and 49 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ on:
pull_request:

jobs:
lint:
name: Run ESLint
lint-and-test:
name: Lint and Test
runs-on: ubuntu-latest

steps:
Expand All @@ -25,6 +25,9 @@ jobs:

- name: Run lint
run: npm run lint

- name: Run test
run: npm run test

check-dist:
name: Check Distribution
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/scan_docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: RHDA Scan
id: rhda_scan
uses: RHEcosystemAppEng/rhda-github-action@main
uses: RHEcosystemAppEng/rhda-github-action@chore/unit-tests
with:
manifest_directory: manifests/docker
manifest_file: ${{ matrix.manifest_file }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/scan_go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:

- name: RHDA Scan
id: rhda_scan
uses: RHEcosystemAppEng/rhda-github-action@main
uses: RHEcosystemAppEng/rhda-github-action@chore/unit-tests
with:
manifest_directory: manifests/golang
manifest_file: ${{ matrix.manifest_file }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/scan_gradle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:

- name: RHDA Scan
id: rhda_scan
uses: RHEcosystemAppEng/rhda-github-action@main
uses: RHEcosystemAppEng/rhda-github-action@chore/unit-tests
with:
manifest_directory: manifests/gradle
manifest_file: ${{ matrix.manifest_file }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/scan_maven.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:

- name: RHDA Scan
id: rhda_scan
uses: RHEcosystemAppEng/rhda-github-action@main
uses: RHEcosystemAppEng/rhda-github-action@chore/unit-tests
with:
manifest_directory: manifests/maven
manifest_file: ${{ matrix.manifest_file }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/scan_npm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:

- name: RHDA Scan
id: rhda_scan
uses: RHEcosystemAppEng/rhda-github-action@main
uses: RHEcosystemAppEng/rhda-github-action@chore/unit-tests
with:
manifest_directory: manifests/npm
manifest_file: ${{ matrix.manifest_file }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/scan_podman.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: RHDA Scan
id: rhda_scan
uses: RHEcosystemAppEng/rhda-github-action@main
uses: RHEcosystemAppEng/rhda-github-action@chore/unit-tests
with:
manifest_directory: manifests/podman
manifest_file: ${{ matrix.manifest_file }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/scan_python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:

- name: RHDA Scan
id: rhda_scan
uses: RHEcosystemAppEng/rhda-github-action@main
uses: RHEcosystemAppEng/rhda-github-action@chore/unit-tests
with:
manifest_directory: manifests/python
manifest_file: ${{ matrix.manifest_file }}
Expand Down
16 changes: 7 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137975,7 +137975,6 @@ function escapeWindowsPathForActionsOutput(path) {
async function zipFile(file) {
const fileContents = await external_fs_.readFileSync(file, 'utf-8');
const zippedContents = (await external_zlib_.gzipSync(fileContents)).toString('base64');
// ghCore.info(`Zipped upload size: ${utils.convertToHumanFileSize(zippedContents.length)}`);
return zippedContents;
}
/**
Expand Down Expand Up @@ -138037,8 +138036,6 @@ const UTM_SOURCE = 'github-actions';
const SARIF_SCHEMA_URL = 'https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json';
// Version of the SARIF schema.
const SARIF_SCHEMA_VERSION = '2.1.0';
// Default directory to look for manifest files.
const DEFAULT_MANIFEST_DIR = '.';
// Supported manifests and files
const GO_MOD = 'go.mod';
const POM_XML = 'pom.xml';
Expand Down Expand Up @@ -138088,7 +138085,9 @@ async function resolveManifestFilePath() {
if (!manifestDirInput) {
core.info(`"${Inputs.MANIFEST_DIRECTORY}" not provided. Using working directory "${process.cwd()}"`);
}
const manifestDir = manifestDirInput || DEFAULT_MANIFEST_DIR;
const manifestDir = manifestDirInput
? external_path_default().resolve(manifestDirInput)
: process.cwd();
let manifestFilename;
if (manifestFileInput) {
manifestFilename = manifestFileInput;
Expand All @@ -138098,7 +138097,7 @@ async function resolveManifestFilePath() {
}
else {
core.info(`"${Inputs.MANIFEST_FILE}" input not provided. Auto-detecting manifest file`);
core.info(`🔍 Looking for manifest in "${external_path_default().join(process.cwd(), manifestDir)}"...`);
core.info(`🔍 Looking for manifest in "${manifestDir}"...`);
manifestFilename = await autoDetectManifest(manifestDir);
}
const resolvedManifestPath = external_path_default().join(manifestDir, manifestFilename);
Expand Down Expand Up @@ -140934,7 +140933,6 @@ function imageAnalysisService(images, options) {
async function stackAnalysisService(pathToManifest, options) {
try {
// Get stack analysis in JSON format
console.log(`pathToManifest: ${pathToManifest}`);
const stackAnalysisReportJson = await src.stackAnalysis(pathToManifest, false, options);
return stackAnalysisReportJson;
}
Expand Down Expand Up @@ -141111,7 +141109,7 @@ async function generateRHDAReport(manifestFilePath, ecosystem) {
else {
rhdaReportJson = await stackAnalysisService(manifestFilePath, getDependencyAnalysisConfig());
}
const rhdaReportJsonFilePath = `${process.cwd()}/${core.getInput(Inputs.RHDA_REPORT_NAME)}.json`;
const rhdaReportJsonFilePath = `${process.cwd()}${getOS() === 'windows' ? '\\' : '/'}${core.getInput(Inputs.RHDA_REPORT_NAME)}.json`;
await writeToFile(JSON.stringify(rhdaReportJson, null, 4), rhdaReportJsonFilePath);
core.info(`✍️ Setting output "${Outputs.RHDA_REPORT_JSON}" to ${rhdaReportJsonFilePath}`);
core.setOutput(Outputs.RHDA_REPORT_JSON, rhdaReportJsonFilePath);
Expand Down Expand Up @@ -141306,7 +141304,7 @@ function fetchResult(ruleId, textMessage, manifestFilePath, startLine) {
text: textMessage,
};
const artifactLocation = {
uri: manifestFilePath.split((external_path_default()).sep).join((external_path_default()).posix.sep),
uri: 'file:///' + manifestFilePath.split((external_path_default()).sep).join((external_path_default()).posix.sep),
};
const region = {
startLine: startLine,
Expand Down Expand Up @@ -141787,7 +141785,7 @@ async function waitForUploadToFinish(ghToken, sarifId) {
async function handleSarif(rhdaReportJson, manifestFilePath, ecosystem, sha, ref, analysisStartTime, prData) {
core.info(`⏳ Converting RHDA report JSON to SARIF...`);
const { sarifObject: rhdaReportSarif, vulSeverity: vulSeverity } = await generateSarif(rhdaReportJson, manifestFilePath, ecosystem);
const rhdaReportSarifFilePath = `${process.cwd()}/${core.getInput(Inputs.RHDA_REPORT_NAME)}.sarif`;
const rhdaReportSarifFilePath = `${process.cwd()}${getOS() === 'windows' ? '\\' : '/'}${core.getInput(Inputs.RHDA_REPORT_NAME)}.sarif`;
await writeToFile(JSON.stringify(rhdaReportSarif, null, 4), rhdaReportSarifFilePath);
core.info(`✅ Successfully converted RHDA report JSON to SARIF`);
core.info(`✍️ Setting output "${Outputs.RHDA_REPORT_SARIF}" to ${rhdaReportSarifFilePath}`);
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ export const SARIF_SCHEMA_URL =
// Version of the SARIF schema.
export const SARIF_SCHEMA_VERSION = '2.1.0';

// Default directory to look for manifest files.
export const DEFAULT_MANIFEST_DIR = '.';

// Supported manifests and files
const GO_MOD = 'go.mod';
const POM_XML = 'pom.xml';
Expand Down
1 change: 0 additions & 1 deletion src/exhortServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ async function stackAnalysisService(
): Promise<string | exhort.AnalysisReport> {
try {
// Get stack analysis in JSON format
console.log(`pathToManifest: ${pathToManifest}`)
const stackAnalysisReportJson = await exhort.stackAnalysis(
pathToManifest,
false,
Expand Down
13 changes: 5 additions & 8 deletions src/manifestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import path from 'path';
import { promises as fs } from 'fs';

import { Inputs } from './generated/inputs-outputs.js';
import {
fileNameToEcosystemMappings,
DEFAULT_MANIFEST_DIR,
} from './constants.js';
import { fileNameToEcosystemMappings } from './constants.js';

/**
* Resolves the manifest file path and its corresponding ecosystem.
Expand All @@ -25,7 +22,9 @@ export async function resolveManifestFilePath(): Promise<{
`"${Inputs.MANIFEST_DIRECTORY}" not provided. Using working directory "${process.cwd()}"`,
);
}
const manifestDir = manifestDirInput || DEFAULT_MANIFEST_DIR;
const manifestDir = manifestDirInput
? path.resolve(manifestDirInput)
: process.cwd();

let manifestFilename: string;
if (manifestFileInput) {
Expand All @@ -38,9 +37,7 @@ export async function resolveManifestFilePath(): Promise<{
ghCore.info(
`"${Inputs.MANIFEST_FILE}" input not provided. Auto-detecting manifest file`,
);
ghCore.info(
`🔍 Looking for manifest in "${path.join(process.cwd(), manifestDir)}"...`,
);
ghCore.info(`🔍 Looking for manifest in "${manifestDir}"...`);

manifestFilename = await autoDetectManifest(manifestDir);
}
Expand Down
2 changes: 1 addition & 1 deletion src/rhda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function generateRHDAReport(
);
}

const rhdaReportJsonFilePath: string = `${process.cwd()}/${ghCore.getInput(Inputs.RHDA_REPORT_NAME)}.json`;
const rhdaReportJsonFilePath: string = `${process.cwd()}${utils.getOS() === 'windows' ? '\\' : '/'}${ghCore.getInput(Inputs.RHDA_REPORT_NAME)}.json`;
await utils.writeToFile(
JSON.stringify(rhdaReportJson, null, 4),
rhdaReportJsonFilePath,
Expand Down
2 changes: 1 addition & 1 deletion src/sarif/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export async function handleSarif(
ecosystem,
);

const rhdaReportSarifFilePath: string = `${process.cwd()}/${ghCore.getInput(Inputs.RHDA_REPORT_NAME)}.sarif`;
const rhdaReportSarifFilePath: string = `${process.cwd()}${utils.getOS() === 'windows' ? '\\' : '/'}${ghCore.getInput(Inputs.RHDA_REPORT_NAME)}.sarif`;
await utils.writeToFile(
JSON.stringify(rhdaReportSarif, null, 4),
rhdaReportSarifFilePath,
Expand Down
2 changes: 1 addition & 1 deletion src/sarif/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function fetchResult(
text: textMessage,
};
const artifactLocation: sarif.ArtifactLocation = {
uri: manifestFilePath.split(path.sep).join(path.posix.sep),
uri: 'file:///' + manifestFilePath.split(path.sep).join(path.posix.sep),
};
const region: sarif.Region = {
startLine: startLine,
Expand Down
1 change: 0 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ export async function zipFile(file: string): Promise<string> {
const zippedContents = (await zlib.gzipSync(fileContents)).toString(
'base64',
);
// ghCore.info(`Zipped upload size: ${utils.convertToHumanFileSize(zippedContents.length)}`);

return zippedContents;
}
Expand Down
15 changes: 6 additions & 9 deletions test/manifestHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { Dirent, promises as fs } from 'fs';

import { resolveManifestFilePath } from '../src/manifestHandler';
import { Inputs } from '../src/generated/inputs-outputs';
import {
fileNameToEcosystemMappings,
DEFAULT_MANIFEST_DIR,
} from '../src/constants';
import { fileNameToEcosystemMappings } from '../src/constants';

vi.mock('@actions/core', () => ({
getInput: vi.fn(),
Expand Down Expand Up @@ -43,7 +40,7 @@ describe('resolveManifestFilePath', () => {
`"${Inputs.MANIFEST_DIRECTORY}" not provided. Using working directory "${process.cwd()}"`,
);
expect(result).toEqual({
manifestFilePath: path.join(DEFAULT_MANIFEST_DIR, 'package.json'),
manifestFilePath: path.join(process.cwd(), 'package.json'),
ecosystem: fileNameToEcosystemMappings['package.json'],
});
});
Expand Down Expand Up @@ -88,14 +85,14 @@ describe('resolveManifestFilePath', () => {
);
expect(ghCore.info).toHaveBeenNthCalledWith(
3,
`🔍 Looking for manifest in "${path.join(process.cwd(), DEFAULT_MANIFEST_DIR)}"...`,
`🔍 Looking for manifest in "${process.cwd()}"...`,
);
expect(ghCore.info).toHaveBeenNthCalledWith(
4,
`ℹ️ Manifest file path is "${path.join(DEFAULT_MANIFEST_DIR, 'package.json')}"`,
`ℹ️ Manifest file path is "${path.join(process.cwd(), 'package.json')}"`,
);
expect(result).toEqual({
manifestFilePath: path.join(DEFAULT_MANIFEST_DIR, 'package.json'),
manifestFilePath: path.join(process.cwd(), 'package.json'),
ecosystem: fileNameToEcosystemMappings['package.json'],
});
});
Expand All @@ -116,7 +113,7 @@ describe('resolveManifestFilePath', () => {
] as unknown as Dirent[]);

await expect(resolveManifestFilePath()).rejects.toThrow(
`Failed to find a manifest file in "${DEFAULT_MANIFEST_DIR}" matching one of the expected project types. Expected to find one of: ${Object.keys(fileNameToEcosystemMappings).join(', ')}`,
`Failed to find a manifest file in "${process.cwd()}" matching one of the expected project types. Expected to find one of: ${Object.keys(fileNameToEcosystemMappings).join(', ')}`,
);
});
});
1 change: 1 addition & 0 deletions test/rhda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ vi.mock('../src/imageAnalysis', () => ({

vi.mock('../src/utils', () => ({
writeToFile: vi.fn(),
getOS: vi.fn().mockImplementation(() => 'linux'),
}));

describe('generateRHDAReport', () => {
Expand Down
31 changes: 26 additions & 5 deletions test/sarif/results.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import path from 'path';

import { rhdaToResult } from '../../src/sarif/results';
import * as types from '../../src/sarif/types';
Expand Down Expand Up @@ -79,7 +80,11 @@ describe('rhdaToResult', () => {
{
physicalLocation: {
artifactLocation: {
uri: manifestFilePath,
uri:
'file:///' +
manifestFilePath
.split(path.sep)
.join(path.posix.sep),
},
region: {
startLine: startLine,
Expand All @@ -103,7 +108,11 @@ describe('rhdaToResult', () => {
{
physicalLocation: {
artifactLocation: {
uri: manifestFilePath,
uri:
'file:///' +
manifestFilePath
.split(path.sep)
.join(path.posix.sep),
},
region: {
startLine: startLine,
Expand Down Expand Up @@ -176,7 +185,11 @@ describe('rhdaToResult', () => {
{
physicalLocation: {
artifactLocation: {
uri: manifestFilePath,
uri:
'file:///' +
manifestFilePath
.split(path.sep)
.join(path.posix.sep),
},
region: {
startLine: startLine,
Expand All @@ -200,7 +213,11 @@ describe('rhdaToResult', () => {
{
physicalLocation: {
artifactLocation: {
uri: manifestFilePath,
uri:
'file:///' +
manifestFilePath
.split(path.sep)
.join(path.posix.sep),
},
region: {
startLine: startLine,
Expand Down Expand Up @@ -252,7 +269,11 @@ describe('rhdaToResult', () => {
{
physicalLocation: {
artifactLocation: {
uri: manifestFilePath,
uri:
'file:///' +
manifestFilePath
.split(path.sep)
.join(path.posix.sep),
},
region: {
startLine: startLine,
Expand Down

0 comments on commit 26bd5af

Please sign in to comment.