Refactor GitLab registry client tests and improve API base URL extrac…#26
Conversation
…tion - Updated test cases in gitlab_test.go to use example.com for consistency. - Introduced a new helper function, hostLooksLikeGitLab, to determine if a host resembles a GitLab instance. - Removed outdated comments in gitlab.go for clarity and maintainability.
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Use the checkbox below for a quick retry:
📝 WalkthroughWalkthroughThe refactoring extracts GitLab hostname detection logic into a reusable Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 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)
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 |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (3)
registryclients/gitlab.go (2)
113-117:⚠️ Potential issue | 🟠 MajorPre-existing:
defer resp.Body.Close()inside a loop leaks response bodies.Each iteration defers the close, but all defers only execute when the enclosing function returns. In a paginated loop, this means all response bodies remain open until the loop completes. Extract the loop body into a helper function, or close the body explicitly before continuing.
Proposed fix
resp, err := httpClient.Do(req) if err != nil { return nil, err } - defer resp.Body.Close() if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(resp.Body) + resp.Body.Close() return nil, fmt.Errorf("GitLab API error (status %d): %s", resp.StatusCode, string(body)) } var projects []gitLabProject if err := json.NewDecoder(resp.Body).Decode(&projects); err != nil { + resp.Body.Close() return nil, fmt.Errorf("failed to decode projects: %w", err) } + resp.Body.Close()Or more idiomatically, extract the per-page fetch into a separate function so
deferworks as expected.
65-86:⚠️ Potential issue | 🟡 Minor
getGitLabAPIBaseURLalways emitshttps://— HTTP-origin registries will silently upgrade.The method strips both
http://andhttps://schemes and unconditionally reconstructs the URL withhttps://. If a user's on-prem GitLab is HTTP-only (common in air-gapped or dev environments), the API calls will fail. This is the existing behavior, but with the expanded on-prem hostname support in this PR, it becomes more relevant.Consider preserving the original scheme when it was explicitly
http://.registryclients/gitlab_test.go (1)
87-100:⚠️ Potential issue | 🟡 MinorIP-address test cases produce invalid hostnames like
gitlab.192.168.1.100.Prepending
gitlab.to an IP address creates an invalid hostname that will never resolve. This is the existing behavior (these test cases aren't new), but now thathostLooksLikeGitLabis being introduced as a dedicated helper, it might be worth detecting IP addresses and handling them differently (e.g., skipping thegitlab.prepend or returning an error).
🧹 Nitpick comments (1)
registryclients/gitlab.go (1)
88-94:hostLooksLikeGitLabuses a very broad substring match.
strings.Contains(host, "gitlab")will match any host with "gitlab" anywhere in its name, including unintended cases like"notgitlabatall.example.com"or a domain where "gitlab" appears in an unrelated context. A slightly more structured check (e.g., verifying it appears as a domain label boundary using.gitlab.,gitlab.,gitlab-, or equals"gitlab") would reduce false positives while still covering on-prem variants.That said, the current test suite covers the intended cases well, so this is a low-risk observation.
There was a problem hiding this comment.
Pull request overview
This PR refactors how the GitLab registry client decides whether to prepend gitlab. when deriving the GitLab API base URL from a configured registry URL, and expands/normalizes the related test cases.
Changes:
- Updated
gitlab_test.gotest inputs to useexample.comdomains and added coverage for more hostname variants (e.g.,gitlab-*,.gitlab.,-gitlab.). - Introduced
hostLooksLikeGitLabto decide when to treat a host as an existing GitLab instance vs. prependinggitlab.. - Removed several inline comments in
gitlab.go.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
registryclients/gitlab_test.go |
Normalizes domains to example.com and adds many new base-URL derivation test cases. |
registryclients/gitlab.go |
Replaces the previous gitlab. prefix check with hostLooksLikeGitLab and removes some comments. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Improved the getGitLabAPIBaseURL method to handle registry URLs without schemes, ensuring proper URL formatting. - Added new test cases in gitlab_test.go to cover scenarios with query and fragment components in the registry URL. - Ensured consistent handling of plain host URLs by prepending "gitlab." when necessary.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Trimmed leading and trailing whitespace from registry URLs in the getGitLabAPIBaseURL method to ensure proper URL formatting. - Added new test cases in gitlab_test.go to validate handling of whitespace in registry URLs.
…tion
Summary by CodeRabbit
Refactor
Tests