Skip to content

Commit 19d3d6a

Browse files
committed
Build filetree with missing directories to support --only-findings scans
Signed-off-by: Omkar Phansopkar <[email protected]>
1 parent 99a1525 commit 19d3d6a

File tree

10 files changed

+343
-46
lines changed

10 files changed

+343
-46
lines changed

src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import "react-toastify/dist/ReactToastify.css";
2929
import "bootstrap/dist/css/bootstrap.min.css";
3030
import "react-tooltip/dist/react-tooltip.css";
3131
import "./styles/app.css";
32+
import "./styles/toastify.css";
3233
import "./styles/dashStyles.css";
3334
import "./styles/customFaColors.css";
3435

src/contexts/dbContext.tsx

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,8 @@ export const WorkbenchDBProvider = (
183183
// Check that the database has the correct header information.
184184
if (!infoHeader) {
185185
const errTitle = "Invalid SQLite file";
186-
const errMessage =
187-
"Invalid SQLite file: " +
188-
sqliteFilePath +
189-
"\n" +
190-
"The SQLite file is invalid. Try re-importing the ScanCode JSON " +
191-
"file and creating a new SQLite file.";
186+
const errMessage = `Invalid SQLite file: ${sqliteFilePath}
187+
The SQLite file is invalid. Try re-importing the ScanCode JSON file and creating a new SQLite file.`;
192188

193189
console.error("Handled invalid sqlite import", {
194190
title: errTitle,
@@ -239,8 +235,7 @@ export const WorkbenchDBProvider = (
239235
.then((db) => db.File.findOne({ where: { parent: "#" } }))
240236
.then(async (root) => {
241237
if (!root) {
242-
console.error("Root path not found !!!!", root);
243-
return;
238+
throw new Error("Root path not found !!");
244239
}
245240

246241
const defaultPath = root.getDataValue("path");
@@ -271,35 +266,20 @@ export const WorkbenchDBProvider = (
271266
);
272267

273268
if (!preventNavigation) changeRouteOnImport();
274-
})
275-
.catch((err) => {
276-
const foundInvalidHistoryItem = GetHistory().find(
277-
(historyItem) => historyItem.sqlite_path === sqliteFilePath
278-
);
279-
if (foundInvalidHistoryItem) {
280-
RemoveEntry(foundInvalidHistoryItem);
281-
}
282-
console.error("Err trying to import sqlite:");
283-
console.error(err);
284-
toast.error(
285-
`Unexpected error while importing json \nPlease check console for more info`
286-
);
287-
abortImport();
288269
});
289270
})
290-
.catch((err) => {
271+
.catch((err: Error) => {
272+
abortImport();
291273
const foundInvalidHistoryItem = GetHistory().find(
292274
(historyItem) => historyItem.sqlite_path === sqliteFilePath
293275
);
294276
if (foundInvalidHistoryItem) {
295277
RemoveEntry(foundInvalidHistoryItem);
296278
}
297-
console.error("Err trying to import sqlite:");
298-
console.error(err);
279+
console.error("Err trying to import sqlite:", err);
299280
toast.error(
300-
`Unexpected error while finalising json import \nPlease check console for more info`
281+
`Sqlite file is outdated or corrupt\nPlease try importing json file again`
301282
);
302-
abortImport();
303283
});
304284
}
305285

@@ -341,11 +321,10 @@ export const WorkbenchDBProvider = (
341321
.then((db) => db.File.findOne({ where: { parent: "#" } }))
342322
.then(async (root) => {
343323
if (!root) {
344-
console.error("Root path not found !!!!");
345324
console.error("Root:", root);
346-
abortImport();
347-
return;
325+
throw new Error("Root path not found !!!!");
348326
}
327+
349328
const defaultPath = root.getDataValue("path");
350329

351330
await updateWorkbenchDB(newWorkbenchDB, sqliteFilePath);
@@ -369,16 +348,29 @@ export const WorkbenchDBProvider = (
369348
);
370349

371350
if (!preventNavigation) changeRouteOnImport();
351+
})
352+
.catch((err) => {
353+
abortImport();
354+
const foundInvalidHistoryItem = GetHistory().find(
355+
(historyItem) => historyItem.sqlite_path === sqliteFilePath
356+
);
357+
if (foundInvalidHistoryItem) {
358+
RemoveEntry(foundInvalidHistoryItem);
359+
}
360+
console.error(err);
361+
toast.error(
362+
`Can't resolve root directory \nPlease check console for more info`
363+
);
372364
});
373365
})
374366
.catch((err) => {
375367
abortImport();
376368
console.error(
377-
"Some error parsing data (caught in workbenchContext) !!",
369+
"Some error parsing json data (caught in dbContext)",
378370
err
379371
);
380372
toast.error(
381-
"Some error parsing data !! \nPlease check console for more info"
373+
"Some error parsing json data !! \nPlease check console for more info"
382374
);
383375
});
384376
}

src/pages/Licenses/Licenses.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ const LicenseDetections = () => {
171171
activateLicenseClue(newLicenseClues[0]);
172172
}
173173
}
174-
})().then(endProcessing);
174+
})()
175+
.then(endProcessing)
176+
.catch((err) => {
177+
endProcessing();
178+
console.error("Error getting license contents", err);
179+
toast.error("Error loading license data.");
180+
});
175181
}, []);
176182

177183
const handleItemToggle = (

src/pages/TableView/TableView.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Op } from "sequelize";
2+
import { toast } from "react-toastify";
23
import React, { useEffect, useState } from "react";
34
import { ColDef, ColumnApi, GridApi, GridReadyEvent } from "ag-grid-community";
45

@@ -101,6 +102,11 @@ const TableView = () => {
101102
if (prevColDefs.length > 0) return prevColDefs; // Don't mutate cols, if already set
102103
return [...COLUMN_GROUPS.DEFAULT];
103104
});
105+
})
106+
.catch((err) => {
107+
endProcessing();
108+
console.error("Error getting tableview contents", err);
109+
toast.error("Error loading table data.");
104110
});
105111

106112
// Update set filters whenever new db is loaded or path is changed

src/services/models/header.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,33 @@ export default function headerModel(sequelize: Sequelize) {
5353
primaryKey: true,
5454
type: DataTypes.INTEGER,
5555
},
56-
json_file_name: DataTypes.STRING,
57-
tool_name: DataTypes.STRING,
58-
tool_version: DataTypes.STRING,
59-
notice: DataTypes.STRING,
60-
duration: DataTypes.DOUBLE,
56+
json_file_name: {
57+
type: DataTypes.STRING,
58+
defaultValue: null,
59+
},
60+
tool_name: {
61+
type: DataTypes.STRING,
62+
defaultValue: null,
63+
},
64+
tool_version: {
65+
type: DataTypes.STRING,
66+
defaultValue: null,
67+
},
68+
notice: {
69+
type: DataTypes.STRING,
70+
defaultValue: null,
71+
},
72+
duration: {
73+
type: DataTypes.DOUBLE,
74+
defaultValue: null,
75+
},
6176
options: jsonDataType("options", {}),
6277
input: jsonDataType("input", []),
6378
header_content: DataTypes.STRING,
64-
files_count: DataTypes.INTEGER,
79+
files_count: {
80+
type: DataTypes.INTEGER,
81+
defaultValue: 0,
82+
},
6583
output_format_version: {
6684
type: DataTypes.STRING,
6785
defaultValue: null,

src/services/workbenchDB.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,12 @@ export class WorkbenchDB {
282282
fileList.forEach((file) => {
283283
const fileParentPath = file.getDataValue("parent");
284284
const fileNode = pathToNodeMap.get(file.getDataValue("path"));
285-
if (Number(file.getDataValue("id")) !== 0) {
285+
if (file.getDataValue("parent") === "#") {
286+
roots.push(fileNode);
287+
} else {
286288
if (pathToNodeMap.has(fileParentPath)) {
287289
pathToNodeMap.get(fileParentPath).children?.push(fileNode);
288290
}
289-
} else {
290-
roots.push(fileNode);
291291
}
292292
});
293293

@@ -401,6 +401,7 @@ export class WorkbenchDB {
401401
this.pause();
402402

403403
promiseChain = promiseChain
404+
.then(() => this._imputeMissingIntermediateDirectories(files))
404405
.then(() => primaryPromise._batchCreateFiles(files))
405406
.then(() => {
406407
const currentProgress = Math.round(
@@ -427,7 +428,8 @@ export class WorkbenchDB {
427428
// See https://github.com/nexB/scancode-toolkit/issues/543
428429
promiseChain
429430
.then(() => {
430-
if (rootPath && !hasRootPath) {
431+
if (!hasRootPath) {
432+
rootPath = rootPath || "no-files";
431433
files.push({
432434
path: rootPath,
433435
name: rootPath,
@@ -436,6 +438,7 @@ export class WorkbenchDB {
436438
});
437439
}
438440
})
441+
.then(() => this._imputeIntermediateDirectories(files))
439442
.then(() => this._batchCreateFiles(files))
440443
.then(() => this.db.Header.create(TopLevelData.parsedHeader))
441444
.then(() => {
@@ -600,7 +603,7 @@ export class WorkbenchDB {
600603
duration: header.duration,
601604
options: header?.options || {},
602605
input,
603-
files_count: header.extra_data?.files_count,
606+
files_count: header.extra_data?.files_count || 0,
604607
output_format_version: header.output_format_version,
605608
spdx_license_list_version: header.extra_data?.spdx_license_list_version,
606609
operating_system: header.extra_data?.system_environment?.operating_system,
@@ -880,6 +883,37 @@ export class WorkbenchDB {
880883
});
881884
}
882885

886+
// Adds & modifies files array in place, adding missing intermediate directories
887+
_imputeIntermediateDirectories(files: Resource[]) {
888+
const availableFiles = new Set(files.map((file) => file.path));
889+
const intermediateDirectories: Resource[] = [];
890+
891+
files.forEach((file) => {
892+
file.parent = parentPath(file.path);
893+
894+
// Add intermediate directories if parent not available in files
895+
if (!availableFiles.has(file.parent)) {
896+
for (
897+
let currentDir = file.parent;
898+
currentDir !== parentPath(currentDir) &&
899+
!availableFiles.has(currentDir);
900+
currentDir = parentPath(currentDir)
901+
) {
902+
availableFiles.add(currentDir);
903+
intermediateDirectories.push({
904+
path: currentDir,
905+
parent: parentPath(currentDir),
906+
name: path.basename(currentDir),
907+
type: "directory",
908+
files_count: 0,
909+
});
910+
}
911+
availableFiles.add(file.parent);
912+
}
913+
});
914+
files.push(...intermediateDirectories);
915+
}
916+
883917
_batchCreateFiles(files: Resource[]) {
884918
// Add batched files to the DB
885919
return this._addFlattenedFiles(files).then(() => this._addFiles(files));

src/styles/toastify.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.Toastify__toast-body {
2+
white-space: pre;
3+
width: fit-content;
4+
padding-right: 10px;
5+
}
6+
7+
.Toastify__toast-container,
8+
.Toastify__toast-body>div:last-child {
9+
width: max-content;
10+
}

0 commit comments

Comments
 (0)