Skip to content

Commit

Permalink
feat: clear cache command
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawkyZ committed Oct 11, 2024
1 parent ec8491f commit 15c375f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,11 @@
"command": "snyk.showLsOutputChannel",
"title": "Show Language Server Output Channel",
"category": "Snyk"
},
{
"command": "snyk.clearPersistedCache",
"title": "Clear Persisted Cache",
"category": "Snyk"
}
]
},
Expand Down
2 changes: 2 additions & 0 deletions src/snyk/base/modules/baseSnykModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IWorkspaceTrust, WorkspaceTrust } from '../../common/configuration/trus
import { ExperimentService } from '../../common/experiment/services/experimentService';
import { ILanguageServer } from '../../common/languageServer/languageServer';
import { CodeIssueData, IacIssueData } from '../../common/languageServer/types';
import { IClearCacheService } from '../../common/services/CacheService';
import { ContextService, IContextService } from '../../common/services/contextService';
import { DownloadService } from '../../common/services/downloadService';
import { FeatureFlagService } from '../../common/services/featureFlagService';
Expand Down Expand Up @@ -36,6 +37,7 @@ export default abstract class BaseSnykModule implements IBaseSnykModule {
protected configurationWatcher: IWatcher;

readonly contextService: IContextService;
cacheService: IClearCacheService;
readonly openerService: IOpenerService;
readonly viewManagerService: IViewManagerService;
protected authService: IAuthenticationService;
Expand Down
3 changes: 2 additions & 1 deletion src/snyk/common/constants/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ export const SNYK_SHOW_ERROR_FROM_CONTEXT_COMMAND = 'snyk.showErrorFromContext';
export const SNYK_GET_LESSON_COMMAND = 'snyk.getLearnLesson';
export const SNYK_GET_SETTINGS_SAST_ENABLED = 'snyk.getSettingsSastEnabled';
export const SNYK_SET_BASE_BRANCH_COMMAND = 'snyk.setBaseBranch';
// commands
export const SNYK_LOGIN_COMMAND = 'snyk.login';
export const SNYK_WORKSPACE_SCAN_COMMAND = 'snyk.workspace.scan';
export const SNYK_TRUST_WORKSPACE_FOLDERS_COMMAND = 'snyk.trustWorkspaceFolders';
export const SNYK_GET_ACTIVE_USER = 'snyk.getActiveUser';
export const SNYK_CODE_FIX_DIFFS_COMMAND = 'snyk.code.fixDiffs';
export const SNYK_FEATURE_FLAG_COMMAND = 'snyk.getFeatureFlagStatus';
export const SNYK_CLEAR_CACHE_COMMAND = 'snyk.clearCache';
export const SNYK_CLEAR_PERSISTED_CACHE_COMMAND = 'snyk.clearPersistedCache';

// custom Snyk constants used in commands
export const SNYK_CONTEXT_PREFIX = 'snyk:';
2 changes: 2 additions & 0 deletions src/snyk/common/constants/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export const COMMAND_DEBOUNCE_INTERVAL = 200; // 200 milliseconds
export const DEFAULT_SCAN_DEBOUNCE_INTERVAL = 1000; // 1 second
export const DEFAULT_LS_DEBOUNCE_INTERVAL = 1000; // 1 second
export const REFRESH_VIEW_DEBOUNCE_INTERVAL = 200; // 200 milliseconds
export const InMemory = "inMemory"

Check failure on line 12 in src/snyk/common/constants/general.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Replace `"inMemory"` with `'inMemory';`

Check failure on line 12 in src/snyk/common/constants/general.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Replace `"inMemory"` with `'inMemory';`
export const Persisted = "persisted"

Check failure on line 13 in src/snyk/common/constants/general.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Replace `"persisted"` with `'persisted';`

Check failure on line 13 in src/snyk/common/constants/general.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Replace `"persisted"` with `'persisted';`
20 changes: 20 additions & 0 deletions src/snyk/common/services/CacheService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { IVSCodeCommands } from '../vscode/commands';
import { SNYK_CLEAR_CACHE_COMMAND } from '../constants/commands';

export interface IClearCacheService {
clearCache(folderUri?: string, cacheType?: string): Promise<void>;
}

export class ClearCacheService implements IClearCacheService {
constructor(private commandExecutor: IVSCodeCommands) {}

async clearCache(folderUri?: string, cacheType?: string): Promise<void> {
try {
const uri = folderUri || '';
const type = cacheType || '';
await this.commandExecutor.executeCommand(SNYK_CLEAR_CACHE_COMMAND, uri, type);
} catch (error) {
console.warn(`[ClearCacheService] Failed to clear cache`);
}
}
}
39 changes: 39 additions & 0 deletions src/snyk/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { OpenIssueCommandArg } from './common/commands/types';
import { configuration } from './common/configuration/instance';
import { SnykConfiguration } from './common/configuration/snykConfiguration';
import {
SNYK_CLEAR_PERSISTED_CACHE_COMMAND,
SNYK_DCIGNORE_COMMAND,
SNYK_ENABLE_CODE_COMMAND,
SNYK_IGNORE_ISSUE_COMMAND,
Expand Down Expand Up @@ -78,6 +79,8 @@ import { OssVulnerabilityCountService } from './snykOss/services/vulnerabilityCo
import { FeatureFlagService } from './common/services/featureFlagService';
import { DiagnosticsIssueProvider } from './common/services/diagnosticsService';
import { CodeIssueData, IacIssueData, OssIssueData } from './common/languageServer/types';
import { ClearCacheService } from './common/services/CacheService';
import { InMemory, Persisted } from './common/constants/general';

class SnykExtension extends SnykLib implements IExtension {
public async activate(vscodeContext: vscode.ExtensionContext): Promise<void> {
Expand All @@ -91,11 +94,45 @@ class SnykExtension extends SnykLib implements IExtension {

try {
await this.initializeExtension(vscodeContext, snykConfiguration);
await this.configureGitHandlers();
} catch (e) {
ErrorHandler.handle(e, Logger);
}
}

async configureGitHandlers(): Promise<void> {

Check failure on line 103 in src/snyk/extension.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Async method 'configureGitHandlers' has no 'await' expression

Check failure on line 103 in src/snyk/extension.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Async method 'configureGitHandlers' has no 'await' expression
// Get the Git extension
const gitExtension = vscode.extensions.getExtension('vscode.git')?.exports;

if (!gitExtension) {
return;
}

// Get the API from the Git extension
const git = gitExtension.getAPI(1);

Check failure on line 112 in src/snyk/extension.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Unsafe call of an `any` typed value

Check failure on line 112 in src/snyk/extension.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Unsafe member access .getAPI on an `any` value

Check failure on line 112 in src/snyk/extension.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Unsafe call of an `any` typed value

Check failure on line 112 in src/snyk/extension.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Unsafe member access .getAPI on an `any` value

// Check if there are any repositories
const repositories = git?.repositories;

Check failure on line 115 in src/snyk/extension.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Unsafe member access .repositories on an `any` value

Check failure on line 115 in src/snyk/extension.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Unsafe member access .repositories on an `any` value

if (!repositories || repositories.length === 0) {

Check failure on line 117 in src/snyk/extension.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Unsafe member access .length on an `any` value

Check failure on line 117 in src/snyk/extension.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Unsafe member access .length on an `any` value
return;
}
const previousBranches = new Map<any, string | undefined>();
// Register event listener for changes in each repository
repositories.forEach((repo: any) => {

Check failure on line 122 in src/snyk/extension.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Unsafe call of an `any` typed value

Check failure on line 122 in src/snyk/extension.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Unsafe member access .forEach on an `any` value

Check failure on line 122 in src/snyk/extension.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Unsafe call of an `any` typed value

Check failure on line 122 in src/snyk/extension.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Unsafe member access .forEach on an `any` value
let previousBranch = repo.state.HEAD?.name;

Check failure on line 123 in src/snyk/extension.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Unsafe member access .state on an `any` value

Check failure on line 123 in src/snyk/extension.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Unsafe member access .state on an `any` value
previousBranches.set(repo, previousBranch);
repo.state.onDidChange(async () => {
const currentBranch = repo.state.HEAD?.name;
const storedPreviousBranch = previousBranches.get(repo);
if (currentBranch !== storedPreviousBranch) {
await this.cacheService.clearCache(repo.rootUri.toString(), InMemory);
previousBranches.set(repo, currentBranch);
}
});
});
}

private async getSnykConfiguration(): Promise<SnykConfiguration | undefined> {
try {
return await SnykConfiguration.get(extensionContext.extensionPath, configuration.isDevelopment);
Expand Down Expand Up @@ -139,6 +176,7 @@ class SnykExtension extends SnykLib implements IExtension {
);

this.learnService = new LearnService(vsCodeCommands);
this.cacheService = new ClearCacheService(vsCodeCommands);

this.codeSettings = new CodeSettings(this.contextService, configuration, this.openerService, vsCodeCommands);

Expand Down Expand Up @@ -425,6 +463,7 @@ class SnykExtension extends SnykLib implements IExtension {
),
vscode.commands.registerCommand(SNYK_INITIATE_LOGIN_COMMAND, () => this.commandController.initiateLogin()),
vscode.commands.registerCommand(SNYK_SET_TOKEN_COMMAND, () => this.commandController.setToken()),
vscode.commands.registerCommand(SNYK_CLEAR_PERSISTED_CACHE_COMMAND, async () => await this.cacheService.clearCache("", Persisted)),
vscode.commands.registerCommand(SNYK_ENABLE_CODE_COMMAND, () =>
this.commandController.executeCommand(SNYK_ENABLE_CODE_COMMAND, () => this.enableCode()),
),
Expand Down

0 comments on commit 15c375f

Please sign in to comment.