Skip to content

fix(versioncheck): normalize semver before comparison#49

Merged
matthyx merged 1 commit into
kubescape:mainfrom
astro-dally:fix-semver-version-check
May 11, 2026
Merged

fix(versioncheck): normalize semver before comparison#49
matthyx merged 1 commit into
kubescape:mainfrom
astro-dally:fix-semver-version-check

Conversation

@astro-dally
Copy link
Copy Markdown
Contributor

@astro-dally astro-dally commented May 11, 2026

Summary

Normalize semantic versions before calling semver.Compare() to avoid incorrect comparisons when BuildNumber does not contain a leading v.

Example:

semver.Compare("4.0.6", "v3.0.15")

treats "4.0.6" as an invalid semantic version and incorrectly evaluates it as lower.

Changes

  • Added normalizeVersion() helper

  • Normalized versions before comparison

  • Added regression tests covering:

    • normalization behavior
    • mixed prefixed/non-prefixed comparisons
    • original invalid semver comparison case

Testing

go test ./...
go test ./pkg/versioncheck -v

Fixes #48

Summary by CodeRabbit

  • Bug Fixes

    • Version comparison now consistently normalizes version formats before performing comparisons, improving reliability when checking against the latest release version.
  • Tests

    • Added comprehensive unit tests validating version normalization and semver comparison behavior across different version string formats.

Review Change Stack

Signed-off-by: astro_dally <animemedia64@gmail.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9991609f-3e87-43b4-ad4e-701c3dd91612

📥 Commits

Reviewing files that changed from the base of the PR and between 8ff7f25 and 5688ed8.

📒 Files selected for processing (2)
  • pkg/versioncheck/versioncheck.go
  • pkg/versioncheck/versioncheck_test.go

📝 Walkthrough

Walkthrough

The PR fixes version comparison failures by normalizing semantic version strings before comparison. A new normalizeVersion helper adds a leading v prefix to version strings lacking one. CheckLatestVersion now normalizes both operands before invoking semver.Compare, preventing incorrect ordering when BuildNumber omits the prefix.

Changes

Version Comparison Normalization

Layer / File(s) Summary
Normalization Helper
pkg/versioncheck/versioncheck.go
Introduces normalizeVersion(v string) that prepends v to non-empty version strings not starting with v.
CheckLatestVersion Update
pkg/versioncheck/versioncheck.go
Updates comparison logic to normalize both BuildNumber and LatestReleaseVersion before passing to semver.Compare.
Test Coverage
pkg/versioncheck/versioncheck_test.go
Adds TestNormalizeVersion to validate normalization behavior, and TestSemverCompare_WithNormalization and TestSemverCompare_WithoutNormalization to confirm semver ordering correctness.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A version without its "v" was leading us astray,
Now normalization ensures comparisons work the right way!
Four-point-oh-six meets three-point-one-five,
And semver knows which one's truly alive. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: normalizing semantic versions before comparison in the version check logic.
Linked Issues check ✅ Passed The PR fully implements the fix specified in issue #48: introduces normalizeVersion() helper, applies it before semver.Compare(), and includes comprehensive regression tests covering the reported issue.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the semver comparison issue in versioncheck.go and adding corresponding unit tests, with no unrelated modifications detected.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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
Copy Markdown
Contributor

@matthyx matthyx left a comment

Choose a reason for hiding this comment

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

thanks @astro-dally it won't solve the real issue, but the fix is valuable and appreciated

@matthyx matthyx merged commit 9757ff6 into kubescape:main May 11, 2026
5 checks passed
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.

Incorrect version comparison when BuildNumber lacks leading "v"

2 participants