Skip to content

Conversation

@tankerkiller125
Copy link
Contributor

@tankerkiller125 tankerkiller125 commented Jan 14, 2026

What type of PR is this?

  • feature

What this PR does / why we need it:

Automatically sets the import ref field for items on creation. Eliminating the need to run the ensure refs tool every single time before export.

Which issue(s) this PR fixes:

Fixes: #793

Summary by CodeRabbit

  • New Features
    • Items now automatically receive an 8-character import reference identifier upon creation, improving tracking and organization of newly created items.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 14, 2026

Walkthrough

Assigns an 8-character ImportRef to newly created inventory items during Create by calling SetImportRef(uuid.New().String()[0:8]) after AssetID assignment.

Changes

Cohort / File(s) Summary
ImportRef Assignment
backend/internal/data/repo/repo_items.go
Added SetImportRef(uuid.New().String()[0:8]) in Create to populate an 8-character ImportRef for new items.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Security Recommendations

  • Validate that using the first 8 characters of a UUID provides acceptable collision resistance for your import matching use case; consider longer prefixes if collision risk is non-trivial.
  • Ensure ImportRef is not relied upon as a primary unique identifier in other systems or DB constraints.
  • Confirm import/re-import logic tolerates collisions (e.g., falls back to other matching criteria) and logs or surfaces ambiguous matches for manual resolution.
  • Normalize and document the ImportRef format so exporters/importers remain consistent.

Poem

📦 A short eight-digit beacon now sits in the stream,
Exports remember themselves, returning like a dream.
No more ghosts in import lines, no items gone astray,
Small change, tidy loop — idempotence finds its way. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat: automatic import ref set' accurately describes the main change—automatically setting the import ref field for items on creation.
Description check ✅ Passed The PR description covers all required sections: type (feature), explanation of changes and rationale, and linked issue (#793).
Linked Issues check ✅ Passed The code change (automatically setting ImportRef via SetImportRef(uuid.New().String()[0:8]) in Create) directly addresses issue #793's requirement that items have HB.import_ref populated at creation.
Out of Scope Changes check ✅ Passed The change is minimal and focused: only adding ImportRef assignment in the Create method of repo_items.go, with no unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch mk/auto-import-ref

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
backend/internal/data/repo/repo_items.go (1)

631-639: Critical: data.ImportRef is unconditionally overwritten, breaking import idempotency.

Line 632 sets ImportRef from data.ImportRef, but line 639 immediately overwrites it with a newly generated value. This defeats the PR objective of supporting idempotent re-imports—when importing items with existing HB.import_ref values, the provided reference is discarded and replaced with a random one.

The auto-generation should only apply when no ImportRef is provided.

🐛 Proposed fix: conditionally generate ImportRef
 func (e *ItemsRepository) Create(ctx context.Context, gid uuid.UUID, data ItemCreate) (ItemOut, error) {
+	importRef := data.ImportRef
+	if importRef == "" {
+		importRef = uuid.New().String()[0:8]
+	}
+
 	q := e.db.Item.Create().
-		SetImportRef(data.ImportRef).
+		SetImportRef(importRef).
 		SetName(data.Name).
 		SetQuantity(data.Quantity).
 		SetDescription(data.Description).
 		SetGroupID(gid).
 		SetLocationID(data.LocationID).
-		SetAssetID(int(data.AssetID)).
-		SetImportRef(uuid.New().String()[0:8])
+		SetAssetID(int(data.AssetID))
🧹 Nitpick comments (2)
backend/internal/data/repo/repo_items.go (2)

697-709: Consider adding auto-generated ImportRef to CreateFromTemplate for consistency.

The Create method now auto-generates an ImportRef, but CreateFromTemplate does not set one. This inconsistency means items created from templates will have empty ImportRef values and won't be exportable with proper references until the "ensure refs" tool is run.

♻️ Suggested addition
 	itemBuilder := tx.Item.Create().
 		SetID(newItemID).
 		SetName(data.Name).
 		SetDescription(data.Description).
 		SetQuantity(data.Quantity).
 		SetLocationID(data.LocationID).
 		SetGroupID(gid).
 		SetAssetID(int(nextAssetID)).
+		SetImportRef(uuid.New().String()[0:8]).
 		SetInsured(data.Insured).

1376-1400: Same consistency issue: Duplicate should also auto-generate ImportRef.

Duplicated items will not have an ImportRef set, creating the same export/import gap addressed by this PR for the standard Create path.

♻️ Suggested addition
 	itemBuilder := tx.Item.Create().
 		SetID(newItemID).
 		SetName(options.CopyPrefix + originalItem.Name).
 		SetDescription(originalItem.Description).
 		SetQuantity(originalItem.Quantity).
 		SetLocationID(originalItem.Location.ID).
 		SetGroupID(gid).
 		SetAssetID(int(nextAssetID)).
+		SetImportRef(uuid.New().String()[0:8]).
 		SetSerialNumber(originalItem.SerialNumber).
📜 Review details

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 342497e and 610e8a4.

📒 Files selected for processing (1)
  • backend/internal/data/repo/repo_items.go
🧰 Additional context used
📓 Path-based instructions (1)
backend/**/*.go

📄 CodeRabbit inference engine (.github/instructions/code.instructions.md)

Backend Go code must pass golangci-lint validation with no errors (6-minute timeout in CI)

Files:

  • backend/internal/data/repo/repo_items.go
🧬 Code graph analysis (1)
backend/internal/data/repo/repo_items.go (1)
backend/internal/data/repo/asset_id_type.go (1)
  • AssetID (9-9)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (17)
  • GitHub Check: build (linux/amd64, ubuntu-latest)
  • GitHub Check: build (linux/arm64, ubuntu-24.04-arm)
  • GitHub Check: Frontend Tests / Integration Tests PGSQL 16
  • GitHub Check: Frontend Tests / Integration Tests PGSQL 17
  • GitHub Check: End-to-End Playwright Tests / E2E Playwright Testing 4/4
  • GitHub Check: Frontend Tests / Integration Tests PGSQL 15
  • GitHub Check: End-to-End Playwright Tests / E2E Playwright Testing 2/4
  • GitHub Check: End-to-End Playwright Tests / E2E Playwright Testing 3/4
  • GitHub Check: End-to-End Playwright Tests / E2E Playwright Testing 1/4
  • GitHub Check: Frontend Tests / Integration Tests
  • GitHub Check: Backend Server Tests / Go
  • GitHub Check: build (linux/amd64, ubuntu-latest)
  • GitHub Check: build (linux/amd64, ubuntu-latest)
  • GitHub Check: build (linux/arm64, ubuntu-24.04-arm)
  • GitHub Check: build (linux/arm64, ubuntu-24.04-arm)
  • GitHub Check: Analyze (go)
  • GitHub Check: Analyze (go)
🔇 Additional comments (1)
backend/internal/data/repo/repo_items.go (1)

638-639: The codebase already handles collision checking where needed; 8-character UUID truncation is acceptable for import reference idempotency.

The CheckRef() method already exists (line 351) and is actively used during CSV import to prevent duplicate items with the same ImportRef. The 8-character truncation reduces entropy to ~32 bits, making collisions theoretically possible at scale (≈65k+ items), but this is a non-critical, non-security identifier used solely for import idempotency, so the risk is acceptable for typical inventory systems.

@cloudflare-workers-and-pages
Copy link

Deploying homebox-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: d17ffe1
Status:🚫  Build failed.

View logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Export Inventory does not set HB.import_ref

1 participant