Skip to content

Commit

Permalink
fix: importing models with deleted components should no longer crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Tethik committed Nov 14, 2023
1 parent 25f7654 commit f5a2681
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
8 changes: 8 additions & 0 deletions core/src/data/models/ModelDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ export class ModelDataService extends EventEmitter {
await this.pool.runTransaction(async (client) => {
for (const threat of threats) {
uuid.set(threat.id!, randomUUID());
if (!uuid.get(threat.componentId)) {
// skip, component no longer exists
continue;
}
await client.query(queryThreats, [
uuid.get(threat.id!),
uuid.get(srcModel.id!),
Expand All @@ -307,6 +311,10 @@ export class ModelDataService extends EventEmitter {

for (const control of controls) {
uuid.set(control.id!, randomUUID());
if (!uuid.get(control.componentId)) {
// skip, component no longer exists
continue;
}
await client.query(queryControls, [
uuid.get(control.id!),
uuid.get(srcModel.id!),
Expand Down
28 changes: 19 additions & 9 deletions core/src/data/suggestions/SuggestionDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,35 @@ export class SuggestionDataService extends EventEmitter {
return;
}

await this.bulkInsert(toModelId, {
threats: threatSuggestions.map((ts) => ({
const threats = threatSuggestions
.map((ts) => ({
...ts,
id: new SuggestionID(
uuidMap.get(ts.componentId) + "/" + ts.id.partialId
),
componentId: uuidMap.get(ts.componentId) as string,
modelId: toModelId,
slug: ts.id.partialId,
})),
controls: controlSuggestions.map((cs) => ({
}))
.filter((ts) => ts.componentId); // If componentId is null then the component this was suggested for may no longer exist

const controls = controlSuggestions
.map((cs) => ({
...cs,
id: new SuggestionID(
uuidMap.get(cs.componentId) + "/" + cs.id.partialId
),
componentId: uuidMap.get(cs.componentId) as string,
modelId: toModelId,
slug: cs.id.partialId,
})),
}))
.filter((cs) => cs.componentId); // If componentId is null then the component this was suggested for may no longer exist

console.log("copy", controls);

await this.bulkInsert(toModelId, {
threats,
controls,
});
}

Expand Down Expand Up @@ -147,8 +157,8 @@ export class SuggestionDataService extends EventEmitter {

let bulkThreats: Promise<QueryResult<any>>[] = [];
if (suggestions.threats.length > 0) {
bulkThreats = suggestions.threats.map((threat) =>
client.query(threatQuery, [
bulkThreats = suggestions.threats.map((threat) => {
return client.query(threatQuery, [
threat.id.val,
modelId,
threat.status || SuggestionStatus.New,
Expand All @@ -157,8 +167,8 @@ export class SuggestionDataService extends EventEmitter {
threat.description,
threat.reason,
threat.source,
])
);
]);
});
}

let bulkControls: Promise<QueryResult<any>>[] = [];
Expand Down

0 comments on commit f5a2681

Please sign in to comment.