From aaabc3ad889d1cb4427c7d347c03863ae0b77bb7 Mon Sep 17 00:00:00 2001 From: Willie Date: Thu, 9 Apr 2026 16:23:01 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20simplify=20audit=20code=20=E2=80=94?= =?UTF-8?q?=20constants,=20dedup=20helpers,=20cache=20pattern?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add audit.CrossSkillResultName constant, use in Go + TS - Merge toAuditInputs/toAgentAuditInputs into one with isFile param - Extract discoverForKind() and toInputsForKind() to eliminate 5+ if-else branches - Simplify launchAuditTUIWithTabs with otherKindFilter dispatch - Use CROSS_SKILL_NAME constant in AuditPage.tsx --- cmd/skillshare/audit.go | 41 ++++++++++++++-------------------- cmd/skillshare/audit_render.go | 23 +++++-------------- internal/audit/crossskill.go | 5 ++++- ui/src/pages/AuditPage.tsx | 4 +++- 4 files changed, 30 insertions(+), 43 deletions(-) diff --git a/cmd/skillshare/audit.go b/cmd/skillshare/audit.go index 61b8630e8..efabdd17c 100644 --- a/cmd/skillshare/audit.go +++ b/cmd/skillshare/audit.go @@ -477,18 +477,10 @@ func scanSkillPath(skillPath, projectRoot string, registry *audit.Registry) (*au return audit.ScanSkill(skillPath) } -func toAuditInputs(skills []auditSkillRef) []audit.SkillInput { - inputs := make([]audit.SkillInput, len(skills)) - for i, s := range skills { - inputs[i] = audit.SkillInput{Name: s.name, Path: s.path} - } - return inputs -} - -func toAgentAuditInputs(agents []auditSkillRef) []audit.SkillInput { - inputs := make([]audit.SkillInput, len(agents)) - for i, a := range agents { - inputs[i] = audit.SkillInput{Name: a.name, Path: a.path, IsFile: true} +func toAuditInputs(items []auditSkillRef, isFile bool) []audit.SkillInput { + inputs := make([]audit.SkillInput, len(items)) + for i, item := range items { + inputs[i] = audit.SkillInput{Name: item.name, Path: item.path, IsFile: isFile} } return inputs } @@ -512,6 +504,17 @@ func collectInstalledAgentPaths(agentsSourcePath string) ([]auditSkillRef, error return agentPaths, nil } +func discoverForKind(kind resourceKindFilter, sourcePath string) ([]auditSkillRef, error) { + if kind == kindAgents { + return collectInstalledAgentPaths(sourcePath) + } + return collectInstalledSkillPaths(sourcePath) +} + +func toInputsForKind(kind resourceKindFilter, items []auditSkillRef) []audit.SkillInput { + return toAuditInputs(items, kind == kindAgents) +} + func scanPathTarget(targetPath, projectRoot string, registry *audit.Registry) (*audit.Result, error) { info, err := os.Stat(targetPath) if err != nil { @@ -596,12 +599,7 @@ func auditInstalled(sourcePath, agentsSourcePath, mode, projectRoot, threshold s progressBar.Increment() } } - var scanInputs []audit.SkillInput - if kind == kindAgents { - scanInputs = toAgentAuditInputs(skillPaths) - } else { - scanInputs = toAuditInputs(skillPaths) - } + scanInputs := toInputsForKind(kind, skillPaths) scanResults := audit.ParallelScan(scanInputs, projectRoot, onDone, reg) if progressBar != nil { progressBar.Stop() @@ -755,12 +753,7 @@ func auditFiltered(sourcePath, agentsSourcePath string, names, groups []string, progressBar.Increment() } } - var scanInputs []audit.SkillInput - if kind == kindAgents { - scanInputs = toAgentAuditInputs(matched) - } else { - scanInputs = toAuditInputs(matched) - } + scanInputs := toInputsForKind(kind, matched) scanResults := audit.ParallelScan(scanInputs, projectRoot, onDone, reg) if progressBar != nil { progressBar.Stop() diff --git a/cmd/skillshare/audit_render.go b/cmd/skillshare/audit_render.go index 1ec224ed4..de072be61 100644 --- a/cmd/skillshare/audit_render.go +++ b/cmd/skillshare/audit_render.go @@ -77,27 +77,16 @@ func launchAuditTUIWithTabs(results []*audit.Result, scanOutputs []audit.ScanOut var otherOutputs []audit.ScanOutput var otherSummary auditRunSummary + otherKindFilter := kindAgents otherSource := ctx.agentsSourcePath - otherKind := "agent" if ctx.kind == kindAgents { + otherKindFilter = kindSkills otherSource = ctx.sourcePath - otherKind = "skill" } if otherSource != "" { - var otherPaths []auditSkillRef - var err error - if otherKind == "agent" { - otherPaths, err = collectInstalledAgentPaths(otherSource) - } else { - otherPaths, err = collectInstalledSkillPaths(otherSource) - } - var otherInputs []audit.SkillInput - if otherKind == "agent" { - otherInputs = toAgentAuditInputs(otherPaths) - } else { - otherInputs = toAuditInputs(otherPaths) - } + otherPaths, err := discoverForKind(otherKindFilter, otherSource) + otherInputs := toInputsForKind(otherKindFilter, otherPaths) if err == nil && len(otherPaths) > 0 { otherScanResults := audit.ParallelScan(otherInputs, ctx.projectRoot, nil, ctx.registry) for i := range otherPaths { @@ -106,7 +95,7 @@ func launchAuditTUIWithTabs(results []*audit.Result, scanOutputs []audit.ScanOut if sr.Err == nil { sr.Result.Threshold = ctx.threshold sr.Result.IsBlocked = sr.Result.HasSeverityAtOrAbove(ctx.threshold) - sr.Result.Kind = otherKind + sr.Result.Kind = otherKindFilter.SingularNoun() if rel, relErr := filepath.Rel(otherSource, sr.Result.ScanTarget); relErr == nil { sr.Result.SkillName = rel } @@ -124,7 +113,7 @@ func launchAuditTUIWithTabs(results []*audit.Result, scanOutputs []audit.ScanOut var filteredResults []*audit.Result var filteredOutputs []audit.ScanOutput for i, r := range results { - if r.SkillName != "_cross-skill" { + if r.SkillName != audit.CrossSkillResultName { filteredResults = append(filteredResults, r) if i < len(scanOutputs) { filteredOutputs = append(filteredOutputs, scanOutputs[i]) diff --git a/internal/audit/crossskill.go b/internal/audit/crossskill.go index 938d70d41..a8ddb59ed 100644 --- a/internal/audit/crossskill.go +++ b/internal/audit/crossskill.go @@ -6,6 +6,9 @@ import ( "strings" ) +// CrossSkillResultName is the synthetic skill name used for cross-skill analysis results. +const CrossSkillResultName = "_cross-skill" + // skillCapability summarises the security-relevant capabilities of a single skill, // derived entirely from its existing Result (TierProfile + Findings). type skillCapability struct { @@ -134,7 +137,7 @@ func CrossSkillAnalysis(results []*Result) *Result { } r := &Result{ - SkillName: "_cross-skill", + SkillName: CrossSkillResultName, Findings: findings, Analyzability: 1.0, } diff --git a/ui/src/pages/AuditPage.tsx b/ui/src/pages/AuditPage.tsx index 4e207f958..b4eb20027 100644 --- a/ui/src/pages/AuditPage.tsx +++ b/ui/src/pages/AuditPage.tsx @@ -35,6 +35,8 @@ import { queryKeys, staleTimes } from '../lib/queryKeys'; type SeverityFilter = 'CRITICAL' | 'HIGH' | 'MEDIUM' | 'LOW' | 'INFO'; type AuditKind = 'skills' | 'agents'; +const CROSS_SKILL_NAME = '_cross-skill'; + const severityFilterOptions: { value: SeverityFilter; label: string }[] = [ { value: 'INFO', label: 'All (INFO+)' }, { value: 'LOW', label: 'LOW+' }, @@ -79,7 +81,7 @@ export default function AuditPage() { // Cross-skill analysis is a derived insight, not an actual scanned resource. const realResults = useMemo(() => { if (!data) return []; - return data.results.filter((r) => r.skillName !== '_cross-skill'); + return data.results.filter((r) => r.skillName !== CROSS_SKILL_NAME); }, [data]); const totalFindings = useMemo(() => {