Skip to content

Commit

Permalink
Merge pull request #77 from bridgecrewio/PCSUP-24582-VisualStudioCode…
Browse files Browse the repository at this point in the history
…-single-file-exception-error

[PCSUP-24582] QA Testing fixes
  • Loading branch information
ChananM authored Sep 8, 2024
2 parents 1a164ea + 628ba5c commit c4e9a95
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
14 changes: 12 additions & 2 deletions src/services/filesService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as vscode from 'vscode';
import logger from '../logger';
import { CheckovResult } from '../types';
import { formatWindowsAbsoluteFilePath, isWindows } from '../utils';

export class FilesService {
private static context: vscode.ExtensionContext;
Expand All @@ -7,13 +10,20 @@ export class FilesService {
FilesService.context = context;
}

public static async openFile(file: string, line: number = 1) {
public static async openResult(result: CheckovResult, line: number = 1) {
if (line < 1) {
line = 1;
}
if (!vscode.window.activeTextEditor) {
vscode.commands.executeCommand('workbench.action.previousEditor');
}
return vscode.window.showTextDocument(vscode.Uri.file(file), { selection: new vscode.Range(line - 1, 0, line - 1, 0) });
let filePath;
if (isWindows()) {
filePath = formatWindowsAbsoluteFilePath(result.file_abs_path);
} else {
filePath = result.file_abs_path;
}
logger.info(`Opening file at ${filePath}`);
return vscode.window.showTextDocument(vscode.Uri.file(filePath), { selection: new vscode.Range(line - 1, 0, line - 1, 0) });
}
}
6 changes: 6 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export const formatWindowsFilePath = (path: string): string => {
return path.replace(':', '').replace(/\\/g, '/');
};

export const formatWindowsAbsoluteFilePath = (path: string): string => {
const splitPath = path.replace(/\\/g, '/').replace(/^\/+/g, '').split('/');
splitPath[0] = splitPath[0].toLocaleUpperCase() + ':';
return splitPath.join('/');
};

export const getOsNameAndVersion = async () => {
const operatingSystem = os.type();
if ("Darwin" === operatingSystem) {
Expand Down
7 changes: 4 additions & 3 deletions src/views/interface/checkovResult/messages/focusString.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { FilesService } from '../../../../services';
import { CheckovResult } from '../../../../types';

export class FocusString {
public static async handle({ fileAbsPath, row }: { fileAbsPath: string, row: number }) {
if (fileAbsPath && row) {
await FilesService.openFile(fileAbsPath, row);
public static async handle({ result, row }: { result: CheckovResult, row: number }) {
if (result && row) {
await FilesService.openResult(result, row);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/views/interface/checkovResult/webviewPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ export class CheckovResultWebviewPanel {
private static getDataFlowItemString(dataFlow: DataFlow, result: CheckovResult): string {
const splitPath = dataFlow.path.split('/');
return `<div class="details">
<a target="_blank" onclick="onSastStepClick('${result.file_abs_path}', ${dataFlow.start.row})">${splitPath[splitPath.length - 1]}: ${dataFlow.start.row}</a><span>${dataFlow.code_block}</span>
<a target="_blank" onclick="onSastStepClick('${result}', ${dataFlow.start.row})">${splitPath[splitPath.length - 1]}: ${dataFlow.start.row}</a><span>${dataFlow.code_block}</span>
</div>`;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export class ResultTreeDataProvider implements vscode.TreeDataProvider<ResultTre

readonly category: CHECKOV_RESULT_CATEGORY;

private _onDidChangeTreeData: vscode.EventEmitter<ResultTreeItem | void> = new vscode.EventEmitter<ResultTreeItem | void>();
readonly onDidChangeTreeData: vscode.Event<ResultTreeItem | void> = this._onDidChangeTreeData.event;

private data: ResultTreeItem[] = [];
private treeService: TreeService;

Expand All @@ -27,6 +30,7 @@ export class ResultTreeDataProvider implements vscode.TreeDataProvider<ResultTre
public refresh() {
const checkovResults = this.getCheckovResults();
this.data = this.treeService.getTreeData(this.category, checkovResults);
this._onDidChangeTreeData.fire();
}

public getTreeItem(element: ResultTreeItem): vscode.TreeItem|Thenable<vscode.TreeItem> {
Expand Down Expand Up @@ -63,7 +67,7 @@ export class ResultTreeDataProvider implements vscode.TreeDataProvider<ResultTre
result.description = fetchedDescription;
}
}
const openedTextEditor = await FilesService.openFile(result.file_abs_path, result.file_line_range[0]);
const openedTextEditor = await FilesService.openResult(result, result.file_line_range[0]);
await CheckovResultWebviewPanel.show(this.category, result, openedTextEditor);
}

Expand Down
4 changes: 2 additions & 2 deletions static/webviews/result/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@
function onDocumentationClick(url) {
vscode.postMessage({ type: 'documentationClick', url: url });
}
function onSastStepClick(fileAbsPath, row) {
vscode.postMessage({ type: 'sastStepClick', fileAbsPath, row });
function onSastStepClick(result, row) {
vscode.postMessage({ type: 'sastStepClick', result, row });
}
</script>
</body>
Expand Down

0 comments on commit c4e9a95

Please sign in to comment.