Skip to content

Commit

Permalink
feat: folderConfigs LS integration wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawkyZ committed Jul 18, 2024
1 parent f10f698 commit db4d7f6
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 3 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@
"default": [],
"description": "Folders to trust for Snyk scans."
},
"snyk.folderConfigs": {
"type": "array",
"default": [],
"description": "Folder configuration for Snyk scans."
},
"snyk.features.preview": {
"type": "object",
"default": {},
Expand Down
27 changes: 27 additions & 0 deletions src/snyk/common/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
YES_CRASH_REPORT_SETTING,
YES_WELCOME_NOTIFICATION_SETTING,
DELTA_FINDINGS,
FOLDER_CONFIGS,
} from '../constants/settings';
import SecretStorageAdapter from '../vscode/secretStorage';
import { IVSCodeWorkspace } from '../vscode/workspace';
Expand All @@ -36,6 +37,13 @@ export type FeaturesConfiguration = {
iacEnabled: boolean | undefined;
};


Check failure on line 40 in src/snyk/common/configuration/configuration.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Delete `⏎`

Check failure on line 40 in src/snyk/common/configuration/configuration.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Delete `⏎`
export type FolderConfig = {
folderPath: string;
baseBranch: string;
localBranches: string[] | undefined;
};

export interface IssueViewOptions {
ignoredIssues: boolean;
openIssues: boolean;
Expand Down Expand Up @@ -122,6 +130,10 @@ export interface IConfiguration {
setEndpoint(endpoint: string): Promise<void>;

getDeltaFindingsEnabled(): boolean;

getFolderConfigs(): FolderConfig[];

setFolderConfigs(folderConfig: FolderConfig[]): Promise<void>;
}

export class Configuration implements IConfiguration {
Expand Down Expand Up @@ -463,6 +475,12 @@ export class Configuration implements IConfiguration {
);
}

getFolderConfigs(): FolderConfig[] {
return (
this.workspace.getConfiguration<FolderConfig[]>(CONFIGURATION_IDENTIFIER, this.getConfigName(FOLDER_CONFIGS)) || []

Check failure on line 480 in src/snyk/common/configuration/configuration.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Insert `⏎·····`

Check failure on line 480 in src/snyk/common/configuration/configuration.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Insert `⏎·····`
);
}

get scanningMode(): string | undefined {
return this.workspace.getConfiguration<string>(CONFIGURATION_IDENTIFIER, this.getConfigName(SCANNING_MODE));
}
Expand All @@ -476,5 +494,14 @@ export class Configuration implements IConfiguration {
);
}

async setFolderConfigs(folderConfigs: FolderConfig[]): Promise<void> {
await this.workspace.updateConfiguration(
CONFIGURATION_IDENTIFIER,
this.getConfigName(FOLDER_CONFIGS),
folderConfigs,
true,
);
}

private getConfigName = (setting: string) => setting.replace(`${CONFIGURATION_IDENTIFIER}.`, '');
}
40 changes: 40 additions & 0 deletions src/snyk/common/configuration/folderConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { FolderConfig, IConfiguration } from './configuration';

export interface IFolderConfigs {
getFolderConfigs(config: IConfiguration): ReadonlyArray<FolderConfig>;

resetFolderConfigsCache(): void;
}

export class FolderConfigs implements IFolderConfigs {
private folderConfigsCache?: ReadonlyArray<FolderConfig>;

getFolderConfigs(config: IConfiguration): ReadonlyArray<FolderConfig> {
if (this.folderConfigsCache !== undefined) {
return this.folderConfigsCache;
}

const folderConfigs = config.getFolderConfigs();

this.folderConfigsCache = folderConfigs;

return folderConfigs;
}

setFolderConfig(_config: IConfiguration, _folderPath: string, _baseBranch: string) {
// Get all folders and update one you need and use setFolderConfig.
// if (this.folderConfigsCache !== undefined) {
// return this.folderConfigsCache;
// }

Check failure on line 29 in src/snyk/common/configuration/folderConfig.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Delete `⏎`

Check failure on line 29 in src/snyk/common/configuration/folderConfig.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Delete `⏎`
// const folderConfigs = config.getFolderConfigs();

Check failure on line 31 in src/snyk/common/configuration/folderConfig.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Replace `⏎····//·this.folderConfigsCache·=·folderConfigs;⏎` with `····//·this.folderConfigsCache·=·folderConfigs;`

Check failure on line 31 in src/snyk/common/configuration/folderConfig.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Replace `⏎····//·this.folderConfigsCache·=·folderConfigs;⏎` with `····//·this.folderConfigsCache·=·folderConfigs;`
// this.folderConfigsCache = folderConfigs;

// return folderConfigs;
}

resetFolderConfigsCache() {
this.folderConfigsCache = undefined;
}
}
1 change: 1 addition & 0 deletions src/snyk/common/constants/languageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export const SNYK_HAS_AUTHENTICATED = '$/snyk.hasAuthenticated';
export const SNYK_CLI_PATH = '$/snyk.isAvailableCli';
export const SNYK_ADD_TRUSTED_FOLDERS = '$/snyk.addTrustedFolders';
export const SNYK_SCAN = '$/snyk.scan';
export const SNYK_FOLDERCONFIG = '$/snyk.folderConfigs';
1 change: 1 addition & 0 deletions src/snyk/common/constants/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const ADVANCED_CUSTOM_LS_PATH = `${CONFIGURATION_IDENTIFIER}.advanced.lan
export const ISSUE_VIEW_OPTIONS_SETTING = `${CONFIGURATION_IDENTIFIER}.issueViewOptions`;
export const SEVERITY_FILTER_SETTING = `${CONFIGURATION_IDENTIFIER}.severity`;
export const TRUSTED_FOLDERS = `${CONFIGURATION_IDENTIFIER}.trustedFolders`;
export const FOLDER_CONFIGS = `${CONFIGURATION_IDENTIFIER}.folderConfigs`;
export const SCANNING_MODE = `${CONFIGURATION_IDENTIFIER}.scanningMode`;

export const DELTA_FINDINGS = `${FEATURES_PREVIEW_SETTING}.deltaFindings`;
11 changes: 9 additions & 2 deletions src/snyk/common/languageServer/languageServer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import _ from 'lodash';
import { firstValueFrom, ReplaySubject, Subject } from 'rxjs';
import { IAuthenticationService } from '../../base/services/authenticationService';
import { IConfiguration } from '../configuration/configuration';
import { FolderConfig, IConfiguration } from '../configuration/configuration';
import {
SNYK_ADD_TRUSTED_FOLDERS,
SNYK_CLI_PATH,
SNYK_FOLDERCONFIG,
SNYK_HAS_AUTHENTICATED,
SNYK_LANGUAGE_SERVER_NAME,
SNYK_SCAN,
Expand Down Expand Up @@ -118,7 +119,7 @@ export class LanguageServer implements ILanguageServer {
};

// Create the language client and start the client.
this.client = this.languageClientAdapter.create('Snyk LS', SNYK_LANGUAGE_SERVER_NAME, serverOptions, clientOptions);
this.client = this.languageClientAdapter.create('SnykLS', SNYK_LANGUAGE_SERVER_NAME, serverOptions, clientOptions);

try {
// Start the client. This will also launch the server
Expand All @@ -138,6 +139,12 @@ export class LanguageServer implements ILanguageServer {
});
});

client.onNotification(SNYK_FOLDERCONFIG, ({ folderConfigs }: { folderConfigs: FolderConfig[] }) => {
this.configuration.setFolderConfigs(folderConfigs).catch((error: Error) => {
ErrorHandler.handle(error, this.logger, error.message);
});
});

client.onNotification(SNYK_CLI_PATH, ({ cliPath }: { cliPath: string }) => {
if (!cliPath) {
ErrorHandler.handle(
Expand Down
4 changes: 3 additions & 1 deletion src/snyk/common/languageServer/settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';
import { CLI_INTEGRATION_NAME } from '../../cli/contants/integration';
import { Configuration, IConfiguration, SeverityFilter } from '../configuration/configuration';
import { Configuration, FolderConfig, IConfiguration, SeverityFilter } from '../configuration/configuration';
import { User } from '../user';
import { PROTOCOL_VERSION } from '../constants/languageServer';

Expand Down Expand Up @@ -41,6 +41,7 @@ export type ServerSettings = {
deviceId?: string;
requiredProtocolVersion?: string;
enableDeltaFindings?: string;
folderConfigs: FolderConfig[];
};

export class LanguageServerSettings {
Expand Down Expand Up @@ -80,6 +81,7 @@ export class LanguageServerSettings {
integrationVersion: await Configuration.getVersion(),
deviceId: user.anonymousId,
requiredProtocolVersion: `${PROTOCOL_VERSION}`,
folderConfigs: configuration.getFolderConfigs()

Check failure on line 84 in src/snyk/common/languageServer/settings.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Insert `,`

Check failure on line 84 in src/snyk/common/languageServer/settings.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Insert `,`
};
}
}
1 change: 1 addition & 0 deletions src/test/unit/common/languageServer/languageServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ suite('Language Server', () => {
insecure: 'true',
requiredProtocolVersion: '12',
scanningMode: 'auto',
folderConfigs: []

Check failure on line 226 in src/test/unit/common/languageServer/languageServer.test.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Insert `,`

Check failure on line 226 in src/test/unit/common/languageServer/languageServer.test.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Insert `,`
};

deepStrictEqual(await languageServer.getInitializationOptions(), expectedInitializationOptions);
Expand Down

0 comments on commit db4d7f6

Please sign in to comment.