fix: revert removal of ConvertFromV1 and add tests#2011
Conversation
Signed-off-by: Frederic Wilhelm <frederic.wilhelm@sap.com>
📝 WalkthroughWalkthroughAdds a Go converter to translate legacy v1 resolver configurations into runtime.Config (handles nil input, drops aliases, maps resolver priorities with defaults, constructs repositories via a runtime.Scheme, and returns conversion errors). Unit tests cover success and error scenarios. Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant ConvertFromV1
participant Scheme
participant RepoConverter
participant Logger
Caller->>ConvertFromV1: call ConvertFromV1(repositoryScheme, config)
ConvertFromV1->>Logger: log deprecation/warnings (if aliases)
alt config == nil
ConvertFromV1-->>Caller: return nil, nil
else
loop for each resolver in config.Resolvers
ConvertFromV1->>Scheme: create new Repository instance
Scheme-->>ConvertFromV1: repositoryObj
ConvertFromV1->>RepoConverter: convert repositoryObj -> runtime.Repository
RepoConverter-->>ConvertFromV1: runtime.Repository / error
ConvertFromV1->>ConvertFromV1: map Priority (use default if nil)
end
ConvertFromV1-->>Caller: assembled runtime.Config or error
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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. Comment |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (3)
bindings/go/configuration/ocm/v1/runtime/convert_v1.go (1)
20-22: Consider usingslog.Warnfor aliases deprecation notice.Since aliases are being silently dropped and this could lead to unexpected behavior for users relying on them,
slog.Warnmight be more appropriate thanslog.Infoto ensure visibility.💡 Suggested change
if len(config.Aliases) > 0 { - slog.Info("aliases are not supported in ocm v2, ignoring") + slog.Warn("aliases are not supported in ocm v2, ignoring") }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@bindings/go/configuration/ocm/v1/runtime/convert_v1.go` around lines 20 - 22, The current conversion path checks config.Aliases and logs with slog.Info when aliases are dropped; change this to use slog.Warn so users see a deprecation/ignored-aliases warning. Locate the check referencing config.Aliases and the slog.Info call (in convert_v1.go), replace the Info call with slog.Warn and keep the message content (or make it slightly more explicit) so dropped aliases are more visible during runtime.bindings/go/configuration/ocm/v1/runtime/convert_v1_test.go (2)
38-141: Good test coverage. Consider adding edge case tests for nil elements.The tests comprehensively cover the main scenarios. However, given the potential nil pointer issues in
convertResolvers, consider adding tests for:
- A resolver slice containing a nil element
- A resolver with a nil
RepositoryfieldThese would help document expected behavior and catch regressions if nil handling is added.
💡 Suggested additional test cases
func TestConvertFromV1_NilResolverInSlice(t *testing.T) { input := &resolverv1.Config{ Type: runtime.NewVersionedType(resolverv1.ConfigType, resolverv1.Version), Resolvers: []*resolverv1.Resolver{ nil, }, } _, err := ConvertFromV1(testScheme, input) assert.Error(t, err) } func TestConvertFromV1_NilRepositoryReturnsError(t *testing.T) { input := &resolverv1.Config{ Type: runtime.NewVersionedType(resolverv1.ConfigType, resolverv1.Version), Resolvers: []*resolverv1.Resolver{ {Repository: nil, Priority: ptr(10)}, }, } _, err := ConvertFromV1(testScheme, input) assert.Error(t, err) }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@bindings/go/configuration/ocm/v1/runtime/convert_v1_test.go` around lines 38 - 141, Add unit tests to cover nil-element edge cases in ConvertFromV1: create two tests that call ConvertFromV1(testScheme, input) where input.Resolvers contains (1) a nil element and (2) a resolver whose Repository is nil, and assert that ConvertFromV1 returns an error in both cases; this exercises convertResolvers/ConvertFromV1 error paths and prevents silent nil-pointer or unexpected-success behavior (referencing ConvertFromV1 and the resolverv1.Resolver Repository field to locate where to add the tests).
31-36: Minor: Raw JSON construction could break with special characters.If
baseURLcontains characters like"or\, the inline JSON construction would produce invalid JSON. For test purposes this is likely acceptable since test inputs are controlled, but usingjson.Marshalwould be more robust.💡 More robust alternative
+import "encoding/json" func rawTestRepo(baseURL string) *runtime.Raw { + data, _ := json.Marshal(map[string]string{ + "type": "test-repo/v1", + "baseURL": baseURL, + }) return &runtime.Raw{ Type: runtime.NewVersionedType("test-repo", "v1"), - Data: []byte(`{"type":"test-repo/v1","baseURL":"` + baseURL + `"}`), + Data: data, } }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@bindings/go/configuration/ocm/v1/runtime/convert_v1_test.go` around lines 31 - 36, The rawTestRepo helper builds JSON by concatenating strings which breaks if baseURL contains quotes or backslashes; update rawTestRepo to construct a small struct (e.g., with fields Type and BaseURL) and use json.Marshal to produce the Data byte slice, then set the Type field to runtime.NewVersionedType("test-repo","v1") and Data to the marshaled bytes so runtime.Raw.Data is valid JSON for any baseURL.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@bindings/go/configuration/ocm/v1/runtime/convert_v1.go`:
- Around line 43-50: The loop over resolvers may dereference a nil pointer; add
explicit nil checks for both `resolver` and `resolver.Repository` before calling
`resolver.Repository.GetType()` and `repositoryScheme.Convert`, and return an
informative error if either is nil (e.g., include the loop index `i` and which
field is missing). Update the block around the `for i, resolver := range
resolvers` loop to validate `resolver != nil` and `resolver.Repository != nil`
prior to calling `repositoryScheme.NewObject(resolver.Repository.GetType())` and
`repositoryScheme.Convert(...)`, returning the error rather than allowing a nil
pointer panic.
---
Nitpick comments:
In `@bindings/go/configuration/ocm/v1/runtime/convert_v1_test.go`:
- Around line 38-141: Add unit tests to cover nil-element edge cases in
ConvertFromV1: create two tests that call ConvertFromV1(testScheme, input) where
input.Resolvers contains (1) a nil element and (2) a resolver whose Repository
is nil, and assert that ConvertFromV1 returns an error in both cases; this
exercises convertResolvers/ConvertFromV1 error paths and prevents silent
nil-pointer or unexpected-success behavior (referencing ConvertFromV1 and the
resolverv1.Resolver Repository field to locate where to add the tests).
- Around line 31-36: The rawTestRepo helper builds JSON by concatenating strings
which breaks if baseURL contains quotes or backslashes; update rawTestRepo to
construct a small struct (e.g., with fields Type and BaseURL) and use
json.Marshal to produce the Data byte slice, then set the Type field to
runtime.NewVersionedType("test-repo","v1") and Data to the marshaled bytes so
runtime.Raw.Data is valid JSON for any baseURL.
In `@bindings/go/configuration/ocm/v1/runtime/convert_v1.go`:
- Around line 20-22: The current conversion path checks config.Aliases and logs
with slog.Info when aliases are dropped; change this to use slog.Warn so users
see a deprecation/ignored-aliases warning. Locate the check referencing
config.Aliases and the slog.Info call (in convert_v1.go), replace the Info call
with slog.Warn and keep the message content (or make it slightly more explicit)
so dropped aliases are more visible during runtime.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: a4b2e8fc-00ba-4be2-8e78-5ed762871e0a
📒 Files selected for processing (2)
bindings/go/configuration/ocm/v1/runtime/convert_v1.gobindings/go/configuration/ocm/v1/runtime/convert_v1_test.go
Signed-off-by: Frederic Wilhelm <frederic.wilhelm@sap.com>
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
bindings/go/configuration/ocm/v1/runtime/convert_v1.go (1)
50-55: Wrap conversion errors with resolver index context.Returning raw errors here makes triage harder; include resolver index/type so failures are actionable.
♻️ Proposed improvement
convertedRepo, err := repositoryScheme.NewObject(resolver.Repository.GetType()) if err != nil { - return nil, err + return nil, fmt.Errorf("resolver[%d]: create repository object for type %q: %w", i, resolver.Repository.GetType(), err) } if err := repositoryScheme.Convert(resolver.Repository, convertedRepo); err != nil { - return nil, err + return nil, fmt.Errorf("resolver[%d]: convert repository type %q: %w", i, resolver.Repository.GetType(), err) }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@bindings/go/configuration/ocm/v1/runtime/convert_v1.go` around lines 50 - 55, When converting resolver.Repository using repositoryScheme.NewObject and repositoryScheme.Convert, wrap any returned errors with contextual information (resolver index and resolver.Repository.GetType()) instead of returning raw err; capture the error, build a new error message like "failed to convert resolver %d (type %s): %w" and return that (using error wrapping e.g. fmt.Errorf) so failures from repositoryScheme.NewObject and repositoryScheme.Convert include resolver index and type for easier triage.bindings/go/configuration/ocm/v1/runtime/convert_v1_test.go (1)
38-161: Add a regression test for nilrepositoryScheme.Current coverage is strong, but it does not assert behavior when
repositorySchemeis nil andResolversis non-empty—the remaining panic path from the converter.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@bindings/go/configuration/ocm/v1/runtime/convert_v1_test.go` around lines 38 - 161, Add a regression test named TestConvertFromV1_NilRepositorySchemeReturnsError that verifies ConvertFromV1 handles a nil repositoryScheme without panicking: save the current repositoryScheme, set repositoryScheme = nil, defer restoring it, build an input resolverv1.Config with one resolver (use rawTestRepo("registry.example.com")), call ConvertFromV1(testScheme, input) and assert that it returns an error (assert.Error); reference ConvertFromV1, repositoryScheme, rawTestRepo and the new test function name to locate where to add this test.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@bindings/go/configuration/ocm/v1/runtime/convert_v1.go`:
- Around line 37-55: The function convertResolvers dereferences repositoryScheme
(calling repositoryScheme.NewObject and repositoryScheme.Convert) without
checking for nil, which can panic; add an early nil check at the top of
convertResolvers that returns a descriptive error if repositoryScheme == nil,
e.g. "repositoryScheme is nil", so callers get an error instead of a panic;
ensure this check runs before any use of repositoryScheme (before calling
repositoryScheme.NewObject or repositoryScheme.Convert) and keep existing nil
checks for individual resolvers and resolver.Repository intact.
---
Nitpick comments:
In `@bindings/go/configuration/ocm/v1/runtime/convert_v1_test.go`:
- Around line 38-161: Add a regression test named
TestConvertFromV1_NilRepositorySchemeReturnsError that verifies ConvertFromV1
handles a nil repositoryScheme without panicking: save the current
repositoryScheme, set repositoryScheme = nil, defer restoring it, build an input
resolverv1.Config with one resolver (use rawTestRepo("registry.example.com")),
call ConvertFromV1(testScheme, input) and assert that it returns an error
(assert.Error); reference ConvertFromV1, repositoryScheme, rawTestRepo and the
new test function name to locate where to add this test.
In `@bindings/go/configuration/ocm/v1/runtime/convert_v1.go`:
- Around line 50-55: When converting resolver.Repository using
repositoryScheme.NewObject and repositoryScheme.Convert, wrap any returned
errors with contextual information (resolver index and
resolver.Repository.GetType()) instead of returning raw err; capture the error,
build a new error message like "failed to convert resolver %d (type %s): %w" and
return that (using error wrapping e.g. fmt.Errorf) so failures from
repositoryScheme.NewObject and repositoryScheme.Convert include resolver index
and type for easier triage.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: d5667b51-2d03-4a4a-b6c5-2202e2b0df01
📒 Files selected for processing (2)
bindings/go/configuration/ocm/v1/runtime/convert_v1.gobindings/go/configuration/ocm/v1/runtime/convert_v1_test.go
Signed-off-by: Frederic Wilhelm <frederic.wilhelm@sap.com>
There was a problem hiding this comment.
🧹 Nitpick comments (2)
bindings/go/configuration/ocm/v1/runtime/convert_v1.go (2)
53-59: Consider adding resolver index to scheme error messages for consistency.Errors from
NewObjectandConvert(lines 55, 58) don't include the resolver index, unlike the nil check errors above (lines 48, 51). Adding the index would make debugging easier when conversions fail with multiple resolvers.💡 Suggested change
convertedRepo, err := repositoryScheme.NewObject(resolver.Repository.GetType()) if err != nil { - return nil, err + return nil, fmt.Errorf("resolver at index %d: failed to create repository object: %w", i, err) } if err := repositoryScheme.Convert(resolver.Repository, convertedRepo); err != nil { - return nil, err + return nil, fmt.Errorf("resolver at index %d: failed to convert repository: %w", i, err) }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@bindings/go/configuration/ocm/v1/runtime/convert_v1.go` around lines 53 - 59, The NewObject and Convert error returns in convert_v1.go do not include the resolver index like the earlier nil-checks; update the error handling around repositoryScheme.NewObject(resolver.Repository.GetType()) and repositoryScheme.Convert(resolver.Repository, convertedRepo) to wrap or format the returned error with the resolver index (e.g., include fmt.Errorf("resolver %d: %w", idx, err) or equivalent) so the returned errors consistently identify which resolver failed during conversion.
20-22: Consider usingslog.Warnfor dropped aliases.Since aliases are silently ignored and data is effectively being dropped during conversion,
slog.Warnwould be more semantically appropriate thanslog.Infoto alert users that their configuration is not fully honored.💡 Suggested change
if len(config.Aliases) > 0 { - slog.Info("aliases are not supported in ocm v2, ignoring") + slog.Warn("aliases are not supported in ocm v2, ignoring") }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@bindings/go/configuration/ocm/v1/runtime/convert_v1.go` around lines 20 - 22, The code in convert_v1.go currently logs dropped aliases with slog.Info when checking config.Aliases; change that call to slog.Warn (or slog.Logger.Warn if using an instance) so dropped aliases are signaled as a warning rather than info, and keep or slightly clarify the message like "aliases are not supported in ocm v2, ignoring" to indicate data is being dropped; update the call site that checks if len(config.Aliases) > 0 to use the warning-level logger.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@bindings/go/configuration/ocm/v1/runtime/convert_v1.go`:
- Around line 53-59: The NewObject and Convert error returns in convert_v1.go do
not include the resolver index like the earlier nil-checks; update the error
handling around repositoryScheme.NewObject(resolver.Repository.GetType()) and
repositoryScheme.Convert(resolver.Repository, convertedRepo) to wrap or format
the returned error with the resolver index (e.g., include fmt.Errorf("resolver
%d: %w", idx, err) or equivalent) so the returned errors consistently identify
which resolver failed during conversion.
- Around line 20-22: The code in convert_v1.go currently logs dropped aliases
with slog.Info when checking config.Aliases; change that call to slog.Warn (or
slog.Logger.Warn if using an instance) so dropped aliases are signaled as a
warning rather than info, and keep or slightly clarify the message like "aliases
are not supported in ocm v2, ignoring" to indicate data is being dropped; update
the call site that checks if len(config.Aliases) > 0 to use the warning-level
logger.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: e37d235c-ecf6-45a5-94fc-1c3a1146efe9
📒 Files selected for processing (1)
bindings/go/configuration/ocm/v1/runtime/convert_v1.go
What this PR does / why we need it
#1992 removed
ConvertFromV1because it appeared unused. However, it is actually used inopen-component-model/bindings/go/repository/component/resolvers/config.go
Line 57 in 651cc58
Some tests were added too, so it does not appear unused.
Summary by CodeRabbit