Repository navigation
fix: allow add-on for draft plan - #2689
Conversation
📝 Walkthrough## Walkthrough
The validation logic within the `PlanAddon.Validate()` method was updated to allow only `PlanStatusDraft` and `PlanStatusScheduled` as valid plan statuses, replacing the previous requirement for `PlanStatusActive`. The method now uses the `lo.Contains` function from the imported `github.com/samber/lo` package to check membership in the allowed statuses list. Error messages were revised to explicitly include the actual plan status and allowed statuses. The add-on status validation error message was reformatted for clarity without changing its logic. Additionally, the test suites were modified by removing immediate plan publishing steps after plan creation in two test files and adding a new subtest that verifies plan add-on assignment creation fails when the plan is published. No changes were made to exported or public entity declarations.
## Changes
| File(s) | Change Summary |
|-------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|
| openmeter/productcatalog/planaddon.go | Updated plan status validation to accept only `Draft` and `Scheduled` statuses using `lo.Contains`, revised error messages, and added import for `github.com/samber/lo`. |
| openmeter/productcatalog/planaddon/service/service_test.go | Removed initial plan publishing in "Addon" test setup; added "Invalid" subtest to verify add-on assignment creation fails for published plans. |
| openmeter/productcatalog/planaddon/adapter/adapter_test.go | Removed plan publishing step after plan creation in `TestPostgresAdapter`; no other test logic changed. |
## Possibly related PRs
- #2595: Refactored and renamed plan status constants, which the current validation changes build upon by using the renamed statuses `PlanStatusDraft` and `PlanStatusScheduled`.Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (1.64.8)Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (3)
💤 Files with no reviewable changes (1)
🚧 Files skipped from review as they are similar to previous changes (2)
⏰ Context from checks skipped due to timeout of 90000ms (12)
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (3)
openmeter/productcatalog/planaddon.go (3)
7-8: Avoid pulling a whole dependency for a singleContainscallImporting
github.com/samber/lojust to leveragelo.Containsincreases the compiled binary size, adds an additional transitive‑dependency surface and complicates dependency management for something that can be expressed in two native lines (or via a small local helper).-import ( - "github.com/samber/lo" -) +// helper – avoids extra dependency +func containsPlanStatus(needle PlanStatus, haystack []PlanStatus) bool { + for _, s := range haystack { + if s == needle { + return true + } + } + return false +}You can then call
containsPlanStatus(...)in place oflo.Contains.
71-71: Minor: allocate the allowed‑status slice once
allowedPlanStatusesdoes not vary between calls; defining it as avar(orconstarray) at package scope avoids per‑validation allocations.-var allowedPlanStatuses = []PlanStatus{PlanStatusActive, PlanStatusDraft, PlanStatusScheduled} +var allowedPlanStatuses = [...]PlanStatus{PlanStatusActive, PlanStatusDraft, PlanStatusScheduled}(Not critical, but a small perf/memory win in hot paths.)
85-86: Error message omits the second invalid conditionThe validation also rejects add‑ons whose
EffectiveTois non‑nil, yet the error text only mentions the status. Consider clarifying to aid debugging:- fmt.Errorf("invalid add-on [addon.key=%s addon.version=%d]: status must be active", + fmt.Errorf("invalid add-on [addon.key=%s addon.version=%d]: must be active and have open‑ended effective period",
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
openmeter/productcatalog/planaddon.go(3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (13)
- GitHub Check: Artifacts / Container image
- GitHub Check: Artifacts / Benthos Collector Container image
- GitHub Check: Test
- GitHub Check: Quickstart
- GitHub Check: Migration Checks
- GitHub Check: Lint
- GitHub Check: E2E
- GitHub Check: Developer environment
- GitHub Check: CI
- GitHub Check: Commit hooks
- GitHub Check: Build
- GitHub Check: Analyze (javascript)
- GitHub Check: Analyze (go)
3123c84 to
1140b12
Compare
1140b12 to
e28870c
Compare
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
openmeter/productcatalog/planaddon/service/service_test.go (2)
457-474: Well-structured test case for validating plan state restrictions.This new test case correctly verifies that creating a plan add-on fails when the plan is in published/active state, aligning with the PR objective to allow add-ons only for draft and scheduled plans.
A minor enhancement would be to verify the specific error message to ensure it clearly indicates that the plan's active status is what caused the failure.
Consider extending the assertion to validate the specific error message:
planAddon, err = planAddonService.CreatePlanAddon(ctx, planAddonInput) require.Errorf(t, err, "creating new plan add-on assignment must fail if plan is published/active") + require.Contains(t, err.Error(), "invalid plan status", "error should indicate invalid plan status")
457-458: Consider more descriptive test naming.The current naming ("Invalid" -> "Plan") is somewhat generic. More specific naming would better communicate the test's purpose.
- t.Run("Invalid", func(t *testing.T) { - t.Run("Plan", func(t *testing.T) { + t.Run("ValidationFailures", func(t *testing.T) { + t.Run("RejectedWhenPlanActive", func(t *testing.T) {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
openmeter/productcatalog/planaddon.go(3 hunks)openmeter/productcatalog/planaddon/service/service_test.go(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- openmeter/productcatalog/planaddon.go
⏰ Context from checks skipped due to timeout of 90000ms (13)
- GitHub Check: Trivy
- GitHub Check: Artifacts / Container image
- GitHub Check: Artifacts / Benthos Collector Container image
- GitHub Check: E2E
- GitHub Check: CI
- GitHub Check: Test
- GitHub Check: Quickstart
- GitHub Check: Migration Checks
- GitHub Check: Lint
- GitHub Check: Developer environment
- GitHub Check: Commit hooks
- GitHub Check: Build
- GitHub Check: Analyze (go)
e28870c to
00d9af8
Compare
Overview
Allow assigning add-on to plan in
draftstate.Summary by CodeRabbit