Skip to content

Commit 83ee678

Browse files
committed
Abort imports for scans with no resources
Signed-off-by: Omkar Phansopkar <[email protected]>
1 parent 8095c99 commit 83ee678

File tree

12 files changed

+84
-74
lines changed

12 files changed

+84
-74
lines changed

src/constants/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const NO_RESOURCES_ERROR = "No files found in the scan"

src/contexts/dbContext.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
UTIL_CHANNEL,
2121
} from "../constants/IpcConnection";
2222
import { DEFAULT_ROUTE_ON_IMPORT, ROUTES } from "../constants/routes";
23+
import { NO_RESOURCES_ERROR } from "../constants/errors";
2324
import { AddEntry, GetHistory, RemoveEntry } from "../services/historyStore";
2425
import { WorkbenchDB } from "../services/workbenchDB";
2526
import { isSqliteSchemaOutdated } from "../utils/checks";
@@ -363,15 +364,19 @@ The SQLite file is invalid. Try re-importing the ScanCode JSON file and creating
363364
);
364365
});
365366
})
366-
.catch((err) => {
367+
.catch((err: Error) => {
367368
abortImport();
368-
console.error(
369-
"Some error parsing json data (caught in dbContext)",
370-
err
371-
);
372-
toast.error(
373-
"Some error parsing json data !! \nPlease check console for more info"
374-
);
369+
if (err.message === NO_RESOURCES_ERROR) {
370+
toast.error("No resources found in scan\nAborting import");
371+
} else {
372+
console.error(
373+
"Some error parsing json data (caught in dbContext)",
374+
err
375+
);
376+
toast.error(
377+
"Some error parsing json data !! \nPlease check console for more info"
378+
);
379+
}
375380
});
376381
}
377382

src/services/workbenchDB.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import { TodoAttributes } from "./models/todo";
5555
import { HeaderAttributes } from "./models/header";
5656
import { LicenseReferenceAttributes } from "./models/licenseReference";
5757
import { LicenseRuleReferenceAttributes } from "./models/licenseRuleReference";
58+
import { NO_RESOURCES_ERROR } from "../constants/errors";
5859

5960
const { version: workbenchVersion } = packageJson;
6061

@@ -309,8 +310,6 @@ export class WorkbenchDB {
309310
const stream = fs.createReadStream(jsonFilePath, { encoding: "utf8" });
310311
let files_count = 0;
311312
let dirs_count = 0;
312-
let rootPath: string | null = null;
313-
let hasRootPath = false;
314313
const batchSize = 1000;
315314
let files: Resource[] = [];
316315
const parsedFilePaths = new Set<string>();
@@ -380,12 +379,6 @@ export class WorkbenchDB {
380379
.on("data", function (file?: Resource) {
381380
if (!file) return;
382381

383-
if (!rootPath) {
384-
rootPath = file.path.split("/")[0];
385-
}
386-
if (rootPath === file.path) {
387-
hasRootPath = true;
388-
}
389382
// @TODO: When/if scancode reports directories in its header, this needs
390383
// to be replaced.
391384
if (parsedFilePaths.size === 0) {
@@ -435,21 +428,14 @@ export class WorkbenchDB {
435428
// Add root directory into data
436429
// See https://github.com/nexB/scancode-toolkit/issues/543
437430
promiseChain
438-
.then(() => {
439-
if (!hasRootPath) {
440-
rootPath = rootPath || "no-files";
441-
files.push({
442-
path: rootPath,
443-
name: rootPath,
444-
type: "directory",
445-
files_count: files_count,
446-
});
447-
parsedFilePaths.add(rootPath);
448-
}
449-
})
450431
.then(() =>
451432
this._imputeIntermediateDirectories(files, parsedFilePaths)
452433
)
434+
.then(() => {
435+
if (files.length === 0) {
436+
throw new Error(NO_RESOURCES_ERROR);
437+
}
438+
})
453439
.then(() => this._batchCreateFiles(files))
454440
.then(() => this.db.Header.create(TopLevelData.parsedHeader))
455441
.then(() => {
@@ -470,10 +456,12 @@ export class WorkbenchDB {
470456
.then(() => {
471457
onProgressUpdate(100);
472458
console.info("JSON parse completed (final step)");
473-
console.timeEnd("json-parse-time");
474459
resolve();
475460
})
476-
.catch((e: unknown) => reject(e));
461+
.catch((e: unknown) => reject(e))
462+
.finally(() => {
463+
console.timeEnd("json-parse-time");
464+
});
477465
})
478466
.on("error", (err: unknown) => {
479467
console.error(

tests/test-scans/fileTree/empty.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/test-scans/fileTree/expectedFileTree.ts

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,25 @@ export const FileTreeSamples: {
4444
},
4545
{
4646
id: 4,
47-
name: "samples",
48-
parent: "#",
49-
path: "samples",
50-
type: "directory",
51-
},
52-
{
53-
id: 5,
5447
name: "licenses",
5548
parent: "samples/JGroups",
5649
path: "samples/JGroups/licenses",
5750
type: "directory",
5851
},
5952
{
60-
id: 6,
53+
id: 5,
6154
name: "JGroups",
6255
parent: "samples",
6356
path: "samples/JGroups",
6457
type: "directory",
6558
},
59+
{
60+
id: 6,
61+
name: "samples",
62+
parent: "#",
63+
path: "samples",
64+
type: "directory",
65+
},
6666
{
6767
id: 7,
6868
name: "src",
@@ -96,7 +96,7 @@ export const FileTreeSamples: {
9696
type: "file",
9797
},
9898
],
99-
id: 5,
99+
id: 4,
100100
isLeaf: false,
101101
key: "samples/JGroups/licenses",
102102
name: "licenses",
@@ -138,7 +138,7 @@ export const FileTreeSamples: {
138138
type: "directory",
139139
},
140140
],
141-
id: 6,
141+
id: 5,
142142
isLeaf: false,
143143
key: "samples/JGroups",
144144
name: "JGroups",
@@ -170,7 +170,7 @@ export const FileTreeSamples: {
170170
type: "directory",
171171
},
172172
],
173-
id: 4,
173+
id: 6,
174174
isLeaf: false,
175175
key: "samples",
176176
name: "samples",
@@ -398,29 +398,4 @@ export const FileTreeSamples: {
398398
},
399399
],
400400
},
401-
{
402-
jsonFileName: "empty.json",
403-
flatData: [
404-
{
405-
id: 1,
406-
name: "no-files",
407-
path: "no-files",
408-
parent: "#",
409-
type: "directory",
410-
},
411-
],
412-
fileTree: [
413-
{
414-
id: 1,
415-
key: "no-files",
416-
name: "no-files",
417-
parent: "#",
418-
path: "no-files",
419-
type: "directory",
420-
isLeaf: false,
421-
title: "no-files",
422-
children: [],
423-
},
424-
],
425-
},
426401
];
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{
2-
"NOTE": "Test for headerless scans"
2+
"files": [
3+
{
4+
"path": "postgresml-2.8.1"
5+
}
6+
]
37
}

tests/test-scans/headers/minimal.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@
1919
"spdx_license_list_version": "3.20"
2020
}
2121
}
22+
],
23+
"files": [
24+
{
25+
"path": "postgresml-2.8.1"
26+
}
2227
]
2328
}

tests/test-scans/headers/withOptions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,10 @@
3232
"files_count": 39
3333
}
3434
}
35+
],
36+
"files": [
37+
{
38+
"path": "postgresml-2.8.1"
39+
}
3540
]
3641
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
{
22
"files": [
33
{
4-
"path": "manifests"
4+
"path": "manifests",
5+
"type": "directory"
56
},
67
{
7-
"path": "manifests/copyright"
8+
"path": "manifests/copyright",
9+
"type": "file"
810
},
911
{
10-
"path": "manifests/FirebaseAnalytics.podspec.json"
12+
"path": "manifests/FirebaseAnalytics.podspec.json",
13+
"type": "file"
1114
}
1215
]
1316
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export const SanitySamples: { jsonFileName: string }[] = [
2-
{ jsonFileName: "empty.json" },
32
{ jsonFileName: "minimal.json" },
43
];
4+
5+
export const NoResourceSamples: { jsonFileName: string }[] = [
6+
{ jsonFileName: "empty.json" },
7+
];

0 commit comments

Comments
 (0)