Skip to content

Commit

Permalink
Merge pull request #117 from snyk/reorgenize-exports
Browse files Browse the repository at this point in the history
chore: reorganize interfaces exports
  • Loading branch information
saark-snyk authored Dec 20, 2021
2 parents 097ed94 + db9b784 commit e7f1afc
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 60 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;
}
8 changes: 3 additions & 5 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 @@ -247,7 +247,7 @@ describe('Functional test of analysis', () => {

const sarifResults = extendedBundle.analysisResults.sarif;

expect(sarifResults.runs[0].tool.driver.rules).toHaveLength(7);
expect(sarifResults.runs[0].tool.driver.rules).toHaveLength(8);
expect(sarifResults.runs[0].results).toHaveLength(15);
const getRes = (path: string) =>
sarifResults.runs[0].results!.find(
Expand All @@ -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
17 changes: 10 additions & 7 deletions tests/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import pick from 'lodash.pick';

import { baseURL, sessionToken, source, TEST_TIMEOUT } from './constants/base';
import { bundleFiles, bundleFilesFull } from './constants/sample';
import { emitter } from '../src/emitter';
import { getFilters, createBundle, checkBundle, extendBundle, getAnalysis, AnalysisStatus } from '../src/http';
import { BundleFiles } from '../src/interfaces/files.interface';

const fakeBundleHash = '3691c255a7fa00b47bb3ca96593f7108f3539cbe4bc6836e20e5c43410971979';
const fakeBundleHash = '66f93024eec238dc454fad6bb8617ce17001c27396336358f200052faff13449';
let fakeBundleHashFull = '';
const realBundleHash = '7f1817d92710efc50f3b68a5f5dcca2bfb7d0ce82db78f2ecd30fd9e9a410059';
const realBundleHash = '';
let realBundleHashFull = '';

const fakeMissingFiles = [
Expand All @@ -35,6 +34,8 @@ describe('Requests to public API', () => {
'.cs',
'.c',
'.cc',
'.cjs',
'.cls',
'.cpp',
'.cs',
'.cxx',
Expand All @@ -52,12 +53,15 @@ describe('Requests to public API', () => {
'.java',
'.js',
'.jsx',
'.kt',
'.mjs',
'.php',
'.phtml',
'.py',
'.rb',
'.rhtml',
'.slim',
'.swift',
'.ts',
'.tsx',
'.vue',
Expand All @@ -71,7 +75,6 @@ describe('Requests to public API', () => {
});

it('test network issues', async () => {

const response = await getFilters('https://faketest.snyk.io', 'test-source', 1);
expect(response.type).toEqual('error');
if (response.type !== 'error') return;
Expand Down Expand Up @@ -192,7 +195,7 @@ describe('Requests to public API', () => {
});
expect(response.type).toEqual('success');
if (response.type === 'error') return;
expect(response.value.bundleHash).toContain('587a6bcb0095606ad57ccc7bb7ac6401475ce4181c13f7136491a16df06544f1');
expect(response.value.bundleHash).toContain('1484a1a5cf09854080e7be7ed023fd085287d5cf71d046aed47d2c03de1190c6');
expect(response.value.missingFiles).toEqual([`new.js`]);
},
TEST_TIMEOUT,
Expand Down Expand Up @@ -237,7 +240,7 @@ describe('Requests to public API', () => {
});
expect(response.type).toEqual('success');
if (response.type !== 'success') return; // TS trick
expect(response.value.bundleHash).toContain('7b0c0099abe1224f0ef92f6a4a0973ec02fa8f57357ec2e7a4e852738bf75178');
expect(response.value.bundleHash).toContain('77d2fc40d9b77f3e8cfc3b5046e634e5edd958ea3a9b00fb12051fa30942d61f');
expect(response.value.missingFiles).toHaveLength(11);
},
TEST_TIMEOUT,
Expand Down Expand Up @@ -290,7 +293,7 @@ describe('Requests to public API', () => {
if (response.type === 'error') return;
expect(response.value.status !== AnalysisStatus.failed).toBeTruthy();

if (response.value.status === AnalysisStatus.complete && response.value.type === 'sarif' ) {
if (response.value.status === AnalysisStatus.complete && response.value.type === 'sarif') {
expect(response.value.sarif.runs[0].results).toHaveLength(17);

expect(new Set(response.value.coverage)).toEqual(
Expand Down

0 comments on commit e7f1afc

Please sign in to comment.