HYPERFLEET-1345 - fix: Address bugs from PR#288#310
Conversation
📝 WalkthroughWalkthroughThe DAO referencer lookup now returns all matching non-deleted resources. Resource deletion reports every inbound reference with kind and name details. Reference validation rejects mismatched or empty target kinds. Mocks, documentation, unit tests, and integration tests were updated for the new contract and behavior. Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant ResourceService
participant ResourceDao
participant SQLDatabase
participant APIClient
ResourceService->>ResourceDao: FindReferencers(targetID)
ResourceDao->>SQLDatabase: Query all matching non-deleted resources
SQLDatabase-->>ResourceDao: Referencer rows
ResourceDao-->>ResourceService: Resource summaries
ResourceService-->>APIClient: 409 Conflict listing referencers
Suggested reviewers: 🚥 Pre-merge checks | ✅ 9 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (9 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
✨ Simplify code
Comment |
Risk Score: 1 —
|
| Signal | Detail | Points |
|---|---|---|
| PR size | 84 lines | +0 |
| Sensitive paths | none | +0 |
| Test coverage | Missing tests for: pkg/api pkg/dao pkg/dao/mocks | +1 |
Computed by hyperfleet-risk-scorer
dece48d to
c914418
Compare
There was a problem hiding this comment.
Actionable comments posted: 4
🧹 Nitpick comments (2)
pkg/dao/resource.go (1)
199-200: 📐 Maintainability & Code Quality | 🔵 Trivial | 🏗️ Heavy liftSynchronize the architecture contract for plural referencers.
The linked architecture document still describes
FindReferencersas returning only the first referencer in lines 448-451 and 1067-1083. Update those comments and examples so future implementations do not reintroduceLIMIT(1).🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@pkg/dao/resource.go` around lines 199 - 200, Update the architecture documentation sections describing FindReferencers to state that it returns all matching referencers, not only the first, and revise the related examples accordingly. Preserve the method’s plural-result contract and explicitly avoid documenting or implying LIMIT(1).Source: Linked repositories
pkg/services/resource_test.go (1)
2199-2206: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winCover the all-referencers and name-based conflict contract.
This test supplies only one referencer and does not assert that the target’s name is used instead of its UUID. Add at least two referencers, assert both names appear, assert
target.Nameappears, and asserttarget.IDdoes not.As per path instructions, critical changed logic paths should have regression coverage.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@pkg/services/resource_test.go` around lines 2199 - 2206, Update the Delete conflict test around mockDao.FindReferencersResult to include at least two referencers, then assert both referencer names and the target’s Name appear in svcErr.Reason. Add an assertion that target.ID is absent, preserving the existing HTTP 409 validation.Source: Path instructions
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@pkg/dao/resource.go`:
- Around line 211-212: Update the error return in the resource lookup flow to
wrap the underlying DAO error with the failed operation and targetID context,
following the project’s Error Model Standard and preserving error unwrapping.
Change the return associated with the visible err check rather than nearby
success-path logic.
- Around line 206-210: Update the query in the ResourceReference lookup around
g2.Model to deduplicate referencers by grouping on resources.id, resources.kind,
and resources.name before scanning into summaries. Preserve the existing target,
deletion-status filters, join, and selected fields.
In `@pkg/services/resource.go`:
- Around line 986-991: Update the kind validation condition in the reference
validation flow around rd.TargetKind so an empty ref.Kind is accepted as the
documented backward-compatible omission, while non-empty kinds must still match
rd.TargetKind and return the existing validation error otherwise.
In `@test/integration/resource_references_test.go`:
- Around line 523-533: Add fatal guards in the resource-reference tests before
dereferencing potentially nil results: after creating the target in the current
test and in TestResourceReferences_CreateEmptyKind, fail with t.Fatal/t.Fatalf
when target creation returns an error; after creating the source, fail
immediately if validation unexpectedly succeeds before accessing svcErr.HTTPCode
or Reason. Preserve the existing status and reason assertions once svcErr is
confirmed non-nil.
---
Nitpick comments:
In `@pkg/dao/resource.go`:
- Around line 199-200: Update the architecture documentation sections describing
FindReferencers to state that it returns all matching referencers, not only the
first, and revise the related examples accordingly. Preserve the method’s
plural-result contract and explicitly avoid documenting or implying LIMIT(1).
In `@pkg/services/resource_test.go`:
- Around line 2199-2206: Update the Delete conflict test around
mockDao.FindReferencersResult to include at least two referencers, then assert
both referencer names and the target’s Name appear in svcErr.Reason. Add an
assertion that target.ID is absent, preserving the existing HTTP 409 validation.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Central YAML (base), Organization UI (inherited)
Review profile: CHILL
Plan: Enterprise
Run ID: 14da0023-d2ad-49c3-a5f9-e55bc8b30029
📒 Files selected for processing (6)
pkg/api/resource_reference.gopkg/dao/mocks/resource.gopkg/dao/resource.gopkg/services/resource.gopkg/services/resource_test.gotest/integration/resource_references_test.go
🔗 Linked repositories identified
CodeRabbit considers these linked repositories for cross-repo context during reviews:
openshift-hyperfleet/architecture(manual)openshift-hyperfleet/hyperfleet-api(manual)openshift-hyperfleet/hyperfleet-sentinel(manual)openshift-hyperfleet/hyperfleet-adapter(manual)openshift-hyperfleet/hyperfleet-broker(manual)
| if err != nil { | ||
| return nil, err |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Wrap the DAO error with target context.
Returning nil, err loses the failed operation and target ID at this boundary. Use the project’s error-wrapping convention, such as fmt.Errorf("find referencers for target %q: %w", targetID, err).
As per path instructions, errors must be wrapped per the Error Model Standard; a raw error return is not sufficient.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@pkg/dao/resource.go` around lines 211 - 212, Update the error return in the
resource lookup flow to wrap the underlying DAO error with the failed operation
and targetID context, following the project’s Error Model Standard and
preserving error unwrapping. Change the return associated with the visible err
check rather than nearby success-path logic.
Source: Path instructions
| target, svcErr := svc.Create(t.Context(), "RefTarget", | ||
| newRefTestResource("RefTarget", fmt.Sprintf("target-wk-%s", uuid.NewString()[:8])), nil) | ||
| Expect(svcErr).To(BeNil()) | ||
|
|
||
| sourceName := fmt.Sprintf("source-wk-%s", uuid.NewString()[:8]) | ||
| refs := makeRefs("dep", struct{ id, kind string }{target.ID, "WrongKind"}) | ||
|
|
||
| _, svcErr = svc.Create(t.Context(), "RefSource", newRefTestResource("RefSource", sourceName), refs) | ||
| Expect(svcErr).NotTo(BeNil(), "wrong kind should fail validation") | ||
| Expect(svcErr.HTTPCode).To(Equal(400)) | ||
| Expect(svcErr.Reason).To(ContainSubstring("does not match expected target kind")) |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Prevent nil dereferences after non-fatal assertions.
Expect does not stop execution. If target creation fails, target.ID can panic; if validation unexpectedly succeeds, svcErr.HTTPCode can panic instead of reporting the intended failure (CWE-476). Use t.Fatal/t.Fatalf guards before dereferencing in both tests.
Proposed fix
target, svcErr := svc.Create(...)
-Expect(svcErr).To(BeNil())
+if svcErr != nil || target == nil {
+ t.Fatalf("creating reference target failed: %v", svcErr)
+}
...
_, svcErr = svc.Create(...)
-Expect(svcErr).NotTo(BeNil(), "wrong kind should fail validation")
+if svcErr == nil {
+ t.Fatal("wrong kind should fail validation")
+}Apply the equivalent guards to TestResourceReferences_CreateEmptyKind.
Also applies to: 540-550
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@test/integration/resource_references_test.go` around lines 523 - 533, Add
fatal guards in the resource-reference tests before dereferencing potentially
nil results: after creating the target in the current test and in
TestResourceReferences_CreateEmptyKind, fail with t.Fatal/t.Fatalf when target
creation returns an error; after creating the source, fail immediately if
validation unexpectedly succeeds before accessing svcErr.HTTPCode or Reason.
Preserve the existing status and reason assertions once svcErr is confirmed
non-nil.
Source: Path instructions
c914418 to
b7fa156
Compare
There was a problem hiding this comment.
♻️ Duplicate comments (1)
pkg/services/resource.go (1)
986-991: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick winRestore backward-compatible empty-kind handling.
The service rejects empty
kind, while the integration test reinforces that regression by expecting HTTP 400. Emptykindmust be accepted; only non-empty mismatches should fail validation.
pkg/services/resource.go#L986-L991: change the condition toref.Kind != "" && ref.Kind != rd.TargetKind.test/integration/resource_references_test.go#L526-L527: removeEmptyKindfrom invalid-kind cases and assert successful creation separately.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@pkg/services/resource.go` around lines 986 - 991, Restore empty-kind compatibility in the validation around the reference kind check in pkg/services/resource.go lines 986-991 by rejecting only non-empty kinds that differ from rd.TargetKind. Update test/integration/resource_references_test.go lines 526-527 to remove EmptyKind from invalid-kind cases and add a separate assertion that creation with an empty kind succeeds.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Duplicate comments:
In `@pkg/services/resource.go`:
- Around line 986-991: Restore empty-kind compatibility in the validation around
the reference kind check in pkg/services/resource.go lines 986-991 by rejecting
only non-empty kinds that differ from rd.TargetKind. Update
test/integration/resource_references_test.go lines 526-527 to remove EmptyKind
from invalid-kind cases and add a separate assertion that creation with an empty
kind succeeds.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Central YAML (base), Organization UI (inherited)
Review profile: CHILL
Plan: Enterprise
Run ID: 4bc11bd4-71c4-4925-aa3c-76c1ba8b5f28
📒 Files selected for processing (6)
pkg/api/resource_reference.gopkg/dao/mocks/resource.gopkg/dao/resource.gopkg/services/resource.gopkg/services/resource_test.gotest/integration/resource_references_test.go
🔗 Linked repositories identified
CodeRabbit considers these linked repositories for cross-repo context during reviews:
openshift-hyperfleet/architecture(manual)openshift-hyperfleet/hyperfleet-api(manual)openshift-hyperfleet/hyperfleet-sentinel(manual)openshift-hyperfleet/hyperfleet-adapter(manual)openshift-hyperfleet/hyperfleet-broker(manual)
🚧 Files skipped from review as they are similar to previous changes (3)
- pkg/services/resource_test.go
- pkg/dao/resource.go
- pkg/dao/mocks/resource.go
|
/retest |
b7fa156 to
84dbb27
Compare
|
/lgtm |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: kuudori The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
2cda293
into
openshift-hyperfleet:main
Summary
Fixes 3 bugs from PR #288 code review. These include updating reference detection to list out all references for a specific resource, a missing validation for the
kindfield in reference requests, and error message to use name instead of UUID.HYPERFLEET-1345
Changes
FindReferencerstoFindReferencerssin the DAO layer and removedLIMIT 1, so the 409 conflict error on delete now lists all resources blocking the deletion instead of just onevalidateReferencesto reject reference requests where the client-providedkinddoes not match the registry's expectedTargetKindNotes
Bugs
#1and#5are already complete#288
#306
Test Plan
FindReferencersrename and new return typemake verify-allpassesmake test-integrationpassesmake lintpasses