Skip to content

Commit 7cedfc6

Browse files
committed
Improve messages and detect duplicates when importing files
1 parent 3d3122f commit 7cedfc6

4 files changed

Lines changed: 75 additions & 43 deletions

File tree

src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { reactionHandler } from './handlers/reaction';
1010
import { loadCommentsFromFile, saveCommentsToFile } from './persistence';
1111
import { saveNoteComment, setNoteStatus } from './helpers';
1212

13-
let noteList: vscode.CommentThread[] = [];
13+
const noteList: vscode.CommentThread[] = [];
1414

1515
export function activate(context: vscode.ExtensionContext) {
1616
Resource.initialize(context);
@@ -185,7 +185,7 @@ export function activate(context: vscode.ExtensionContext) {
185185
);
186186

187187
// load persisted comments from file
188-
noteList = loadCommentsFromFile();
188+
noteList.push(...loadCommentsFromFile());
189189
}
190190

191191
export function deactivate(context: vscode.ExtensionContext) {

src/parsers/semgrep.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,31 @@ class SemgrepParser {
77
static parse(fileContent: string) {
88
const toolFindings: ToolFinding[] = [];
99

10-
const semgrepFindings = JSON.parse(fileContent).results;
11-
semgrepFindings.map((semgrepFinding: any) => {
12-
// uri
13-
let fullPath = '';
14-
if (vscode.workspace.workspaceFolders) {
15-
fullPath = vscode.workspace.workspaceFolders[0].uri.fsPath + '/';
16-
}
17-
const uri = vscode.Uri.file(`${fullPath}${semgrepFinding.path}`);
10+
try {
11+
const semgrepFindings = JSON.parse(fileContent).results;
12+
semgrepFindings.map((semgrepFinding: any) => {
13+
// uri
14+
let fullPath = '';
15+
if (vscode.workspace.workspaceFolders) {
16+
fullPath = vscode.workspace.workspaceFolders[0].uri.fsPath + '/';
17+
}
18+
const uri = vscode.Uri.file(`${fullPath}${semgrepFinding.path}`);
1819

19-
// range
20-
const range = new vscode.Range(
21-
semgrepFinding.start.line - 1,
22-
0,
23-
semgrepFinding.end.line - 1,
24-
0,
25-
);
20+
// range
21+
const range = new vscode.Range(
22+
semgrepFinding.start.line - 1,
23+
0,
24+
semgrepFinding.end.line - 1,
25+
0,
26+
);
2627

27-
// instantiate tool finding and add to list
28-
const toolFinding = new ToolFinding(uri, range, semgrepFinding.extra.message);
29-
toolFindings.push(toolFinding);
30-
});
28+
// instantiate tool finding and add to list
29+
const toolFinding = new ToolFinding(uri, range, semgrepFinding.extra.message);
30+
toolFindings.push(toolFinding);
31+
});
32+
} catch {
33+
/* empty */
34+
}
3135

3236
return toolFindings;
3337
}

src/webviews/assets/main.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,15 @@
99
.querySelector('.process-file-button')
1010
.addEventListener('click', () => onButtonClicked());
1111

12-
// // Handle messages sent from the extension to the webview
13-
// window.addEventListener('message', (event) => {
14-
// const message = event.data; // The json data that the extension sent
15-
// switch (message.type) {
16-
// case 'foo': {
17-
// break;
18-
// }
19-
// }
20-
// });
21-
2212
function onButtonClicked() {
2313
let toolSelect = document.getElementById('toolSelect');
2414
let toolName = toolSelect.options[toolSelect.selectedIndex].text;
2515

2616
let selectedFile = document.getElementById('fileInput').files[0];
27-
readFile(selectedFile).then((fileContent) =>
17+
readFile(selectedFile).then((fileContent) => {
2818
vscode.postMessage({ type: 'processToolFile', toolName, fileContent }),
29-
);
19+
(document.getElementById('fileInput').value = '');
20+
});
3021
}
3122

3223
async function readFile(file) {

src/webviews/importToolResultsWebview.ts

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,52 @@ function processToolFile(
106106
}
107107
}
108108

109-
// instantiate comments based on parsed tool findings
110-
if (toolFindings.length) {
111-
toolFindings.map((result: ToolFinding) => {
112-
const newThread = commentController.createCommentThread(
113-
result.uri,
114-
result.range,
115-
[],
116-
);
117-
saveNoteComment(newThread, result.text, true, noteList, toolName);
118-
});
109+
if (!toolFindings.length) {
110+
vscode.window.showErrorMessage('An error has ocurred while parsing the file.');
111+
return;
112+
}
113+
114+
if (noteList.length && identifyPotentialDuplicates(toolName, noteList)) {
115+
vscode.window
116+
.showWarningMessage(
117+
`Potential duplicates. Current comments already include findings from ${toolName}. Do you want to import findings anyway?`,
118+
'Yes',
119+
'No',
120+
)
121+
.then((answer) => {
122+
if (answer === 'Yes') {
123+
saveToolFindings(toolFindings, noteList, toolName);
124+
}
125+
});
126+
} else {
127+
saveToolFindings(toolFindings, noteList, toolName);
119128
}
120129
}
130+
131+
function identifyPotentialDuplicates(
132+
toolName: string,
133+
noteList: vscode.CommentThread[],
134+
) {
135+
return noteList.some((thread) => {
136+
return thread.comments[0].author.name === toolName;
137+
});
138+
}
139+
140+
function saveToolFindings(
141+
toolFindings: ToolFinding[],
142+
noteList: vscode.CommentThread[],
143+
toolName: string,
144+
) {
145+
// instantiate comments based on parsed tool findings
146+
toolFindings.forEach((toolFinding: ToolFinding) => {
147+
const newThread = commentController.createCommentThread(
148+
toolFinding.uri,
149+
toolFinding.range,
150+
[],
151+
);
152+
saveNoteComment(newThread, toolFinding.text, true, noteList, toolName);
153+
});
154+
vscode.window.showInformationMessage(
155+
`${toolFindings.length} findings were imported successfully.`,
156+
);
157+
}

0 commit comments

Comments
 (0)