feat(findings): add one-click cleanup for historical findings

- Add DB `DeleteFindings` bulk deletion inside `WithEvidenceTx`, locking affected tasks/findings, cascading retests/cases, and removing originating `exploration_nodes` (kind='finding').
- Add `DELETE /api/exploration/findings` endpoint supporting `scope` (`all`, `filtered`, `selected`) via JSON body or query params with strict validation and 409 on archive-state conflicts.
- Add `api.cleanupFindings`, mock handler support, bilingual `findingsCleanup.*` i18n keys, and a one-click cleanup button + confirmation dialog on the Findings page refreshing all four views.
- Add Go DB/server tests (`TestDeleteFindings`, `TestDeleteFindingsValidation`, `TestDeleteFindingsEndpoint`).

Fixes #157
This commit is contained in:
ji ruo committed 2026-11-23 18:20:33 +08:00
1 parent 7b196bf683
commit e196a06aa9
9 files changed
+755 -3

No files matched your search

+44
View File
@@ -483,6 +483,10 @@ function mockFilterFindings(q: URLSearchParams): (typeof mockFindings)[number][]
if (vulnclass) list = list.filter((finding) => finding.vulnclass === vulnclass);
if (taskID === "__unassigned__") list = list.filter((finding) => !finding.task_id);
else if (taskID) list = list.filter((finding) => finding.task_id === taskID);
const fromTs = q.get("from") ? Date.parse(q.get("from") as string) : Number.NaN;
const toTs = q.get("to") ? Date.parse(q.get("to") as string) : Number.NaN;
if (Number.isFinite(fromTs)) list = list.filter((finding) => Date.parse(finding.ts) >= fromTs);
if (Number.isFinite(toTs)) list = list.filter((finding) => Date.parse(finding.ts) <= toTs);
return list;
}
@@ -2317,6 +2321,11 @@ function route(m: string, path: string, seg: string[], q: URLSearchParams, b: Re
if (seg[0] === "exploration" && seg[1] === "findings" && seg.length === 3 && seg[2] !== "stats") {
const f = mockFindings.find((x) => x.id === seg[2]);
if (!f) return {};
if (m === "DELETE") {
const idx = mockFindings.findIndex((x) => x.id === seg[2]);
if (idx >= 0) mockFindings.splice(idx, 1);
return { deleted: idx >= 0, id: Number(seg[2]) || 0 };
}
if (m === "PATCH") {
if (typeof b.status === "string") f.status = b.status as typeof f.status;
if (typeof b.severity === "string") f.severity = b.severity as typeof f.severity;
@@ -2339,6 +2348,41 @@ function route(m: string, path: string, seg: string[], q: URLSearchParams, b: Re
};
}
if (path === "/exploration/findings") {
if (m === "DELETE") {
const scope = (typeof b.scope === "string" ? b.scope : q.get("scope"))?.trim() ?? "";
let targets: (typeof mockFindings)[number][] = [];
if (scope === "all") {
targets = [...mockFindings];
} else if (scope === "selected") {
const rawIds = Array.isArray(b.ids)
? b.ids.map((v) => String(v))
: (q.get("ids") ?? "")
.split(",")
.map((s) => s.trim())
.filter(Boolean);
const idSet = new Set(rawIds);
targets = mockFindings.filter((f) => idSet.has(f.id));
} else if (scope === "filtered") {
const params = new URLSearchParams(q);
if (b.filters && typeof b.filters === "object") {
for (const [k, v] of Object.entries(b.filters as Record<string, unknown>)) {
if (typeof v === "string" && v) params.set(k, v);
}
}
targets = mockApplyAssetScope(mockFilterFindings(params), params.get("asset_scope"));
} else {
throw new Error(`bad scope: ${scope}`);
}
const targetSet = new Set(targets.map((f) => f.id));
let deleted = 0;
for (let i = mockFindings.length - 1; i >= 0; i--) {
if (targetSet.has(mockFindings[i].id)) {
mockFindings.splice(i, 1);
deleted++;
}
}
return { deleted };
}
// finding_id=id:真后端用独立表行 id 作为状态/详情句柄,mock 里用自身 id 顶上。
// report 仅详情接口返回,列表剥掉(与后端一致)。
const withFid = (f: (typeof mockFindings)[number]) => ({