Skip to content

Commit 97b3492

Browse files
committed
references report
1 parent 509ee40 commit 97b3492

File tree

6 files changed

+111
-14
lines changed

6 files changed

+111
-14
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## 0.3.1
4+
5+
- Generate references report (beta).
6+
- Bugfixes.
7+
38
## 0.3.0
49

510
- Improve the view of imported notes.
@@ -14,7 +19,7 @@
1419

1520
- Added an explorer to track the progress of the audit.
1621
- Possibility to mark files as reviewed.
17-
- Functionality to exclude files and folders from the audit.
22+
- Functionality to exclude files and folders from the audit scope.
1823
- Cleaning of icons to reduce the size of the extension.
1924
- Minify SVG.
2025
- Fix: update minimist package.

package.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "code-auditor",
33
"displayName": "CodeAuditor",
44
"description": "Code Auditor Notebook",
5-
"version": "0.3.0",
5+
"version": "0.3.1",
66
"publisher": "red4sec",
77
"icon": "resources/code-auditor.png",
88
"readme": "README.md",
@@ -40,6 +40,7 @@
4040
"onCommand:code-auditor.newNote",
4141
"onCommand:code-auditor.removeNote",
4242
"onCommand:code-auditor.generateReport",
43+
"onCommand:code-auditor.generateReferences",
4344
"onCommand:code-auditor.setNoteState",
4445
"onCommand:code-auditor.importSlither",
4546
"onCommand:code-auditor.importSemgrep",
@@ -113,6 +114,14 @@
113114
"dark": "resources/dark/preview.svg"
114115
}
115116
},
117+
{
118+
"command": "code-auditor.generateReferences",
119+
"title": "Audit: Generate references",
120+
"icon": {
121+
"light": "resources/light/preview.svg",
122+
"dark": "resources/dark/preview.svg"
123+
}
124+
},
116125
{
117126
"command": "code-auditor.importSlither",
118127
"title": "Audit: Import Slither",
@@ -264,6 +273,10 @@
264273
"command": "code-auditor.generateReport",
265274
"when": "view == code-auditor.noteExplorer"
266275
},
276+
{
277+
"command": "code-auditor.generateReferences",
278+
"when": "view == code-auditor.noteExplorer"
279+
},
267280
{
268281
"command": "code-auditor.importSlither",
269282
"when": "view == code-auditor.noteExplorer"
@@ -272,7 +285,6 @@
272285
"command": "code-auditor.importSemgrep",
273286
"when": "view == code-auditor.noteExplorer"
274287
}
275-
276288
],
277289
"view/title": [
278290
{

src/codeauditor.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { noteProvider } from './noteexplorer';
55
import { fileState, noteState, noteType } from './types';
66
import { excludePath, newNote, removeNote, setFileState, setNoteState, setNoteType } from './notes';
77
import { progressProvider } from './progressexplorer';
8-
import { generateReport } from './report';
8+
import { generateReferences, generateReport } from './report';
99
import { auditDataInit } from './storage';
1010
import { ImportSemgrepReport, ImportSlitherReport } from './importnotes';
1111

@@ -49,10 +49,15 @@ export function activate(context: vscode.ExtensionContext) {
4949
if (item) { excludePath(item.uri); }
5050
}));
5151
context.subscriptions.push(vscode.commands.registerCommand('code-auditor.generateReport', () => {
52-
const out = vscode.window.createOutputChannel("report");
52+
const out = vscode.window.createOutputChannel("Audit Report");
5353
generateReport(out);
5454
out.show();
5555
}));
56+
context.subscriptions.push(vscode.commands.registerCommand('code-auditor.generateReferences', () => {
57+
const out = vscode.window.createOutputChannel("Audit References");
58+
generateReferences(out);
59+
out.show();
60+
}));
5661
context.subscriptions.push(vscode.commands.registerCommand('code-auditor.importSlither', () => {
5762
ImportSlitherReport();
5863
}));

src/filter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const currentFilter: filerOptions = {
1717
};
1818

1919
export function toggleFilter(filter: string) {
20-
if(Object.prototype.hasOwnProperty.call(currentFilter, filter)) {
20+
if (Object.prototype.hasOwnProperty.call(currentFilter, filter)) {
2121
currentFilter[filter] = !currentFilter[filter];
2222
}
2323
vscode.commands.executeCommand('setContext', 'code-auditor.filter.' + filter, currentFilter[filter]);
@@ -60,6 +60,9 @@ export function listFilterNotes(): FileCollection {
6060
}
6161
notes[parseInt(lineNum)] = note;
6262
}
63+
if (Object.keys(notes).length === 0) {
64+
continue;
65+
}
6366
nodes[fileName] = {
6467
lines: fileInfo.lines,
6568
state: fileInfo.state,

src/noteexplorer.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ export class noteProvider implements vscode.TreeDataProvider<noteNode> {
4646
const nodes: noteNode[] = [];
4747

4848
for (const [fileName, fileInfo] of Object.entries(filteredNotes)) {
49-
if (Object.keys(fileInfo.notes).length === 0) {
50-
continue;
51-
}
5249
const label = path.parse(fileName).base;
5350
const desc = path.parse(fileName).dir;
5451
nodes.push(

src/report.ts

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,97 @@
11
import * as vscode from 'vscode';
2+
import { URL } from 'url';
23
import { auditData } from './storage';
34
import { listFilterNotes } from './filter';
5+
import { noteSeparator } from './importnotes';
46

57

68
export function generateReport(out: vscode.OutputChannel) {
79
if (!auditData) {
810
vscode.window.showErrorMessage("Extension not ready");
911
return;
1012
}
11-
for (const [fileName, fileInfo] of Object.entries(listFilterNotes())) {
12-
if (Object.keys(fileInfo.notes).length === 0) {
13-
continue;
14-
}
13+
14+
const filteredNotes = listFilterNotes();
15+
for (const fileName of Object.keys(filteredNotes).sort()) {
1516
out.appendLine(`${fileName}`);
16-
for (const [lineNum, note] of Object.entries(fileInfo.notes)) {
17+
for (const [lineNum, note] of Object.entries(filteredNotes[fileName].notes)) {
1718
let afectedLines = note.length > 1 ? `${lineNum}:${parseInt(lineNum) + note.length - 1}` : lineNum;
1819
if (afectedLines.length < 4) { afectedLines += '\t'; }
1920
out.appendLine(`\t${afectedLines}\t- ${note.type}: ${note.state}\t=> ${note.message}`);
2021
}
2122
}
2223
}
24+
25+
export async function generateReferences(out: vscode.OutputChannel) {
26+
if (!auditData) {
27+
vscode.window.showErrorMessage("Extension not ready");
28+
return;
29+
}
30+
31+
const inputSort = await vscode.window.showInputBox({
32+
value: "3",
33+
prompt: "Sort by the first # words",
34+
placeHolder: "number between 1 and 10",
35+
ignoreFocusOut: true,
36+
});
37+
if (!inputSort) {
38+
vscode.window.showErrorMessage("Operation cancelled");
39+
return;
40+
}
41+
let sortBy = parseInt(inputSort);
42+
if (!sortBy || sortBy < 1 || sortBy > 10) { sortBy = 2; }
43+
44+
let inputUrl = await vscode.window.showInputBox({
45+
prompt: "Enter the base URL or leave empty",
46+
placeHolder: "optional base URL",
47+
ignoreFocusOut: true
48+
});
49+
let baseURL;
50+
if (inputUrl) {
51+
inputUrl = inputUrl.trim();
52+
if (!inputUrl.endsWith('/')) {
53+
inputUrl += '/';
54+
}
55+
try {
56+
const tryURL = new URL(inputUrl);
57+
baseURL = inputUrl;
58+
} catch (error) {
59+
vscode.window.showInformationMessage("Invalid base URL");
60+
baseURL = "";
61+
}
62+
}
63+
64+
const noterefs: { [key: string]: string[]; } = {};
65+
const filteredNotes = listFilterNotes();
66+
for (const fileName of Object.keys(filteredNotes).sort()) {
67+
for (const [lineNum, note] of Object.entries(filteredNotes[fileName].notes)) {
68+
if (!note.message) { continue; }
69+
const lineInd = baseURL ? 'L' : '';
70+
const afectedLines = note.length > 1 ? `${lineInd}${lineNum}-${lineInd}${parseInt(lineNum) + note.length - 1}` : lineInd + lineNum;
71+
72+
for (const x of note.message.split(noteSeparator)) {
73+
const key = x.toLowerCase().split(' ').slice(0, sortBy).join(' ');
74+
const ref = `${fileName}#${afectedLines}`;
75+
76+
if (noterefs[key]) {
77+
if (!noterefs[key].includes(ref)) {
78+
noterefs[key].push(ref);
79+
}
80+
} else {
81+
noterefs[key] = [ref];
82+
}
83+
}
84+
}
85+
}
86+
87+
for (const desc of Object.keys(noterefs).sort()) {
88+
out.appendLine(`${desc}`);
89+
for (const ref of Object.values(noterefs[desc])) {
90+
if (baseURL) {
91+
out.appendLine(`\t ${new URL(baseURL + ref)}`);
92+
} else {
93+
out.appendLine(`\t- ${ref}`);
94+
}
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)