Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle LS folderConfigs [IDE-502] #491

Merged
merged 9 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
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 @@
);
}

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 @@
);
}

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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary if we want to enable trace logs later. The Language Client Id can't have an empty space


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 @@
deviceId?: string;
requiredProtocolVersion?: string;
enableDeltaFindings?: string;
folderConfigs: FolderConfig[];
};

export class LanguageServerSettings {
Expand Down Expand Up @@ -80,6 +81,7 @@
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 `,`
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@
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
Loading