Skip to content

fix: allow add-on for draft plan - #2689

Merged
chrisgacsal merged 1 commit into
mainfrom
refactor/productcatalog
Apr 17, 2025
Merged

chrisgacsal merged 1 commit into
mainfrom
refactor/productcatalog

Conversation

@chrisgacsal

@chrisgacsal chrisgacsal commented Apr 17, 2025 •

Copy link
Copy Markdown
Collaborator

Overview

Allow assigning add-on to plan in draft state.

Summary by CodeRabbit

  • Bug Fixes
    • Updated validation to allow only draft and scheduled plan statuses.
    • Enhanced error messages for invalid plan and add-on statuses for clearer feedback.
    • Added a new test to prevent add-on assignments on published plans.

@chrisgacsal chrisgacsal added the release-note/ignore Ignore this change when generating release notes label Apr 17, 2025
@chrisgacsal chrisgacsal self-assigned this Apr 17, 2025
@chrisgacsal
chrisgacsal requested a review from a team as a code owner April 17, 2025 13:44
@coderabbitai

coderabbitai Bot commented Apr 17, 2025 •

Copy link
Copy Markdown
Contributor
📝 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
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e28870c and 00d9af8.

📒 Files selected for processing (3)
  • openmeter/productcatalog/planaddon.go (3 hunks)
  • openmeter/productcatalog/planaddon/adapter/adapter_test.go (0 hunks)
  • openmeter/productcatalog/planaddon/service/service_test.go (1 hunks)
💤 Files with no reviewable changes (1)
  • openmeter/productcatalog/planaddon/adapter/adapter_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • openmeter/productcatalog/planaddon/service/service_test.go
  • openmeter/productcatalog/planaddon.go
⏰ Context from checks skipped due to timeout of 90000ms (12)
  • GitHub Check: Artifacts / Benthos Collector Container image
  • GitHub Check: Artifacts / Container image
  • GitHub Check: CI
  • GitHub Check: Quickstart
  • GitHub Check: E2E
  • GitHub Check: Lint
  • GitHub Check: Commit hooks
  • GitHub Check: Developer environment
  • GitHub Check: Test
  • GitHub Check: Migration Checks
  • GitHub Check: Build
  • GitHub Check: Analyze (go)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
openmeter/productcatalog/planaddon.go (3)

7-8: Avoid pulling a whole dependency for a single Contains call

Importing github.com/samber/lo just to leverage lo.Contains increases 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 of lo.Contains.


71-71: Minor: allocate the allowed‑status slice once

allowedPlanStatuses does not vary between calls; defining it as a var (or const array) 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 condition

The validation also rejects add‑ons whose EffectiveTo is 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

📥 Commits

Reviewing files that changed from the base of the PR and between 8200a17 and 6c1d30c.

📒 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)

Comment thread openmeter/productcatalog/planaddon.go Resolved
@chrisgacsal
chrisgacsal force-pushed the refactor/productcatalog branch 2 times, most recently from 3123c84 to 1140b12 Compare April 17, 2025 13:49
GAlexIHU
GAlexIHU previously approved these changes Apr 17, 2025
tothandras
tothandras previously approved these changes Apr 17, 2025

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1140b12 and e28870c.

📒 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)

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

Labels

release-note/ignore Ignore this change when generating release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants