Skip to content

Commit

Permalink
refactor: reorganise code structure to prepare for Snyk OSS integration
Browse files Browse the repository at this point in the history
  • Loading branch information
michelkaporin committed Aug 2, 2021
1 parent 27ba2d2 commit 7582fc1
Show file tree
Hide file tree
Showing 65 changed files with 693 additions and 664 deletions.
File renamed without changes.
File renamed without changes.
123 changes: 0 additions & 123 deletions src/interfaces/SnykInterfaces.ts

This file was deleted.

File renamed without changes.
43 changes: 43 additions & 0 deletions src/snyk/base/modules/baseSnykModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as vscode from 'vscode';
import { configuration } from '../../common/configuration';
import { ContextService, IContextService } from '../../common/services/contextService';
import { IOpenerService, OpenerService } from '../../common/services/openerService';
import { IViewManagerService, ViewManagerService } from '../../common/services/viewManagerService';
import SnykEditorsWatcher from '../../snykCode/watchers/editorsWatcher';
import { ISnykCode, SnykCode } from '../../snykCode/code';
import SnykStatusBarItem, { IStatusBarItem } from '../statusBarItem/statusBarItem';
import SettingsWatcher from '../../common/watchers/settingsWatcher';
import { IWatcher } from '../../common/watchers/interfaces';
import { IBaseSnykModule, errorType } from './interfaces';

export default abstract class BaseSnykModule implements IBaseSnykModule {
statusBarItem: IStatusBarItem;
filesWatcher: vscode.FileSystemWatcher;
editorsWatcher: IWatcher;
settingsWatcher: IWatcher;
contextService: IContextService;
openerService: IOpenerService;
viewManagerService: IViewManagerService;

snykCode: ISnykCode;

constructor() {
this.statusBarItem = new SnykStatusBarItem();
this.editorsWatcher = new SnykEditorsWatcher();
this.settingsWatcher = new SettingsWatcher();
this.viewManagerService = new ViewManagerService();
this.contextService = new ContextService();
this.openerService = new OpenerService();
this.snykCode = new SnykCode(
configuration,
this.openerService,
this.viewManagerService,
this.filesWatcher,
this.processError.bind(this),
);
}

abstract processError(error: errorType, options?: { [key: string]: any }): Promise<void>;

abstract startExtension(): Promise<void>;
}
45 changes: 45 additions & 0 deletions src/snyk/base/modules/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { IContextService } from '../../common/services/contextService';
import { IOpenerService } from '../../common/services/openerService';
import { IViewManagerService } from '../../common/services/viewManagerService';
import { ISnykCode } from '../../snykCode/code';
import { IStatusBarItem } from '../statusBarItem/statusBarItem';
import { ExtensionContext, FileSystemWatcher } from 'vscode';
import { IWatcher } from '../../common/watchers/interfaces';

export interface IBaseSnykModule {
statusBarItem: IStatusBarItem;
filesWatcher: FileSystemWatcher;
settingsWatcher: IWatcher;
contextService: IContextService;
openerService: IOpenerService;
viewManagerService: IViewManagerService;
snykCode: ISnykCode;

// Abstract methods
processError(error: errorType, options?: { [key: string]: any }): Promise<void>;
startExtension(): Promise<void>;
}

export interface IReportModule {
resetTransientErrors(): void;
}

export interface ILoginModule {
initiateLogin(): Promise<void>;
checkSession(): Promise<string>;
checkCodeEnabled(): Promise<boolean>;
checkAdvancedMode(): Promise<void>;
}

export interface ISnykLib {
setMode(mode: string): void;
enableCode(): Promise<void>;
}

export interface IExtension extends IBaseSnykModule, IReportModule, ILoginModule, ISnykLib {
context: ExtensionContext | undefined;
activate(context: ExtensionContext): void;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type errorType = Error | any;
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { checkSession, startSession } from '@snyk/code-client';
import { getIpFamily, IpFamily } from '@snyk/code-client/dist/http';
import { LoginModuleInterface } from '../../../interfaces/SnykInterfaces';
import { configuration } from '../../configuration';
import { SNYK_CONTEXT } from '../../constants/views';
import { Logger } from '../../logger';
import { errorsLogs } from '../../messages/errorsServerLogMessages';
import ReportModule from './ReportModule';
import { configuration } from '../../common/configuration';
import { SNYK_CONTEXT } from '../../common/constants/views';
import { Logger } from '../../common/logger/logger';
import { errorsLogs } from '../../common/messages/errorsServerLogMessages';
import { ILoginModule } from './interfaces';
import ReportModule from './reportModule';

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

abstract class LoginModule extends ReportModule implements LoginModuleInterface {
abstract class LoginModule extends ReportModule implements ILoginModule {
private pendingLogin = false;
private pendingToken = '';

Expand Down Expand Up @@ -106,8 +106,6 @@ abstract class LoginModule extends ReportModule implements LoginModuleInterface

await this.contextService.setContext(SNYK_CONTEXT.CODE_ENABLED, enabled);
if (!enabled) {
this.loadAnalytics();

this.loadingBadge.setLoadingBadge(true, this);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { constants, reportError } from '@snyk/code-client';
import * as _ from 'lodash';
import { errorType, ReportModuleInterface } from '../../../interfaces/SnykInterfaces';
import { configuration } from '../../configuration';
import { configuration } from '../../common/configuration';
import {
COMMAND_DEBOUNCE_INTERVAL,
CONNECTION_ERROR_RETRY_INTERVAL,
MAX_CONNECTION_RETRIES,
} from '../../constants/general';
import { SNYK_CONTEXT, SNYK_ERROR_CODES } from '../../constants/views';
import { Logger } from '../../logger';
import { errorsLogs } from '../../messages/errorsServerLogMessages';
import { ILoadingBadge, LoadingBadge } from '../../view/loadingBadge';
import BaseSnykModule from './BaseSnykModule';

abstract class ReportModule extends BaseSnykModule implements ReportModuleInterface {
} from '../../common/constants/general';
import { SNYK_CONTEXT, SNYK_ERROR_CODES } from '../../common/constants/views';
import { Logger } from '../../common/logger/logger';
import { errorsLogs } from '../../common/messages/errorsServerLogMessages';
import { ILoadingBadge, LoadingBadge } from '../views/loadingBadge';
import BaseSnykModule from './baseSnykModule';
import { errorType, IReportModule } from './interfaces';

abstract class ReportModule extends BaseSnykModule implements IReportModule {
private transientErrors = 0;
protected loadingBadge: ILoadingBadge;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import * as _ from 'lodash';
import { SnykLibInterface } from '../../../interfaces/SnykInterfaces';
import { configuration } from '../../configuration';
import { configuration } from '../../common/configuration';
import {
EXECUTION_DEBOUNCE_INTERVAL,
EXECUTION_PAUSE_INTERVAL,
EXECUTION_THROTTLING_INTERVAL,
} from '../../constants/general';
import { SNYK_CONTEXT, SNYK_MODE_CODES } from '../../constants/views';
import { Logger } from '../../logger';
import { errorsLogs } from '../../messages/errorsServerLogMessages';
import { userMe } from '../../services/userService';
import BundlesModule from './BundlesModule';

export default class SnykLib extends BundlesModule implements SnykLibInterface {
} from '../../common/constants/general';
import { SNYK_CONTEXT, SNYK_MODE_CODES } from '../../common/constants/views';
import { Logger } from '../../common/logger/logger';
import { errorsLogs } from '../../common/messages/errorsServerLogMessages';
import { userMe } from '../../common/services/userService';
import * as vscode from 'vscode';
import LoginModule from './loginModule';
import { analytics } from '../../common/analytics/analytics';
import { ISnykLib } from './interfaces';

export default class SnykLib extends LoginModule implements ISnykLib {
private _mode = SNYK_MODE_CODES.AUTO;
// Platform-independant type definition.
private _unpauseTimeout: ReturnType<typeof setTimeout> | undefined;
Expand Down Expand Up @@ -69,10 +71,10 @@ export default class SnykLib extends BundlesModule implements SnykLibInterface {
try {
const user = await userMe();
if (user) {
this.analytics.identify(user.id);
analytics.identify(user.id);
}

await this.startAnalysis(manual);
await this.startSnykCodeAnalysis(manual);
} catch (err) {
await this.processError(err, {
message: errorsLogs.failedExecutionDebounce,
Expand Down Expand Up @@ -109,14 +111,30 @@ export default class SnykLib extends BundlesModule implements SnykLibInterface {
await this.checkCodeEnabled();

Logger.info('Snyk Code was enabled.');
await this.startAnalysis(false);
try {
await this.startSnykCodeAnalysis(false);
} catch (err) {
await this.processError(err);
}
}
}

async startSnykCodeAnalysis(manual: boolean): Promise<void> {
const paths = (vscode.workspace.workspaceFolders || []).map(f => f.uri.fsPath);

if (paths.length) {
await this.contextService.setContext(SNYK_CONTEXT.WORKSPACE_FOUND, true);

await this.snykCode.startAnalysis(paths, manual);
} else {
await this.contextService.setContext(SNYK_CONTEXT.WORKSPACE_FOUND, false);
}
}

onDidChangeWelcomeViewVisibility(visible: boolean): void {
if (visible && !configuration.token) {
// Track if a user is not authenticated and expanded the analysis view
this.analytics.logWelcomeViewIsViewed();
analytics.logWelcomeViewIsViewed();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export interface PendingTaskInterface {
export interface IPendingTask {
waiter: Promise<void>;
isCompleted: boolean;
complete(): void;
}

export class PendingTask implements PendingTaskInterface {
export class PendingTask implements IPendingTask {
private _promise: Promise<void>;
private _resolve: ((value?: unknown) => void) | undefined;
private _resolved: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import * as vscode from 'vscode';
import { StatusBarItemInterface } from '../../../interfaces/SnykInterfaces';
import { SNYK_NAME } from '../../constants/general';
import { SNYK_SETTINGS_COMMAND } from '../../constants/commands';
import { SNYK_NAME } from '../../common/constants/general';
import { SNYK_SETTINGS_COMMAND } from '../../common/constants/commands';

class SnykStatusBarItem implements StatusBarItemInterface {
export interface IStatusBarItem {
snykStatusBarItem: vscode.StatusBarItem;
show(): void;
}

class SnykStatusBarItem implements IStatusBarItem {
public snykStatusBarItem: vscode.StatusBarItem;
public constructor() {
this.snykStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TreeNode } from './treeNode';
import { TreeNodeProvider } from './treeNodeProvider';
import { TreeNode } from '../../common/views/treeNode';
import { TreeNodeProvider } from '../../common/views/treeNodeProvider';

/*
Provides an empty tree data for views with welcome content ("viewsWelcome" in package.json) because they are tree views by default.
Expand Down
Loading

0 comments on commit 7582fc1

Please sign in to comment.