Skip to content

Commit

Permalink
chore: reorganize interfaces exports
Browse files Browse the repository at this point in the history
  • Loading branch information
saark-snyk committed Dec 20, 2021
1 parent 097ed94 commit d4b04a6
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 52 deletions.
57 changes: 29 additions & 28 deletions src/analysis.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-await-in-loop */
import omit from 'lodash.omit';

import { AnalyzeFoldersOptions, prepareExtendingBundle, resolveBundleFilePath, calcHash } from './files';
import { prepareExtendingBundle, resolveBundleFilePath, calcHash } from './files';
import { POLLING_INTERVAL } from './constants';
import {
GetAnalysisErrorCodes,
Expand All @@ -10,13 +10,19 @@ import {
Result,
GetAnalysisResponseDto,
AnalysisFailedResponse,
AnalysisOptions,
ConnectionOptions,
GetAnalysisOptions,
} from './http';
import { createBundleFromFolders, FileBundle, remoteBundleFactory } from './bundles';
import { createBundleFromFolders, remoteBundleFactory } from './bundles';
import { emitter } from './emitter';
import { AnalysisResult, AnalysisResultLegacy, AnalysisResultSarif, AnalysisFiles, Suggestion } from './interfaces/analysis-result.interface';
import {
AnalysisResult,
AnalysisResultLegacy,
AnalysisResultSarif,
AnalysisFiles,
Suggestion,
} from './interfaces/analysis-result.interface';
import { FileAnalysisOptions } from './interfaces/analysis-options.interface';
import { FileAnalysis } from './interfaces/files.interface';

const sleep = (duration: number) => new Promise(resolve => setTimeout(resolve, duration));

Expand Down Expand Up @@ -86,17 +92,6 @@ function normalizeResultFiles(files: AnalysisFiles, baseDir: string): AnalysisFi
return files;
}

interface FileAnalysisOptions {
connection: ConnectionOptions;
analysisOptions: AnalysisOptions;
fileOptions: AnalyzeFoldersOptions;
}

export interface FileAnalysis extends FileAnalysisOptions {
fileBundle: FileBundle;
analysisResults: AnalysisResult;
}

export async function analyzeFolders(options: FileAnalysisOptions): Promise<FileAnalysis | null> {
const fileBundle = await createBundleFromFolders({
...options.connection,
Expand All @@ -120,19 +115,24 @@ export async function analyzeFolders(options: FileAnalysisOptions): Promise<File
return { fileBundle, analysisResults, ...options };
}

function mergeBundleResults(oldAnalysisResults: AnalysisResult,
function mergeBundleResults(
oldAnalysisResults: AnalysisResult,
newAnalysisResults: AnalysisResult,
limitToFiles: string[],
removedFiles: string[] = [],
baseDir: string,
): AnalysisResult {

if (newAnalysisResults.type == 'sarif') {
return mergeSarifResults(oldAnalysisResults as AnalysisResultSarif, newAnalysisResults, limitToFiles, removedFiles);
}

return mergeLegacyResults(oldAnalysisResults as AnalysisResultLegacy, newAnalysisResults, limitToFiles, removedFiles, baseDir);

return mergeLegacyResults(
oldAnalysisResults as AnalysisResultLegacy,
newAnalysisResults,
limitToFiles,
removedFiles,
baseDir,
);
}

function mergeSarifResults(
Expand Down Expand Up @@ -227,8 +227,7 @@ function mergeLegacyResults(
limitToFiles: string[],
removedFiles: string[] = [],
baseDir: string,
): AnalysisResultLegacy {

): AnalysisResultLegacy {
// expand relative file names to absolute ones only for legacy results
newAnalysisResults.files = normalizeResultFiles(newAnalysisResults.files, baseDir);

Expand Down Expand Up @@ -259,11 +258,7 @@ function mergeLegacyResults(
};
}

interface ExtendAnalysisOptions extends FileAnalysis {
files: string[];
}

export async function extendAnalysis(options: ExtendAnalysisOptions): Promise<FileAnalysis | null> {
export async function extendAnalysis(options: FileAnalysis & { files: string[] }): Promise<FileAnalysis | null> {
const { files, removedFiles } = await prepareExtendingBundle(
options.fileBundle.baseDir,
options.fileBundle.supportedFiles,
Expand Down Expand Up @@ -303,7 +298,13 @@ export async function extendAnalysis(options: ExtendAnalysisOptions): Promise<Fi
limitToFiles,
});

analysisResults = mergeBundleResults(options.analysisResults, analysisResults, limitToFiles, removedFiles, options.fileBundle.baseDir);
analysisResults = mergeBundleResults(
options.analysisResults,
analysisResults,
limitToFiles,
removedFiles,
options.fileBundle.baseDir,
);

return { ...options, fileBundle, analysisResults };
}
6 changes: 4 additions & 2 deletions src/bundles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { BundleFiles, FileInfo, SupportedFiles } from './interfaces/files.interf
import {
composeFilePayloads,
resolveBundleFiles,
AnalyzeFoldersOptions,
collectIgnoreRules,
determineBaseDir,
collectBundleFiles,
Expand All @@ -29,6 +28,7 @@ import {

import { MAX_PAYLOAD, MAX_UPLOAD_ATTEMPTS, UPLOAD_CONCURRENCY } from './constants';
import { emitter } from './emitter';
import { AnalyzeFoldersOptions } from './interfaces/analysis-options.interface';

type BundleErrorCodes = CreateBundleErrorCodes | CheckBundleErrorCodes | ExtendBundleErrorCodes;

Expand All @@ -38,7 +38,9 @@ interface PrepareRemoteBundleOptions extends ConnectionOptions {
removedFiles?: string[];
}

async function* prepareRemoteBundle(options: PrepareRemoteBundleOptions): AsyncGenerator<Result<RemoteBundle, BundleErrorCodes>> {
async function* prepareRemoteBundle(
options: PrepareRemoteBundleOptions,
): AsyncGenerator<Result<RemoteBundle, BundleErrorCodes>> {
let response: Result<RemoteBundle, BundleErrorCodes>;
let { bundleHash } = options;
let cumulativeProgress = 0;
Expand Down
18 changes: 3 additions & 15 deletions src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
CACHE_KEY,
DOTSNYK_FILENAME,
} from './constants';

import { CollectBundleFilesOptions } from './interfaces/analysis-options.interface';
import { SupportedFiles, FileInfo } from './interfaces/files.interface';

const isWindows = nodePath.sep === '\\';
Expand Down Expand Up @@ -225,18 +225,6 @@ async function* searchFiles(
}
}

export interface AnalyzeFoldersOptions {
paths: string[];
symlinksEnabled?: boolean;
defaultFileIgnores?: string[];
}

export interface CollectBundleFilesOptions extends AnalyzeFoldersOptions {
supportedFiles: SupportedFiles;
baseDir: string;
fileIgnores: string[];
}

/**
* Returns bundle files from requested paths
* */
Expand Down Expand Up @@ -350,15 +338,15 @@ export async function prepareExtendingBundle(
};
}

export function getBundleFilePath(filePath: string, baseDir: string) {
export function getBundleFilePath(filePath: string, baseDir: string): string {
const relPath = baseDir ? nodePath.relative(baseDir, filePath) : filePath; // relPath without explicit base makes no sense
const posixPath = !isWindows ? relPath : relPath.replace(/\\/g, '/');
return encodeURI(posixPath);
}

export function calcHash(content: string): string {
return crypto.createHash(HASH_ALGORITHM).update(content).digest(ENCODE_TYPE);
};
}

export async function getFileInfo(
filePath: string,
Expand Down
13 changes: 10 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { analyzeFolders, extendAnalysis, FileAnalysis } from './analysis';
import { analyzeFolders, extendAnalysis } from './analysis';
import { createBundleFromFolders } from './bundles';
import { emitter } from './emitter';
import { startSession, checkSession, getAnalysis, getIpFamily, IpFamily } from './http';
import * as constants from './constants';
import { getGlobPatterns } from './files';

import { SupportedFiles } from './interfaces/files.interface';
import { SupportedFiles, FileAnalysis } from './interfaces/files.interface';
import { AnalysisSeverity } from './interfaces/analysis-options.interface';
import { AnalysisResult, AnalysisResultLegacy, FilePath, FileSuggestion, Suggestion, Marker } from './interfaces/analysis-result.interface';
import {
AnalysisResult,
AnalysisResultLegacy,
FilePath,
FileSuggestion,
Suggestion,
Marker,
} from './interfaces/analysis-result.interface';

export {
getGlobPatterns,
Expand Down
20 changes: 20 additions & 0 deletions src/interfaces/analysis-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import { SupportedFiles } from '..';
import { AnalysisOptions, ConnectionOptions } from '../http';

// eslint-disable-next-line import/prefer-default-export, no-shadow
export enum AnalysisSeverity {
info = 1,
warning = 2,
critical = 3,
}
export interface FileAnalysisOptions {
connection: ConnectionOptions;
analysisOptions: AnalysisOptions;
fileOptions: AnalyzeFoldersOptions;
}

export interface AnalyzeFoldersOptions {
paths: string[];
symlinksEnabled?: boolean;
defaultFileIgnores?: string[];
}

export interface CollectBundleFilesOptions extends AnalyzeFoldersOptions {
supportedFiles: SupportedFiles;
baseDir: string;
fileIgnores: string[];
}
9 changes: 9 additions & 0 deletions src/interfaces/files.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { AnalysisResult } from '..';
import { FileBundle } from '../bundles';
import { FileAnalysisOptions } from './analysis-options.interface';

export interface File {
hash: string;
content: string;
Expand All @@ -19,3 +23,8 @@ export type SupportedFiles = {
configFiles: string[];
extensions: string[];
};

export interface FileAnalysis extends FileAnalysisOptions {
fileBundle: FileBundle;
analysisResults: AnalysisResult;
}
6 changes: 2 additions & 4 deletions tests/analysis.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import jsonschema from 'jsonschema';

import { analyzeFolders, extendAnalysis, FileAnalysis } from '../src/analysis';
import { analyzeFolders, extendAnalysis } from '../src/analysis';
import { uploadRemoteBundle } from '../src/bundles';
import { baseURL, sessionToken, source, TEST_TIMEOUT } from './constants/base';
import { sampleProjectPath, bundleFiles, bundleFilesFull, bundleExtender } from './constants/sample';
Expand Down Expand Up @@ -262,9 +262,7 @@ describe('Functional test of analysis', () => {
if (!sampleRes) return; // TS trick
expect(sampleRes.ruleIndex).toBeDefined();
if (!sampleRes.ruleIndex) return; // TS trick
expect(sampleRes!.ruleId).toEqual(
sarifResults.runs[0].tool.driver.rules![sampleRes!.ruleIndex!].id,
);
expect(sampleRes!.ruleId).toEqual(sarifResults.runs[0].tool.driver.rules![sampleRes!.ruleIndex!].id);

expect(extendedBundle.analysisResults.timing.analysis).toBeGreaterThanOrEqual(
extendedBundle.analysisResults.timing.fetchingCode,
Expand Down

0 comments on commit d4b04a6

Please sign in to comment.