From 9df00fcde01ce0ca46e7afeff12b1a0826f3c11f Mon Sep 17 00:00:00 2001 From: Jason Luong Date: Wed, 25 Oct 2023 16:34:09 +0100 Subject: [PATCH 1/5] feat: adds new LS OssService class --- src/snyk/base/modules/baseSnykModule.ts | 2 + src/snyk/common/languageServer/settings.ts | 2 +- src/snyk/extension.ts | 47 ++-- src/snyk/snykOss/ossServiceLs.ts | 62 ++++++ src/snyk/snykOss/views/interfaces.ts | 11 + ...SuggestionWebviewProviderLanguageServer.ts | 202 ++++++++++++++++++ 6 files changed, 313 insertions(+), 13 deletions(-) create mode 100644 src/snyk/snykOss/ossServiceLs.ts create mode 100644 src/snyk/snykOss/views/interfaces.ts create mode 100644 src/snyk/snykOss/views/suggestion/ossSuggestionWebviewProviderLanguageServer.ts diff --git a/src/snyk/base/modules/baseSnykModule.ts b/src/snyk/base/modules/baseSnykModule.ts index 5a3bee102..391e865ba 100644 --- a/src/snyk/base/modules/baseSnykModule.ts +++ b/src/snyk/base/modules/baseSnykModule.ts @@ -23,6 +23,7 @@ import { IMarkdownStringAdapter, MarkdownStringAdapter } from '../../common/vsco import { IWatcher } from '../../common/watchers/interfaces'; import { ICodeSettings } from '../../snykCode/codeSettings'; import SnykEditorsWatcher from '../../snykCode/watchers/editorsWatcher'; +import { OssServiceLanguageServer } from '../../snykOss/ossServiceLs'; import { OssService } from '../../snykOss/services/ossService'; import { OssVulnerabilityCountService } from '../../snykOss/services/vulnerabilityCount/ossVulnerabilityCountService'; import { IAuthenticationService } from '../services/authenticationService'; @@ -45,6 +46,7 @@ export default abstract class BaseSnykModule implements IBaseSnykModule { protected authService: IAuthenticationService; protected downloadService: DownloadService; protected ossService?: OssService; + protected ossServiceLanguageServer?: OssServiceLanguageServer; protected advisorService?: AdvisorProvider; protected commandController: CommandController; protected scanModeService: ScanModeService; diff --git a/src/snyk/common/languageServer/settings.ts b/src/snyk/common/languageServer/settings.ts index fe92e2501..55b5f161a 100644 --- a/src/snyk/common/languageServer/settings.ts +++ b/src/snyk/common/languageServer/settings.ts @@ -44,7 +44,7 @@ export class LanguageServerSettings { return { activateSnykCodeSecurity: `${codeSecurityEnabled}`, activateSnykCodeQuality: `${codeQualityEnabled}`, - activateSnykOpenSource: 'false', + activateSnykOpenSource: 'true', // TODO: read value from config like we do for code/iac activateSnykIac: `${iacEnabled}`, enableTelemetry: `${configuration.shouldReportEvents}`, sendErrorReports: `${configuration.shouldReportErrors}`, diff --git a/src/snyk/extension.ts b/src/snyk/extension.ts index a3688f750..34d0d94ba 100644 --- a/src/snyk/extension.ts +++ b/src/snyk/extension.ts @@ -20,13 +20,10 @@ import { SNYK_INITIATE_LOGIN_COMMAND, SNYK_OPEN_BROWSER_COMMAND, SNYK_OPEN_ISSUE_COMMAND, - SNYK_OPEN_LOCAL_COMMAND, - SNYK_SET_TOKEN_COMMAND, - SNYK_SETTINGS_COMMAND, - SNYK_SHOW_LS_OUTPUT_COMMAND, + SNYK_OPEN_LOCAL_COMMAND, SNYK_SETTINGS_COMMAND, SNYK_SET_TOKEN_COMMAND, SNYK_SHOW_LS_OUTPUT_COMMAND, SNYK_SHOW_OUTPUT_COMMAND, SNYK_START_COMMAND, - SNYK_WORKSPACE_SCAN_COMMAND, + SNYK_WORKSPACE_SCAN_COMMAND } from './common/constants/commands'; import { MEMENTO_FIRST_INSTALL_DATE_KEY } from './common/constants/globalState'; import { @@ -37,7 +34,7 @@ import { SNYK_VIEW_ANALYSIS_IAC, SNYK_VIEW_ANALYSIS_OSS, SNYK_VIEW_SUPPORT, - SNYK_VIEW_WELCOME, + SNYK_VIEW_WELCOME } from './common/constants/views'; import { ErrorHandler } from './common/error/errorHandler'; import { ErrorReporter } from './common/error/errorReporter'; @@ -74,11 +71,13 @@ import { IacService } from './snykIac/iacService'; import IacIssueTreeProvider from './snykIac/views/iacIssueTreeProvider'; import { IacSuggestionWebviewProvider } from './snykIac/views/suggestion/iacSuggestionWebviewProvider'; import { EditorDecorator } from './snykOss/editor/editorDecorator'; +import { OssServiceLanguageServer } from './snykOss/ossServiceLs'; import { OssService } from './snykOss/services/ossService'; import { OssVulnerabilityCountService } from './snykOss/services/vulnerabilityCount/ossVulnerabilityCountService'; import { ModuleVulnerabilityCountProvider } from './snykOss/services/vulnerabilityCount/vulnerabilityCountProvider'; import { OssVulnerabilityTreeProvider } from './snykOss/views/ossVulnerabilityTreeProvider'; import { OssSuggestionWebviewProvider } from './snykOss/views/suggestion/ossSuggestionWebviewProvider'; +import { OssSuggestionWebviewProviderLanguageServer } from './snykOss/views/suggestion/ossSuggestionWebviewProviderLanguageServer'; import { DailyScanJob } from './snykOss/watchers/dailyScanJob'; class SnykExtension extends SnykLib implements IExtension { @@ -223,6 +222,30 @@ class SnykExtension extends SnykLib implements IExtension { this.workspaceTrust, ); + + const ossSuggestionProvider = new OssSuggestionWebviewProviderLanguageServer( + vsCodeWindow, + extensionContext, + Logger, + vsCodeLanguages, + vsCodeWorkspace, + ) + + this.ossServiceLanguageServer = new OssServiceLanguageServer( + extensionContext, + configuration, + ossSuggestionProvider, + new CodeActionAdapter(), + this.codeActionKindAdapter, + this.viewManagerService, + vsCodeWorkspace, + this.workspaceTrust, + this.languageServer, + vsCodeLanguages, + Logger, + this.analytics, + ) + const iacSuggestionProvider = new IacSuggestionWebviewProvider( vsCodeWindow, extensionContext, @@ -263,12 +286,12 @@ class SnykExtension extends SnykLib implements IExtension { this.registerCommands(vscodeContext); const codeSecurityIssueProvider = new CodeSecurityIssueTreeProvider( - this.viewManagerService, - this.contextService, - this.snykCode, - configuration, - vsCodeLanguages, - ), + this.viewManagerService, + this.contextService, + this.snykCode, + configuration, + vsCodeLanguages, + ), codeQualityIssueProvider = new CodeQualityIssueTreeProvider( this.viewManagerService, this.contextService, diff --git a/src/snyk/snykOss/ossServiceLs.ts b/src/snyk/snykOss/ossServiceLs.ts new file mode 100644 index 000000000..820a7974d --- /dev/null +++ b/src/snyk/snykOss/ossServiceLs.ts @@ -0,0 +1,62 @@ +import { Subscription } from "rxjs"; +import { IAnalytics } from "../common/analytics/itly"; +import { IConfiguration } from "../common/configuration/configuration"; +import { IWorkspaceTrust } from "../common/configuration/trustedFolders"; +import { ILanguageServer } from "../common/languageServer/languageServer"; +import { OssIssueData, Scan, ScanProduct } from "../common/languageServer/types"; +import { ILog } from "../common/logger/interfaces"; +import { ProductService } from "../common/services/productService"; +import { IViewManagerService } from "../common/services/viewManagerService"; +import { ICodeActionAdapter, ICodeActionKindAdapter } from "../common/vscode/codeAction"; +import { ExtensionContext } from "../common/vscode/extensionContext"; +import { IVSCodeLanguages } from "../common/vscode/languages"; +import { IVSCodeWorkspace } from "../common/vscode/workspace"; +import { IOssSuggestionWebviewProvider } from "./views/interfaces"; + +export class OssServiceLanguageServer extends ProductService { + constructor( + extensionContext: ExtensionContext, + config: IConfiguration, + suggestionProvider: IOssSuggestionWebviewProvider, + readonly codeActionAdapter: ICodeActionAdapter, + readonly codeActionKindAdapter: ICodeActionKindAdapter, + viewManagerService: IViewManagerService, + workspace: IVSCodeWorkspace, + workspaceTrust: IWorkspaceTrust, + languageServer: ILanguageServer, + languages: IVSCodeLanguages, + logger: ILog, + readonly analytics: IAnalytics, + ) { + super( + extensionContext, + config, + suggestionProvider, + viewManagerService, + workspace, + workspaceTrust, + languageServer, + languages, + logger, + ); + + // this.registerCodeActionsProvider( + // new OssCodeActionsProvider(this.result, codeActionAdapter, codeActionKindAdapter, languages, analytics), + // ); + } + + subscribeToLsScanMessages(): Subscription { + return this.languageServer.scan$.subscribe((scan: Scan) => { + if (scan.product !== ScanProduct.OpenSource) { + return; + } + + console.log("received OSS scan message", scan); // TODO: delete + super.handleLsScanMessage(scan); + }); + } + + refreshTreeView() { + this.viewManagerService.refreshOssView(); + } +} diff --git a/src/snyk/snykOss/views/interfaces.ts b/src/snyk/snykOss/views/interfaces.ts new file mode 100644 index 000000000..21eb873a9 --- /dev/null +++ b/src/snyk/snykOss/views/interfaces.ts @@ -0,0 +1,11 @@ +import { Issue, OssIssueData } from '../../common/languageServer/types'; +import { IWebViewProvider } from '../../common/views/webviewProvider'; + +export interface IOssSuggestionWebviewProvider extends IWebViewProvider> { + openIssueId: string | undefined; +} + +export type OssIssueCommandArgLanguageServer = OssIssueData & { + matchingIdVulnerabilities: OssIssueData[]; + overviewHtml: string; +}; diff --git a/src/snyk/snykOss/views/suggestion/ossSuggestionWebviewProviderLanguageServer.ts b/src/snyk/snykOss/views/suggestion/ossSuggestionWebviewProviderLanguageServer.ts new file mode 100644 index 000000000..415859bea --- /dev/null +++ b/src/snyk/snykOss/views/suggestion/ossSuggestionWebviewProviderLanguageServer.ts @@ -0,0 +1,202 @@ +import * as vscode from 'vscode'; +import { SNYK_OPEN_BROWSER_COMMAND } from '../../../common/constants/commands'; +import { SNYK_VIEW_SUGGESTION_IAC, SNYK_VIEW_SUGGESTION_OSS } from '../../../common/constants/views'; +import { ErrorHandler } from '../../../common/error/errorHandler'; +import { IacIssueData, Issue, OssIssueData } from '../../../common/languageServer/types'; +import { ILog } from '../../../common/logger/interfaces'; +import { getNonce } from '../../../common/views/nonce'; +import { WebviewPanelSerializer } from '../../../common/views/webviewPanelSerializer'; +import { IWebViewProvider, WebviewProvider } from '../../../common/views/webviewProvider'; +import { ExtensionContext } from '../../../common/vscode/extensionContext'; +import { IVSCodeLanguages } from '../../../common/vscode/languages'; +import { IVSCodeWindow } from '../../../common/vscode/window'; +import { IVSCodeWorkspace } from '../../../common/vscode/workspace'; +import { messages as errorMessages } from '../../messages/error'; +// import { getAbsoluteMarkerFilePath } from '../../utils/analysisUtils'; +// import { IssueUtils } from '../../utils/issueUtils'; +// import { ICodeSuggestionWebviewProvider } from '../interfaces'; + +export class OssSuggestionWebviewProviderLanguageServer + extends WebviewProvider> + implements IWebViewProvider> +{ + // For consistency reasons, the single source of truth for the current suggestion is the + // panel state. The following field is only used in + private issue: Issue | undefined; + + constructor( + private readonly window: IVSCodeWindow, + protected readonly context: ExtensionContext, + protected readonly logger: ILog, + private readonly languages: IVSCodeLanguages, + private readonly workspace: IVSCodeWorkspace, + ) { + super(context, logger); + } + + activate(): void { + this.context.addDisposables( + this.window.registerWebviewPanelSerializer(SNYK_VIEW_SUGGESTION_OSS, new WebviewPanelSerializer(this)), + ); + } + + get openIssueId(): string | undefined { + return this.issue?.id; + } + + async showPanel(issue: Issue): Promise { + try { + await this.focusSecondEditorGroup(); + + if (this.panel) { + this.panel.reveal(vscode.ViewColumn.Two, true); + } else { + this.panel = vscode.window.createWebviewPanel( + SNYK_VIEW_SUGGESTION_OSS, + 'Snyk OSS Vulnerability', + { + viewColumn: vscode.ViewColumn.Two, + preserveFocus: true, + }, + this.getWebviewOptions(), + ); + this.registerListeners(); + } + + this.panel.webview.html = this.getHtmlForWebview(this.panel.webview); + this.panel.iconPath = vscode.Uri.joinPath( + vscode.Uri.file(this.context.extensionPath), + 'media', + 'images', + 'snyk-iac.svg', // TODO: USE OSS ICON + ); + + await this.panel.webview.postMessage({ type: 'set', args: issue }); + + this.issue = issue; + } catch (e) { + ErrorHandler.handle(e, this.logger, errorMessages.suggestionViewShowFailed); + } + } + + protected registerListeners(): void { + if (!this.panel) return; + + this.panel.onDidDispose(() => this.onPanelDispose(), null, this.disposables); + this.panel.onDidChangeViewState(() => this.checkVisibility(), undefined, this.disposables); + // Handle messages from the webview + this.panel.webview.onDidReceiveMessage(msg => this.handleMessage(msg), undefined, this.disposables); + } + + disposePanel(): void { + super.disposePanel(); + } + + protected onPanelDispose(): void { + super.onPanelDispose(); + } + + private async handleMessage(message: any) { + try { + const { type, value } = message; + switch (type) { + case 'openBrowser': { + await vscode.commands.executeCommand(SNYK_OPEN_BROWSER_COMMAND, value); + break; + } + default: { + throw new Error('Unknown message type'); + } + } + } catch (e) { + ErrorHandler.handle(e, this.logger, errorMessages.suggestionViewShowFailed); + } + } + + protected getHtmlForWebview(webview: vscode.Webview): string { + const images: Record = [ + ['dark-critical-severity', 'svg'], + ['dark-high-severity', 'svg'], + ['dark-medium-severity', 'svg'], + ['dark-low-severity', 'svg'], + ].reduce((accumulator: Record, [name, ext]) => { + const uri = this.getWebViewUri('media', 'images', `${name}.${ext}`); + if (!uri) throw new Error('Image missing.'); + accumulator[name] = uri.toString(); + return accumulator; + }, {}); + + const scriptUri = this.getWebViewUri( + 'out', + 'snyk', + 'snykIac', + 'views', + 'suggestion', + 'iacSuggestionWebviewScript.js', + ); + const styleUri = this.getWebViewUri('media', 'views', 'oss', 'suggestion', 'suggestion.css'); // make it common + + const nonce = getNonce(); + + return ` + + + + + + + + + + + +
+
+
+ + + + + + + + + +
+
+
+
+
+
+
Description
+
+
+
+
Impact
+
+
+
+
Path
+
+ +
+
+
+
+
+

Remediation

+
+
+ + + + + `; + } +} From 29706d7fbd3f05f1511b5f7fcd11d022a1abd58d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Scha=CC=88fer?= <101886095+PeterSchafer@users.noreply.github.com> Date: Thu, 26 Oct 2023 10:18:06 +0200 Subject: [PATCH 2/5] chore: read OSS settings for ls initialization options --- src/snyk/common/languageServer/settings.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/snyk/common/languageServer/settings.ts b/src/snyk/common/languageServer/settings.ts index 55b5f161a..96228872d 100644 --- a/src/snyk/common/languageServer/settings.ts +++ b/src/snyk/common/languageServer/settings.ts @@ -33,6 +33,8 @@ export class LanguageServerSettings { static async fromConfiguration(configuration: IConfiguration): Promise { const featuresConfiguration = configuration.getFeaturesConfiguration(); + const ossEnabled = _.isUndefined(featuresConfiguration.ossEnabled) ? true : featuresConfiguration.ossEnabled; + const iacEnabled = _.isUndefined(featuresConfiguration.iacEnabled) ? true : featuresConfiguration.iacEnabled; const codeSecurityEnabled = _.isUndefined(featuresConfiguration.codeSecurityEnabled) ? true @@ -44,7 +46,7 @@ export class LanguageServerSettings { return { activateSnykCodeSecurity: `${codeSecurityEnabled}`, activateSnykCodeQuality: `${codeQualityEnabled}`, - activateSnykOpenSource: 'true', // TODO: read value from config like we do for code/iac + activateSnykOpenSource: `${ossEnabled}`, activateSnykIac: `${iacEnabled}`, enableTelemetry: `${configuration.shouldReportEvents}`, sendErrorReports: `${configuration.shouldReportErrors}`, From ace740a8e084b16493daf952b09ecf734cef351c Mon Sep 17 00:00:00 2001 From: Jason Luong Date: Thu, 26 Oct 2023 11:06:52 +0100 Subject: [PATCH 3/5] refactor: rename ossServiceLs file` --- src/snyk/base/modules/baseSnykModule.ts | 2 +- src/snyk/extension.ts | 2 +- .../snykOss/{ossServiceLs.ts => ossServiceLanguageServer.ts} | 0 src/snyk/snykOss/watchers/dailyScanJob.ts | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename src/snyk/snykOss/{ossServiceLs.ts => ossServiceLanguageServer.ts} (100%) diff --git a/src/snyk/base/modules/baseSnykModule.ts b/src/snyk/base/modules/baseSnykModule.ts index 391e865ba..4dc05fdbd 100644 --- a/src/snyk/base/modules/baseSnykModule.ts +++ b/src/snyk/base/modules/baseSnykModule.ts @@ -23,7 +23,7 @@ import { IMarkdownStringAdapter, MarkdownStringAdapter } from '../../common/vsco import { IWatcher } from '../../common/watchers/interfaces'; import { ICodeSettings } from '../../snykCode/codeSettings'; import SnykEditorsWatcher from '../../snykCode/watchers/editorsWatcher'; -import { OssServiceLanguageServer } from '../../snykOss/ossServiceLs'; +import { OssServiceLanguageServer } from '../../snykOss/ossServiceLanguageServer'; import { OssService } from '../../snykOss/services/ossService'; import { OssVulnerabilityCountService } from '../../snykOss/services/vulnerabilityCount/ossVulnerabilityCountService'; import { IAuthenticationService } from '../services/authenticationService'; diff --git a/src/snyk/extension.ts b/src/snyk/extension.ts index 34d0d94ba..c40d8ecfb 100644 --- a/src/snyk/extension.ts +++ b/src/snyk/extension.ts @@ -71,7 +71,7 @@ import { IacService } from './snykIac/iacService'; import IacIssueTreeProvider from './snykIac/views/iacIssueTreeProvider'; import { IacSuggestionWebviewProvider } from './snykIac/views/suggestion/iacSuggestionWebviewProvider'; import { EditorDecorator } from './snykOss/editor/editorDecorator'; -import { OssServiceLanguageServer } from './snykOss/ossServiceLs'; +import { OssServiceLanguageServer } from './snykOss/ossServiceLanguageServer'; import { OssService } from './snykOss/services/ossService'; import { OssVulnerabilityCountService } from './snykOss/services/vulnerabilityCount/ossVulnerabilityCountService'; import { ModuleVulnerabilityCountProvider } from './snykOss/services/vulnerabilityCount/vulnerabilityCountProvider'; diff --git a/src/snyk/snykOss/ossServiceLs.ts b/src/snyk/snykOss/ossServiceLanguageServer.ts similarity index 100% rename from src/snyk/snykOss/ossServiceLs.ts rename to src/snyk/snykOss/ossServiceLanguageServer.ts diff --git a/src/snyk/snykOss/watchers/dailyScanJob.ts b/src/snyk/snykOss/watchers/dailyScanJob.ts index 6b21793b8..7931c7765 100644 --- a/src/snyk/snykOss/watchers/dailyScanJob.ts +++ b/src/snyk/snykOss/watchers/dailyScanJob.ts @@ -4,7 +4,7 @@ export class DailyScanJob { private readonly dayInMs = 86400000; private job: NodeJS.Timeout; - constructor(private readonly extension: IExtension) {} + constructor(private readonly extension: IExtension) { } schedule(): void { if (this.job) { From 9dd04f8b18cc7d2c536ad4c4a88cc7b21e861420 Mon Sep 17 00:00:00 2001 From: Jason Luong Date: Thu, 26 Oct 2023 11:15:31 +0100 Subject: [PATCH 4/5] fix: linting --- src/snyk/extension.ts | 26 +++++++++-------- src/snyk/snykOss/ossServiceLanguageServer.ts | 30 ++++++++++---------- src/snyk/snykOss/watchers/dailyScanJob.ts | 2 +- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/snyk/extension.ts b/src/snyk/extension.ts index c40d8ecfb..60b551275 100644 --- a/src/snyk/extension.ts +++ b/src/snyk/extension.ts @@ -20,10 +20,13 @@ import { SNYK_INITIATE_LOGIN_COMMAND, SNYK_OPEN_BROWSER_COMMAND, SNYK_OPEN_ISSUE_COMMAND, - SNYK_OPEN_LOCAL_COMMAND, SNYK_SETTINGS_COMMAND, SNYK_SET_TOKEN_COMMAND, SNYK_SHOW_LS_OUTPUT_COMMAND, + SNYK_OPEN_LOCAL_COMMAND, + SNYK_SETTINGS_COMMAND, + SNYK_SET_TOKEN_COMMAND, + SNYK_SHOW_LS_OUTPUT_COMMAND, SNYK_SHOW_OUTPUT_COMMAND, SNYK_START_COMMAND, - SNYK_WORKSPACE_SCAN_COMMAND + SNYK_WORKSPACE_SCAN_COMMAND, } from './common/constants/commands'; import { MEMENTO_FIRST_INSTALL_DATE_KEY } from './common/constants/globalState'; import { @@ -34,7 +37,7 @@ import { SNYK_VIEW_ANALYSIS_IAC, SNYK_VIEW_ANALYSIS_OSS, SNYK_VIEW_SUPPORT, - SNYK_VIEW_WELCOME + SNYK_VIEW_WELCOME, } from './common/constants/views'; import { ErrorHandler } from './common/error/errorHandler'; import { ErrorReporter } from './common/error/errorReporter'; @@ -222,14 +225,13 @@ class SnykExtension extends SnykLib implements IExtension { this.workspaceTrust, ); - const ossSuggestionProvider = new OssSuggestionWebviewProviderLanguageServer( vsCodeWindow, extensionContext, Logger, vsCodeLanguages, vsCodeWorkspace, - ) + ); this.ossServiceLanguageServer = new OssServiceLanguageServer( extensionContext, @@ -244,7 +246,7 @@ class SnykExtension extends SnykLib implements IExtension { vsCodeLanguages, Logger, this.analytics, - ) + ); const iacSuggestionProvider = new IacSuggestionWebviewProvider( vsCodeWindow, @@ -286,12 +288,12 @@ class SnykExtension extends SnykLib implements IExtension { this.registerCommands(vscodeContext); const codeSecurityIssueProvider = new CodeSecurityIssueTreeProvider( - this.viewManagerService, - this.contextService, - this.snykCode, - configuration, - vsCodeLanguages, - ), + this.viewManagerService, + this.contextService, + this.snykCode, + configuration, + vsCodeLanguages, + ), codeQualityIssueProvider = new CodeQualityIssueTreeProvider( this.viewManagerService, this.contextService, diff --git a/src/snyk/snykOss/ossServiceLanguageServer.ts b/src/snyk/snykOss/ossServiceLanguageServer.ts index 820a7974d..0260baca6 100644 --- a/src/snyk/snykOss/ossServiceLanguageServer.ts +++ b/src/snyk/snykOss/ossServiceLanguageServer.ts @@ -1,17 +1,17 @@ -import { Subscription } from "rxjs"; -import { IAnalytics } from "../common/analytics/itly"; -import { IConfiguration } from "../common/configuration/configuration"; -import { IWorkspaceTrust } from "../common/configuration/trustedFolders"; -import { ILanguageServer } from "../common/languageServer/languageServer"; -import { OssIssueData, Scan, ScanProduct } from "../common/languageServer/types"; -import { ILog } from "../common/logger/interfaces"; -import { ProductService } from "../common/services/productService"; -import { IViewManagerService } from "../common/services/viewManagerService"; -import { ICodeActionAdapter, ICodeActionKindAdapter } from "../common/vscode/codeAction"; -import { ExtensionContext } from "../common/vscode/extensionContext"; -import { IVSCodeLanguages } from "../common/vscode/languages"; -import { IVSCodeWorkspace } from "../common/vscode/workspace"; -import { IOssSuggestionWebviewProvider } from "./views/interfaces"; +import { Subscription } from 'rxjs'; +import { IAnalytics } from '../common/analytics/itly'; +import { IConfiguration } from '../common/configuration/configuration'; +import { IWorkspaceTrust } from '../common/configuration/trustedFolders'; +import { ILanguageServer } from '../common/languageServer/languageServer'; +import { OssIssueData, Scan, ScanProduct } from '../common/languageServer/types'; +import { ILog } from '../common/logger/interfaces'; +import { ProductService } from '../common/services/productService'; +import { IViewManagerService } from '../common/services/viewManagerService'; +import { ICodeActionAdapter, ICodeActionKindAdapter } from '../common/vscode/codeAction'; +import { ExtensionContext } from '../common/vscode/extensionContext'; +import { IVSCodeLanguages } from '../common/vscode/languages'; +import { IVSCodeWorkspace } from '../common/vscode/workspace'; +import { IOssSuggestionWebviewProvider } from './views/interfaces'; export class OssServiceLanguageServer extends ProductService { constructor( @@ -51,7 +51,7 @@ export class OssServiceLanguageServer extends ProductService { return; } - console.log("received OSS scan message", scan); // TODO: delete + console.log('received OSS scan message', scan); // TODO: delete super.handleLsScanMessage(scan); }); } diff --git a/src/snyk/snykOss/watchers/dailyScanJob.ts b/src/snyk/snykOss/watchers/dailyScanJob.ts index 7931c7765..6b21793b8 100644 --- a/src/snyk/snykOss/watchers/dailyScanJob.ts +++ b/src/snyk/snykOss/watchers/dailyScanJob.ts @@ -4,7 +4,7 @@ export class DailyScanJob { private readonly dayInMs = 86400000; private job: NodeJS.Timeout; - constructor(private readonly extension: IExtension) { } + constructor(private readonly extension: IExtension) {} schedule(): void { if (this.job) { From 0b801e90287ccb026f59bda43e210451ecad45ec Mon Sep 17 00:00:00 2001 From: Jason Luong Date: Thu, 26 Oct 2023 11:43:37 +0100 Subject: [PATCH 5/5] chore: adds unit tests for new ossService --- src/snyk/snykOss/ossServiceLanguageServer.ts | 1 - .../snykOss/ossServiceLanguageServer.test.ts | 81 +++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/test/unit/snykOss/ossServiceLanguageServer.test.ts diff --git a/src/snyk/snykOss/ossServiceLanguageServer.ts b/src/snyk/snykOss/ossServiceLanguageServer.ts index 0260baca6..286297250 100644 --- a/src/snyk/snykOss/ossServiceLanguageServer.ts +++ b/src/snyk/snykOss/ossServiceLanguageServer.ts @@ -51,7 +51,6 @@ export class OssServiceLanguageServer extends ProductService { return; } - console.log('received OSS scan message', scan); // TODO: delete super.handleLsScanMessage(scan); }); } diff --git a/src/test/unit/snykOss/ossServiceLanguageServer.test.ts b/src/test/unit/snykOss/ossServiceLanguageServer.test.ts new file mode 100644 index 000000000..0b09e21b0 --- /dev/null +++ b/src/test/unit/snykOss/ossServiceLanguageServer.test.ts @@ -0,0 +1,81 @@ +import { strictEqual } from 'assert'; +import sinon from 'sinon'; +import { IAnalytics } from '../../../snyk/common/analytics/itly'; +import { IConfiguration } from '../../../snyk/common/configuration/configuration'; +import { WorkspaceTrust } from '../../../snyk/common/configuration/trustedFolders'; +import { ILanguageServer } from '../../../snyk/common/languageServer/languageServer'; +import { OssIssueData, ScanProduct, ScanStatus } from '../../../snyk/common/languageServer/types'; +import { IProductService } from '../../../snyk/common/services/productService'; +import { IViewManagerService } from '../../../snyk/common/services/viewManagerService'; +import { ICodeActionAdapter, ICodeActionKindAdapter } from '../../../snyk/common/vscode/codeAction'; +import { ExtensionContext } from '../../../snyk/common/vscode/extensionContext'; +import { IVSCodeLanguages } from '../../../snyk/common/vscode/languages'; +import { IVSCodeWorkspace } from '../../../snyk/common/vscode/workspace'; +import { OssServiceLanguageServer } from '../../../snyk/snykOss/ossServiceLanguageServer'; +import { OssSuggestionWebviewProviderLanguageServer } from '../../../snyk/snykOss/views/suggestion/ossSuggestionWebviewProviderLanguageServer'; +import { LanguageServerMock } from '../mocks/languageServer.mock'; +import { LoggerMock } from '../mocks/logger.mock'; + +suite('OSS Service', () => { + let ls: ILanguageServer; + let service: IProductService; + let refreshViewFake: sinon.SinonSpy; + + setup(() => { + ls = new LanguageServerMock(); + refreshViewFake = sinon.fake(); + + const viewManagerService = { + refreshOssView: refreshViewFake, + } as unknown as IViewManagerService; + + service = new OssServiceLanguageServer( + {} as ExtensionContext, + {} as IConfiguration, + {} as OssSuggestionWebviewProviderLanguageServer, + {} as ICodeActionAdapter, + { + getQuickFix: sinon.fake(), + } as ICodeActionKindAdapter, + viewManagerService, + { + getWorkspaceFolders: () => [''], + } as IVSCodeWorkspace, + new WorkspaceTrust(), + ls, + { + registerCodeActionsProvider: sinon.fake(), + } as unknown as IVSCodeLanguages, + new LoggerMock(), + {} as IAnalytics, + ); + }); + + teardown(() => { + sinon.restore(); + }); + + test('Scan returned for OSS product', () => { + ls.scan$.next({ + product: ScanProduct.OpenSource, + folderPath: 'test/path', + issues: [], + status: ScanStatus.InProgress, + }); + + strictEqual(service.isAnalysisRunning, true); + sinon.assert.calledOnce(refreshViewFake); + }); + + test('Scan not returned for non-OSS product', () => { + ls.scan$.next({ + product: ScanProduct.Code, + folderPath: 'test/path', + issues: [], + status: ScanStatus.InProgress, + }); + + strictEqual(service.isAnalysisRunning, false); + sinon.assert.notCalled(refreshViewFake); + }); +});