Skip to content

Commit

Permalink
CM-31339 - Display progress bar only for on-demand scans (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshalX authored Jan 18, 2024
1 parent b14368c commit 8cef164
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [v1.2.1]

- Disable progress bar for scans on save
- Fix communication with CLI
- Fix quick fix ignore functionality for SCA

Expand Down
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ function initCommands(
documentToScan: vscode.window.activeTextEditor.document,
workspaceFolderPath: vscode.workspace.workspaceFolders?.[0]?.uri.fsPath,
diagnosticCollection,
onDemand: true,
},
treeView
);
Expand Down Expand Up @@ -401,6 +402,7 @@ function initCommands(
pathToScan: workspaceFolder.uri.fsPath,
workspaceFolderPath: workspaceFolder.uri.fsPath,
diagnosticCollection,
onDemand: true,
},
treeView
);
Expand Down
24 changes: 15 additions & 9 deletions src/services/scaScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface ScaScanParams {
workspaceFolderPath?: string;
diagnosticCollection: vscode.DiagnosticCollection;
config: IConfig;
onDemand?: boolean;
}

export function scaScan(
Expand All @@ -32,18 +33,23 @@ export function scaScan(
return;
}

if (!params.onDemand) {
_scaScan(params, undefined, undefined, treeView);
return;
}

vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
cancellable: true,
},
async (progress, token) => {
await _scaScanWithProgress(params, progress, token, treeView);
await _scaScan(params, progress, token, treeView);
},
);
}

const _initScanState = (params: ScaScanParams, progress: ProgressBar) => {
const _initScanState = (params: ScaScanParams, progress?: ProgressBar) => {
extensionOutput.info(StatusBarTexts.ScanWait);
statusBar.showScanningInProgress();

Expand All @@ -52,7 +58,7 @@ const _initScanState = (params: ScaScanParams, progress: ProgressBar) => {
);
updateWorkspaceState(VscodeStates.ScaScanInProgress, true);

progress.report({
progress?.report({
message: `SCA scanning ${params.workspaceFolderPath}...`,
});
};
Expand All @@ -67,18 +73,18 @@ const _getRunnableCliScaScan = (params: ScaScanParams): RunCliResult => {
return cliWrapper.getRunnableScaScanCommand(cliParams);
};

const _scaScanWithProgress = async (
const _scaScan = async (
params: ScaScanParams,
progress: ProgressBar,
cancellationToken: vscode.CancellationToken,
treeView: TreeView
progress?: ProgressBar,
cancellationToken?: vscode.CancellationToken,
treeView?: TreeView
) => {
try {
_initScanState(params, progress);

const runnableScaScan = _getRunnableCliScaScan(params);

cancellationToken.onCancellationRequested(async () => {
cancellationToken?.onCancellationRequested(async () => {
await runnableScaScan.getCancelPromise();
finalizeScanState(VscodeStates.ScaScanInProgress, true, progress);
});
Expand Down Expand Up @@ -144,7 +150,7 @@ export const detectionsToDiagnostics = async (
const handleScanDetections = async (
result: any,
diagnosticCollection: vscode.DiagnosticCollection,
treeView: TreeView
treeView?: TreeView
) => {
const {detections} = result;

Expand Down
21 changes: 14 additions & 7 deletions src/services/secretsScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@ interface SecretsScanParams {
workspaceFolderPath?: string;
diagnosticCollection: vscode.DiagnosticCollection;
config: IConfig;
onDemand?: boolean;
}

export const secretScan = (
params: SecretsScanParams,
treeView?: TreeView,
) => {
// we are showing progress bar only for on-demand scans
if (!params.onDemand) {
_secretScan(params, undefined, undefined, treeView);
return;
}

vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
cancellable: true,
},
async (progress, token) => {
await _secretScanWithProgress(params, progress, token, treeView);
await _secretScan(params, progress, token, treeView);
},
);
};
Expand All @@ -45,22 +52,22 @@ const _getRunnableCliSecretsScan = (params: SecretsScanParams): RunCliResult =>
return cliWrapper.getRunnableSecretsScanCommand(cliParams);
};

const _initScanState = (params: SecretsScanParams, progress: ProgressBar) => {
const _initScanState = (params: SecretsScanParams, progress?: ProgressBar) => {
extensionOutput.info(StatusBarTexts.ScanWait);
extensionOutput.info('Initiating scan for file: ' + params.documentToScan.fileName);

statusBar.showScanningInProgress();
updateWorkspaceState(VscodeStates.SecretsScanInProgress, true);

progress.report({
progress?.report({
message: `Secrets scanning ${params.documentToScan.fileName}...`,
});
};

export async function _secretScanWithProgress(
export async function _secretScan(
params: SecretsScanParams,
progress: ProgressBar,
cancellationToken: vscode.CancellationToken,
progress?: ProgressBar,
cancellationToken?: vscode.CancellationToken,
treeView?: TreeView,
) {
try {
Expand All @@ -77,7 +84,7 @@ export async function _secretScanWithProgress(

const runnableSecretsScan = _getRunnableCliSecretsScan(params);

cancellationToken.onCancellationRequested(async () => {
cancellationToken?.onCancellationRequested(async () => {
await runnableSecretsScan.getCancelPromise();
finalizeScanState(VscodeStates.SecretsScanInProgress, true, progress);
});
Expand Down

0 comments on commit 8cef164

Please sign in to comment.