-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
* refactor: remove unused exports/variables * chore: remove empty file --------- Co-authored-by: Bastian Doetsch <bastian.doetsch@snyk.io>
- Loading branch information
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,164 +1,3 @@ | ||
import { firstValueFrom } from 'rxjs'; | ||
import parseArgsStringToArgv from 'string-argv'; | ||
import { AnalysisStatusProvider } from '../../common/analysis/statusProvider'; | ||
import { IConfiguration } from '../../common/configuration/configuration'; | ||
import { IWorkspaceTrust } from '../../common/configuration/trustedFolders'; | ||
import { ErrorHandler } from '../../common/error/errorHandler'; | ||
import { ILanguageServer } from '../../common/languageServer/languageServer'; | ||
import { ILog } from '../../common/logger/interfaces'; | ||
import { messages as analysisMessages } from '../../common/messages/analysisMessages'; | ||
import { DownloadService } from '../../common/services/downloadService'; | ||
import { ExtensionContext } from '../../common/vscode/extensionContext'; | ||
import { IVSCodeWorkspace } from '../../common/vscode/workspace'; | ||
import { CliExecutable } from '../cliExecutable'; | ||
import { CliProcess } from '../process'; | ||
|
||
export class CliError { | ||
constructor(public error: string | Error | unknown, public path?: string, public isCancellation = false) {} | ||
Check warning on line 2 in src/snyk/cli/services/cliService.ts GitHub Actions / build / Build and Test (ubuntu-latest)
Check warning on line 2 in src/snyk/cli/services/cliService.ts GitHub Actions / build / Build and Test (windows-latest)
Check warning on line 2 in src/snyk/cli/services/cliService.ts GitHub Actions / build / Build and Test (macos-latest)
Check warning on line 2 in src/snyk/cli/services/cliService.ts GitHub Actions / build / Build and Test (ubuntu-latest)
Check warning on line 2 in src/snyk/cli/services/cliService.ts GitHub Actions / build / Build and Test (macos-latest)
|
||
} | ||
|
||
export abstract class CliService<CliResult> extends AnalysisStatusProvider { | ||
protected abstract readonly command: string[]; | ||
protected result: CliResult | CliError | undefined; | ||
|
||
private cliProcess?: CliProcess; | ||
private _isCliReady: boolean; | ||
private _isAnyWorkspaceFolderTrusted = true; | ||
|
||
constructor( | ||
protected readonly extensionContext: ExtensionContext, | ||
protected readonly logger: ILog, | ||
protected readonly config: IConfiguration, | ||
protected readonly workspace: IVSCodeWorkspace, | ||
protected readonly downloadService: DownloadService, | ||
protected readonly languageServer: ILanguageServer, | ||
protected readonly workspaceTrust: IWorkspaceTrust, | ||
) { | ||
super(); | ||
} | ||
|
||
get isCliReady(): boolean { | ||
return this._isCliReady; | ||
} | ||
|
||
get isAnyWorkspaceFolderTrusted(): boolean { | ||
return this._isAnyWorkspaceFolderTrusted; | ||
} | ||
|
||
async test(manualTrigger: boolean, reportTriggeredEvent: boolean): Promise<CliResult | CliError | void> { | ||
this.ensureDependencies(); | ||
|
||
const currentCliPath = CliExecutable.getPath(this.extensionContext.extensionPath, this.config.getCliPath()); | ||
const currentCliPathExists = await CliExecutable.exists( | ||
this.extensionContext.extensionPath, | ||
this.config.getCliPath(), | ||
); | ||
await this.synchronizeCliPathIfNeeded(currentCliPath, currentCliPathExists); | ||
if (currentCliPathExists) { | ||
const cliPath = this.config.getCliPath(); | ||
if (!cliPath) { | ||
throw new Error('CLI path is not set, probably failed migration.'); | ||
} | ||
|
||
this.logger.info(`Using CLI path ${cliPath}`); | ||
this.languageServer.cliReady$.next(cliPath); | ||
} | ||
|
||
// Prevent from CLI scan until Language Server downloads the CLI. | ||
const cliPath = await firstValueFrom(this.languageServer.cliReady$); | ||
this._isCliReady = true; | ||
|
||
const workspaceFolders = this.workspace.getWorkspaceFolders(); | ||
if (workspaceFolders.length == 0) { | ||
throw new Error('No workspace was opened.'); | ||
} | ||
|
||
const foldersToTest = this.workspaceTrust.getTrustedFolders(this.config, workspaceFolders); | ||
if (foldersToTest.length == 0) { | ||
this.handleNoTrustedFolders(); | ||
this.logger.info(`Skipping Open Source scan. ${analysisMessages.noWorkspaceTrustDescription}`); | ||
return; | ||
} | ||
this._isAnyWorkspaceFolderTrusted = true; | ||
|
||
// Start test | ||
this.analysisStarted(); | ||
this.beforeTest(manualTrigger, reportTriggeredEvent); | ||
this.result = undefined; | ||
|
||
if (this.cliProcess) { | ||
const killed = this.cliProcess.kill(); | ||
if (!killed) this.logger.error('Failed to kill an already running CLI instance.'); | ||
} | ||
|
||
this.cliProcess = new CliProcess(this.logger, this.config, this.workspace); | ||
const args = this.buildArguments(foldersToTest); | ||
|
||
let output: string; | ||
try { | ||
output = await this.cliProcess.spawn(cliPath, foldersToTest[0], args); | ||
} catch (spawnError) { | ||
if (spawnError instanceof CliError) { | ||
return spawnError; | ||
} | ||
|
||
const result = new CliError(spawnError, ''); | ||
this.finalizeTest(result); | ||
return result; | ||
} | ||
|
||
const mappedResult = this.mapToResultType(output); | ||
this.finalizeTest(mappedResult); | ||
|
||
return mappedResult; | ||
} | ||
|
||
// Synchronizes user configuration with CLI path passed to the Snyk LS. | ||
// TODO: Remove in VS Code + Language Server feature cleanup. | ||
private async synchronizeCliPathIfNeeded(cliPath: string, cliPathExists: boolean) { | ||
if (!this.config.getCliPath() && cliPathExists) { | ||
this.logger.info("Synchronising extension's CLI path with Language Server"); | ||
try { | ||
await this.config.setCliPath(cliPath); | ||
} catch (e) { | ||
ErrorHandler.handle(e, this.logger, "Failed to synchronize extension's CLI path with Language Server"); | ||
} | ||
} | ||
|
||
return cliPath; | ||
} | ||
|
||
protected abstract mapToResultType(rawCliResult: string): CliResult; | ||
|
||
protected abstract ensureDependencies(): void; | ||
|
||
protected abstract beforeTest(manualTrigger: boolean, reportTriggeredEvent: boolean): void; | ||
protected abstract afterTest(result: CliResult | CliError): void; | ||
|
||
handleNoTrustedFolders() { | ||
this._isAnyWorkspaceFolderTrusted = false; | ||
} | ||
|
||
private buildArguments(foldersToTest: ReadonlyArray<string>): string[] { | ||
const args = []; | ||
|
||
args.push(...this.command); | ||
args.push(...foldersToTest); | ||
args.push('--json'); | ||
|
||
const additionalParams = this.config.getAdditionalCliParameters(); | ||
if (additionalParams) { | ||
args.push(...parseArgsStringToArgv(additionalParams.trim())); | ||
} | ||
|
||
return args; | ||
} | ||
|
||
// To be called to finalise the analysis | ||
public finalizeTest(result: CliResult | CliError): void { | ||
this.result = result; | ||
|
||
this.analysisFinished(); | ||
this.afterTest(result); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,2 @@ | ||
export const SupportedCliPlatformsList = ['linux', 'win32', 'darwin'] as const; | ||
const SupportedCliPlatformsList = ['linux', 'win32', 'darwin'] as const; | ||
export type CliSupportedPlatform = typeof SupportedCliPlatformsList[number]; | ||
|
||
export function isPlatformSupported(platform: NodeJS.Platform): boolean { | ||
return SupportedCliPlatformsList.find(p => p === platform) !== undefined; | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,11 @@ | ||
// Changing this requires changing display name in package.json. | ||
export const SNYK_NAME = 'Snyk Security'; | ||
export const SNYK_TOKEN_KEY = 'snyk.token'; | ||
export const SNYK_UNIQUE_EXTENSION_NAME = 'Snyk Vulnerability Scanner'; | ||
const SNYK_UNIQUE_EXTENSION_NAME = 'Snyk Vulnerability Scanner'; | ||
export const SNYK_PUBLISHER = 'snyk-security'; | ||
export const SNYK_NAME_EXTENSION = SNYK_UNIQUE_EXTENSION_NAME.toLowerCase().replace(/[()]/g, '').replace(/\s/g, '-'); | ||
export const MAX_CONNECTION_RETRIES = 5; // max number of automatic retries before showing an error | ||
export const IDE_NAME = 'Visual Studio Code'; | ||
export const IDE_NAME_SHORT = 'vscode'; | ||
export const COMMAND_DEBOUNCE_INTERVAL = 200; // 200 milliseconds | ||
export const DEFAULT_SCAN_DEBOUNCE_INTERVAL = 1000; // 1 second | ||
export const DEFAULT_LS_DEBOUNCE_INTERVAL = 1000; // 1 second | ||
export const EXECUTION_THROTTLING_INTERVAL = 1000 * 10; // * 60 * 30; // 30 minutes | ||
export const EXECUTION_PAUSE_INTERVAL = 1000 * 60 * 30; // 30 minutes | ||
export const REFRESH_VIEW_DEBOUNCE_INTERVAL = 200; // 200 milliseconds | ||
// If CONNECTION_ERROR_RETRY_INTERVAL is smaller than EXECUTION_DEBOUNCE_INTERVAL it might get swallowed by the debouncer | ||
export const CONNECTION_ERROR_RETRY_INTERVAL = DEFAULT_SCAN_DEBOUNCE_INTERVAL * 2 + 1000 * 3; | ||
|
||
export const SNYK_LEARN_API_CACHE_DURATION_IN_MS = 1000 * 60 * 60 * 24; // 1 day |