Skip to content

chore: unify gathering to standard line creation - #3904

Merged
turip merged 1 commit into
mainfrom
chore/unify-gathering-to-standard-line-conversion
Mar 2, 2026
Merged

turip merged 1 commit into
mainfrom
chore/unify-gathering-to-standard-line-conversion

Conversation

@turip

@turip turip commented Feb 27, 2026 •

Copy link
Copy Markdown
Member

Overview

This refactor unifies the redundant standard line conversion parts of the code.

Summary by CodeRabbit

  • Refactor
    • Enhanced invoice line conversion with improved validation and error handling. Optimized data state management through proper cloning of configuration and subscription objects to ensure data integrity. Consolidated conversion logic to reduce code duplication and increase maintainability.

@turip
turip requested a review from a team as a code owner February 27, 2026 08:59
@coderabbitai

coderabbitai Bot commented Feb 27, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The PR refactors GatheringLine's conversion method from AsStandardLine() to AsNewStandardLine(invoiceID string) with validation, adding proper cloning for nested structures and error handling. Call sites are updated to use the new method, with a private helper function removed.

Changes

Cohort / File(s) Summary
Method Refactoring
openmeter/billing/gatheringinvoice.go
Updated AsStandardLine() to AsNewStandardLine(invoiceID string) with invoiceID validation and error handling. Added cloning for Annotations, Metadata, TaxConfig, Subscription, and RateCardDiscounts. Explicitly initializes DBState to nil.
Service Integration
openmeter/billing/service/gatheringinvoicependinglines.go, openmeter/billing/service/invoice.go
Updated call sites to use the new AsNewStandardLine(invoiceID) method with error propagation. Removed the private convertGatheringLineToNewStandardLine helper function. Updated line conversion logic in invoice processing to handle the new error return.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

release-note/misc, area/billing

Suggested reviewers

  • tothandras
  • chrisgacsal
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: unifying redundant gathering-to-standard-line conversion logic across multiple files by consolidating it into a single method.
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 (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch chore/unify-gathering-to-standard-line-conversion

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.

@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.

🧹 Nitpick comments (1)
openmeter/billing/gatheringinvoice.go (1)

663-671: Small fail-fast tweak: validate invoiceID before cloning annotations.

Line [664] does cloning work before the required input check on Line [669]. Flipping those two blocks keeps error precedence cleaner and avoids unnecessary work on invalid input.

♻️ Proposed refactor
 func (g GatheringLine) AsNewStandardLine(invoiceID string) (*StandardLine, error) {
-	clonedAnnotations, err := g.Annotations.Clone()
-	if err != nil {
-		return nil, fmt.Errorf("cloning annotations: %w", err)
-	}
-
 	if invoiceID == "" {
 		return nil, errors.New("invoice ID is required")
 	}
+
+	clonedAnnotations, err := g.Annotations.Clone()
+	if err != nil {
+		return nil, fmt.Errorf("cloning annotations: %w", err)
+	}
As per coding guidelines, in Golang readability and maintainability should be prioritized.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openmeter/billing/gatheringinvoice.go` around lines 663 - 671, In
GatheringLine.AsNewStandardLine validate the invoiceID parameter before
attempting g.Annotations.Clone(): move the invoiceID == "" check to run prior to
cloning so the function returns the "invoice ID is required" error fast and
avoids unnecessary work; update the block ordering inside AsNewStandardLine
accordingly so cloning (clonedAnnotations, err := g.Annotations.Clone()) only
runs after the invoiceID validation passes.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@openmeter/billing/gatheringinvoice.go`:
- Around line 663-671: In GatheringLine.AsNewStandardLine validate the invoiceID
parameter before attempting g.Annotations.Clone(): move the invoiceID == ""
check to run prior to cloning so the function returns the "invoice ID is
required" error fast and avoids unnecessary work; update the block ordering
inside AsNewStandardLine accordingly so cloning (clonedAnnotations, err :=
g.Annotations.Clone()) only runs after the invoiceID validation passes.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1c7c3b1 and b354727.

📒 Files selected for processing (3)
  • openmeter/billing/gatheringinvoice.go
  • openmeter/billing/service/gatheringinvoicependinglines.go
  • openmeter/billing/service/invoice.go

@turip turip added release-note/ignore Ignore this change when generating release notes area/billing labels Feb 27, 2026
@turip
turip enabled auto-merge (squash) February 27, 2026 16:17
@turip
turip requested review from GAlexIHU and tothandras February 27, 2026 16:17

if invoiceID == "" {
return nil, errors.New("invoice ID is required")
}

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.

I'd move this up to be the first check

@turip
turip merged commit c2c9ae6 into main Mar 2, 2026
30 of 32 checks passed
@turip
turip deleted the chore/unify-gathering-to-standard-line-conversion branch March 2, 2026 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants