Skip to content

Commit

Permalink
fix: view management, don't show trust & connect when not needed (#377)
Browse files Browse the repository at this point in the history
* fix: view management - don't show trust & connect without reason

Signed-off-by: Bastian Doetsch <[email protected]>

* docs: update CHANGELOG.md

Signed-off-by: Bastian Doetsch <[email protected]>

* chore: remove unnecessary suppression comment

Signed-off-by: Bastian Doetsch <[email protected]>

* refactor: rename `notLoading` to `initialized`

Signed-off-by: Bastian Doetsch <[email protected]>

---------

Signed-off-by: Bastian Doetsch <[email protected]>
  • Loading branch information
bastiandoetsch authored Sep 29, 2023
1 parent aad80bb commit f158194
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 213 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Snyk Security - Code and Open Source Dependencies Changelog

## [1.23.2]

### Fixed

- Removed false positives feature flag
- View management: show accurate information during startup of the plugin

## [1.23.1]

Expand Down
33 changes: 16 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -230,41 +230,35 @@
},
"views": {
"snyk": [
{
"id": "snyk.views.features",
"name": "Snyk",
"when": "snyk:loggedIn && !snyk:featuresSelected && !snyk:error",
"type": "webview"
},
{
"id": "snyk.views.welcome",
"name": "Snyk",
"when": "!snyk:loggedIn || snyk:error || snyk:featuresSelected && !snyk:workspaceFound"
"when": "!snyk:loggedIn || snyk:error || !snyk:workspaceFound"
},
{
"id": "snyk.views.analysis.oss",
"name": "Open Source Security",
"when": "snyk:loggedIn && snyk:featuresSelected && snyk:workspaceFound && !snyk:error"
"when": "snyk:initialized && snyk:loggedIn && snyk:workspaceFound && !snyk:error"
},
{
"id": "snyk.views.analysis.code.security",
"name": "Code Security",
"when": "snyk:lsCodePreview && snyk:loggedIn && snyk:codeEnabled && snyk:featuresSelected && snyk:workspaceFound && !snyk:error"
"when": "snyk:initialized && snyk:loggedIn && snyk:codeEnabled && snyk:workspaceFound && !snyk:error"
},
{
"id": "snyk.views.analysis.configuration",
"name": "Configuration",
"when": "snyk:loggedIn && snyk:featuresSelected && snyk:workspaceFound && !snyk:error"
"name": "Configuration Issues",
"when": "snyk:initialized && snyk:loggedIn && snyk:workspaceFound && !snyk:error"
},
{
"id": "snyk.views.analysis.code.quality",
"name": "Code Quality",
"when": "snyk:lsCodePreview && snyk:loggedIn && snyk:codeEnabled && snyk:featuresSelected && snyk:workspaceFound && !snyk:error"
"when": "snyk:initialized && snyk:loggedIn && snyk:codeEnabled && snyk:workspaceFound && !snyk:error"
},
{
"id": "snyk.views.analysis.code.enablement",
"name": "Code Security & Quality",
"when": "snyk:loggedIn && snyk:featuresSelected && !snyk:codeEnabled && snyk:workspaceFound && !snyk:error"
"when": "snyk:initialized && snyk:loggedIn && !snyk:codeEnabled && snyk:workspaceFound && !snyk:error"
},
{
"id": "snyk.views.support",
Expand All @@ -278,15 +272,20 @@
"contents": "Snyk has encountered a problem. Please restart the extension: \n[Restart](command:snyk.start 'Restart Snyk')\nIf the error persists, please check your [settings](command:snyk.settings) and [contact us](https://snyk.io/contact-us/?utm_source=vsc)!",
"when": "snyk:error == 'blocking'"
},
{
"view": "snyk.views.welcome",
"contents": "Welcome to Snyk for Visual Studio Code. 👋\n👉 Please wait, the extension is loading...",
"when": "!snyk:error && !snyk:initialized"
},
{
"view": "snyk.views.welcome",
"contents": "Welcome to Snyk for Visual Studio Code. 👋\n👉 Connect with Snyk to start your first analysis!\nWhen scanning folder files, Snyk may automatically execute code such as invoking the package manager to get dependency information. You should only scan projects you trust. [More info](https://docs.snyk.io/ide-tools/visual-studio-code-extension/workspace-trust)\n[Trust workspace and connect](command:snyk.initiateLogin 'Connect with Snyk')\nBy connecting your account with Snyk, you agree to the Snyk [Privacy Policy](https://snyk.io/policies/privacy), and the Snyk [Terms of Service](https://snyk.io/policies/terms-of-service).",
"when": "!snyk:error && !snyk:loggedIn"
"when": "!snyk:error && snyk:initialized && !snyk:loggedIn"
},
{
"view": "snyk.views.welcome",
"contents": "We are now redirecting you to our auth page, go ahead and log in. If a browser window doesn't open after a few seconds, please copy the url below and manually paste it in a browser.\n[Copy URL to clipboard](command:snyk.copyAuthLink 'Copy URL to clipboard')",
"when": "!snyk:error && !snyk:loggedIn && snyk:authenticating"
"when": "!snyk:error && snyk:initialized && !snyk:loggedIn && snyk:authenticating"
},
{
"view": "snyk.views.analysis.code.enablement",
Expand All @@ -295,7 +294,7 @@
{
"view": "snyk.views.welcome",
"contents": "Open a workspace or a folder in Visual Studio Code to start the analysis.",
"when": "!snyk:error && snyk:loggedIn && snyk:featuresSelected && !snyk:workspaceFound"
"when": "!snyk:error && snyk:initialized && snyk:loggedIn && !snyk:workspaceFound"
}
],
"menus": {
Expand Down Expand Up @@ -453,6 +452,6 @@
"uuid": "^8.3.2",
"validate-npm-package-name": "^3.0.0",
"vscode-languageclient": "8.1.0",
"vscode-languageserver-textdocument":"^1.0.8"
"vscode-languageserver-textdocument": "^1.0.8"
}
}
9 changes: 2 additions & 7 deletions src/snyk/base/modules/snykLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,12 @@ export default class SnykLib extends BaseSnykModule implements ISnykLib {
await this.contextService.setContext(SNYK_CONTEXT.LOGGEDIN, true);

if (!configuration.getFeaturesConfiguration()) {
await this.contextService.setContext(SNYK_CONTEXT.FEATURES_SELECTED, false);
return;
}

await this.contextService.setContext(SNYK_CONTEXT.FEATURES_SELECTED, true);

const workspacePaths = vsCodeWorkspace.getWorkspaceFolders();
await this.setWorkspaceContext(workspacePaths);

await this.user.identify(vsCodeCommands, this.analytics);

const workspacePaths = vsCodeWorkspace.getWorkspaceFolders();
if (workspacePaths.length) {
this.logFullAnalysisIsTriggered(manual);
void this.startOssAnalysis(manual, false);
Expand Down Expand Up @@ -80,7 +75,7 @@ export default class SnykLib extends BaseSnykModule implements ISnykLib {
await this.contextService.setContext(SNYK_CONTEXT.ADVANCED, configuration.shouldShowAdvancedView);
}

private async setWorkspaceContext(workspacePaths: string[]): Promise<void> {
protected async setWorkspaceContext(workspacePaths: string[]): Promise<void> {
const workspaceFound = !!workspacePaths.length;
await this.contextService.setContext(SNYK_CONTEXT.WORKSPACE_FOUND, workspaceFound);
}
Expand Down
103 changes: 0 additions & 103 deletions src/snyk/base/views/featureSelection/featuresViewProvider.ts

This file was deleted.

27 changes: 0 additions & 27 deletions src/snyk/base/views/featureSelection/featuresViewScript.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/snyk/common/constants/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ export const SNYK_VIEW_ANALYSIS_IAC = 'snyk.views.analysis.configuration';
// Having multiple boolean contexts instead of a single context
// with multiple values helps us to avoid flickering UI.
export const SNYK_CONTEXT = {
INITIALIZED: 'initialized', // default to loading state (notLoading = false when boolean is initialized)
LOGGEDIN: 'loggedIn',
AUTHENTICATING: 'authenticating',
FEATURES_SELECTED: 'featuresSelected',
CODE_ENABLED: 'codeEnabled',
CODE_LOCAL_ENGINE_ENABLED: 'codeLocalEngineEnabled',
LS_CODE_PREVIEW: 'lsCodePreview',
WORKSPACE_FOUND: 'workspaceFound',
ERROR: 'error',
MODE: 'mode',
Expand Down
5 changes: 0 additions & 5 deletions src/snyk/common/services/contextService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export interface IContextService {
shouldShowCodeAnalysis: boolean;
shouldShowOssAnalysis: boolean;
shouldShowIacAnalysis: boolean;
isCodeInLsPreview: boolean;

setContext(key: string, value: unknown): Promise<void>;
}
Expand All @@ -25,10 +24,6 @@ export class ContextService implements IContextService {
await setContext(key, value);
}

get isCodeInLsPreview(): boolean {
return !!this.viewContext[SNYK_CONTEXT.LS_CODE_PREVIEW];
}

get shouldShowCodeAnalysis(): boolean {
return this.shouldShowAnalysis && !!this.viewContext[SNYK_CONTEXT.CODE_ENABLED];
}
Expand Down
3 changes: 1 addition & 2 deletions src/snyk/common/services/viewManagerService.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import _ from 'lodash';
import { EventEmitter, TreeView } from 'vscode';
import { FeaturesViewProvider } from '../../base/views/featureSelection/featuresViewProvider';
import { FeaturesConfiguration } from '../configuration/configuration';
import { configuration } from '../configuration/instance';
import { REFRESH_VIEW_DEBOUNCE_INTERVAL } from '../constants/general';
import { TreeNode } from '../views/treeNode';

export type ViewType = FeaturesViewProvider | TreeView<TreeNode>;
export type ViewType = TreeView<TreeNode>;

export class ViewContainer {
private container = new Map<string, ViewType>();
Expand Down
28 changes: 20 additions & 8 deletions src/snyk/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import SnykLib from './base/modules/snykLib';
import { AuthenticationService } from './base/services/authenticationService';
import { ScanModeService } from './base/services/scanModeService';
import { EmptyTreeDataProvider } from './base/views/emptyTreeDataProvider';
import { FeaturesViewProvider } from './base/views/featureSelection/featuresViewProvider';
import { SupportProvider } from './base/views/supportProvider';
import { messages } from './cli/messages/messages';
import { Iteratively } from './common/analytics/itly';
Expand Down Expand Up @@ -37,7 +36,6 @@ import {
SNYK_VIEW_ANALYSIS_CODE_SECURITY,
SNYK_VIEW_ANALYSIS_IAC,
SNYK_VIEW_ANALYSIS_OSS,
SNYK_VIEW_FEATURES,
SNYK_VIEW_SUPPORT,
SNYK_VIEW_WELCOME,
} from './common/constants/views';
Expand Down Expand Up @@ -109,6 +107,19 @@ class SnykExtension extends SnykLib implements IExtension {
}

private async initializeExtension(vscodeContext: vscode.ExtensionContext, snykConfiguration?: SnykConfiguration) {
// initialize context correctly
// see package.json when each view is shown, based on context value
await this.contextService.setContext(SNYK_CONTEXT.INITIALIZED, false);

// default to true, as the check is async and can only be done after startup of LS
// if set to true, the option to enable code is not shown in the initialization phase
await this.contextService.setContext(SNYK_CONTEXT.CODE_ENABLED, true);

// set the workspace context so that the text to add folders is only shown if really the case
// initializing after LS startup and just before scan is too late
const workspacePaths = vsCodeWorkspace.getWorkspaceFolders();
await this.setWorkspaceContext(workspacePaths);

this.user = await User.getAnonymous(this.context, Logger);

this.analytics = new Iteratively(
Expand Down Expand Up @@ -287,10 +298,7 @@ class SnykExtension extends SnykLib implements IExtension {
configuration,
);

const featuresViewProvider = new FeaturesViewProvider(vscodeContext.extensionUri, this.contextService);

vscodeContext.subscriptions.push(
vscode.window.registerWebviewViewProvider(SNYK_VIEW_FEATURES, featuresViewProvider),
vscode.window.registerTreeDataProvider(SNYK_VIEW_ANALYSIS_OSS, ossVulnerabilityProvider),
vscode.window.registerTreeDataProvider(SNYK_VIEW_SUPPORT, new SupportProvider()),
);
Expand Down Expand Up @@ -332,7 +340,6 @@ class SnykExtension extends SnykLib implements IExtension {
// Fill the view container to expose views for tests
const viewContainer = this.viewManagerService.viewContainer;
viewContainer.set(SNYK_VIEW_WELCOME, welcomeTree);
viewContainer.set(SNYK_VIEW_FEATURES, featuresViewProvider);

vscode.workspace.onDidChangeWorkspaceFolders(e => {
this.workspaceTrust.resetTrustedFoldersCache();
Expand Down Expand Up @@ -393,14 +400,19 @@ class SnykExtension extends SnykLib implements IExtension {
configuration,
);

await this.contextService.setContext(SNYK_CONTEXT.LS_CODE_PREVIEW, true);

// noinspection ES6MissingAwait
void this.advisorScoreDisposable.activate();

// Wait for LS startup to finish before updating the codeEnabled context
// The codeEnabled context depends on an LS command
await this.languageServer.start();

// initialize contexts
const loggedIn = await configuration.getToken();
if (loggedIn != undefined) {
await this.contextService.setContext(SNYK_CONTEXT.LOGGEDIN, true);
}
await this.contextService.setContext(SNYK_CONTEXT.INITIALIZED, true);
await this.codeSettings.updateIsCodeEnabled();

// Actually start analysis
Expand Down
Loading

0 comments on commit f158194

Please sign in to comment.