feat: setup uses cache by default; replace stale field mappings on re-run#29
Conversation
…-run - Add --use-cache (default true) and --cache-dir to setup command. Purchase sources are read from devices.json instead of fetching all ABM devices. ABM client is skipped entirely when --use-cache is set. - MergeFieldMapping now accepts replaceValues: existing entries whose ABM attribute value is in that set are removed before writing fresh ones, preventing stale field IDs from accumulating across setup runs. - Add GetPurchaseSourcesFromCache to abmclient for cache-based lookup. - Add TestMergeFieldMapping_ReplaceStaleIDs test. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughAdds cache-aware setup flow: new cache reader for purchase sources; refactors ABM purchase-source extraction into a helper; setup gains a --use-cache flag to skip ABM and load cached devices; MergeFieldMapping gains replaceValues to remove stale mappings; SetupFields short-circuits on dry-run. Changes
Sequence Diagram(s)sequenceDiagram
participant User as User
participant Setup as Setup Cmd
participant Cache as Cache Dir
participant ABM as ABM Client
participant Config as Config Manager
alt use-cache flag enabled
User->>Setup: run setup --use-cache
Setup->>Cache: read devices.json
Cache-->>Setup: cached device list
Setup->>Setup: collectPurchaseSources(cached devices)
Setup->>Config: MergeFieldMapping(..., replaceValues)
Config-->>User: setup complete (cache)
else use-cache flag disabled
User->>Setup: run setup
Setup->>ABM: init client
ABM-->>Setup: client ready
Setup->>ABM: fetch MDM servers & purchase sources
ABM-->>Setup: ABM data
Setup->>Config: MergeFieldMapping(..., nil)
Config-->>User: setup complete (ABM)
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
abmclient/client.go (1)
241-269: Usefilepath.Joinfor cross-platform path handling.Line 244 uses string concatenation (
cacheDir + "/devices.json") which may cause issues on Windows where the path separator is\. Consider usingfilepath.Joinfor portability.♻️ Proposed fix
import ( "context" "encoding/json" "fmt" "io" "os" + "path/filepath" "time" ... func GetPurchaseSourcesFromCache(cacheDir string) ([]PurchaseSource, error) { - data, err := os.ReadFile(cacheDir + "/devices.json") + data, err := os.ReadFile(filepath.Join(cacheDir, "devices.json")) if err != nil { return nil, fmt.Errorf("reading devices cache: %w", err) }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@abmclient/client.go` around lines 241 - 269, GetPurchaseSourcesFromCache currently builds the devices.json path with string concatenation (cacheDir + "/devices.json") which breaks on Windows; replace that with filepath.Join(cacheDir, "devices.json") where os.ReadFile is called and add the "path/filepath" import to the file (or ensure it's present) so path construction is cross-platform compatible.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@cmd/setup.go`:
- Around line 127-139: SetupFields currently calls the upstream library
unconditionally and ignores the client's dry-run flag; add an early guard at the
top of SetupFields to return nil, ErrDryRun when c.DryRun is true (matching
other write helpers in snipe/client.go) so no Create/Update/Associate API calls
are executed in dry-run mode, keeping behavior consistent with supplier_mapping
and other write methods.
---
Nitpick comments:
In `@abmclient/client.go`:
- Around line 241-269: GetPurchaseSourcesFromCache currently builds the
devices.json path with string concatenation (cacheDir + "/devices.json") which
breaks on Windows; replace that with filepath.Join(cacheDir, "devices.json")
where os.ReadFile is called and add the "path/filepath" import to the file (or
ensure it's present) so path construction is cross-platform compatible.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: cc79b576-82a4-405f-8550-f452dbd39c9b
📒 Files selected for processing (5)
abmclient/client.gocmd/root.gocmd/setup.goconfig/config.goconfig/config_test.go
…ard to SetupFields Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
🧹 Nitpick comments (1)
abmclient/client.go (1)
253-269: Extract the purchase-source collection loop into one helper.This block now duplicates the filtering/deduplication logic in
Client.GetAllPurchaseSources. Sincesetupcan now source purchase sources from either live ABM data ordevices.json, keeping the two paths separate makes it easy for future normalization fixes to land in only one branch and produce different scaffolding.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@abmclient/client.go` around lines 253 - 269, Extract the duplicated filtering/deduplication loop into a single helper function (e.g., collectPurchaseSourcesFromDevices) that accepts the devices slice and returns ([]PurchaseSource, error) or ([]PurchaseSource) matching the current behavior; replace the duplicated loop in Client.GetAllPurchaseSources and in setup to call this helper instead of repeating the logic, ensuring it handles nil Attributes, empty PurchaseSourceType, constructs PurchaseSource{Type: string(d.Attributes.PurchaseSourceType), ID: d.Attributes.PurchaseSourceID}, and deduplicates using a seen map keyed by PurchaseSource so behavior of PurchaseSource, Client.GetAllPurchaseSources, and setup remains unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@abmclient/client.go`:
- Around line 253-269: Extract the duplicated filtering/deduplication loop into
a single helper function (e.g., collectPurchaseSourcesFromDevices) that accepts
the devices slice and returns ([]PurchaseSource, error) or ([]PurchaseSource)
matching the current behavior; replace the duplicated loop in
Client.GetAllPurchaseSources and in setup to call this helper instead of
repeating the logic, ensuring it handles nil Attributes, empty
PurchaseSourceType, constructs PurchaseSource{Type:
string(d.Attributes.PurchaseSourceType), ID: d.Attributes.PurchaseSourceID}, and
deduplicates using a seen map keyed by PurchaseSource so behavior of
PurchaseSource, Client.GetAllPurchaseSources, and setup remains unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: e9e58a1e-4a3f-4d4c-a678-1edd3fa2b524
📒 Files selected for processing (2)
abmclient/client.gosnipe/client.go
Deduplicate the purchase source iteration logic into a shared helper that accepts []abm.OrgDevice. GetPurchaseSourcesFromCache unwraps []Device to []abm.OrgDevice before calling it. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
🧹 Nitpick comments (1)
abmclient/client.go (1)
246-262: Move the cache reader out ofclient.go.
GetPurchaseSourcesFromCacheadds filesystem/JSON cache handling to the ABM client wrapper file. A smallabmclient/cache.go-style home would keepclient.gofocused on upstream-client behavior and make the cache path easier to evolve independently.As per coding guidelines, "Organize API client wrappers in dedicated files that wrap upstream libraries with project-specific logic (e.g., dry-run enforcement) in
abmclient/client.goandsnipe/client.go."🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@abmclient/client.go` around lines 246 - 262, GetPurchaseSourcesFromCache contains filesystem/JSON cache handling that should be relocated out of client.go; create a new abmclient/cache.go in package abmclient and move the GetPurchaseSourcesFromCache implementation there (preserving its signature and behavior: reading devices.json, unmarshalling into []Device, converting to []abm.OrgDevice and calling collectPurchaseSources), update imports if needed, and ensure any callers of GetPurchaseSourcesFromCache or references to Device/collectPurchaseSources continue to compile; keep client.go focused on upstream-client behavior and run tests to confirm no API/behavior changes.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@abmclient/client.go`:
- Around line 246-262: GetPurchaseSourcesFromCache contains filesystem/JSON
cache handling that should be relocated out of client.go; create a new
abmclient/cache.go in package abmclient and move the GetPurchaseSourcesFromCache
implementation there (preserving its signature and behavior: reading
devices.json, unmarshalling into []Device, converting to []abm.OrgDevice and
calling collectPurchaseSources), update imports if needed, and ensure any
callers of GetPurchaseSourcesFromCache or references to
Device/collectPurchaseSources continue to compile; keep client.go focused on
upstream-client behavior and run tests to confirm no API/behavior changes.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 283ae06b-fcad-4237-8803-b1a4a8aa1fb4
📒 Files selected for processing (1)
abmclient/client.go
Separate filesystem/JSON cache concerns from upstream-client behavior by relocating GetPurchaseSourcesFromCache into a dedicated cache.go file. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Summary
setupnow has--use-cache(defaulttrue) and--cache-dirflags. Purchase sources are read fromdevices.jsoninstead of fetching all ABM devices on every run. ABM client is skipped entirely when using cache (no credentials needed for purchase source discovery).MergeFieldMappingnow accepts areplaceValuesset — existingfield_mappingentries whose ABM attribute value is in that set are removed before writing fresh ones. This prevents stale_snipeit_*_Nfield IDs from accumulating whensetupis re-run after fields are recreated.GetPurchaseSourcesFromCachetoabmclientfor reading purchase sources fromdevices.json.TestMergeFieldMapping_ReplaceStaleIDstest.Test plan
go test ./...passesaxm2snipe setup -v(with existing cache) — completes without ABM call, updates field mappings, removes old IDsaxm2snipe setup -v --use-cache=false— fetches from ABM as before🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Bug Fixes
Tests