Fix parseHelmVersion to handle helm versions without 'v' prefix (#2132)

* Initial plan

* Fix panic in helmfile init when parsing invalid helm versions

Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com>

* Fix parseHelmVersion to handle versions without v prefix

Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com>

* Simplify parseHelmVersion function to be more readable

Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com>
This commit is contained in:
Copilot 2025-08-14 21:11:09 +08:00 committed by GitHub
parent 8c123dcdda
commit 7f18858182
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View File

@ -69,7 +69,13 @@ func parseHelmVersion(versionStr string) (*semver.Version, error) {
return nil, fmt.Errorf("empty helm version")
}
v, err := chartify.FindSemVerInfo(versionStr)
// Check if version string starts with "v", if not add it
processedVersion := strings.TrimSpace(versionStr)
if !strings.HasPrefix(processedVersion, "v") {
processedVersion = "v" + processedVersion
}
v, err := chartify.FindSemVerInfo(processedVersion)
if err != nil {
return nil, fmt.Errorf("error find helm srmver version '%s': %w", versionStr, err)

View File

@ -1140,6 +1140,24 @@ func TestParseHelmVersion(t *testing.T) {
want: nil,
wantErr: true,
},
{
name: "version without v prefix",
version: "3.2.4",
want: semver.MustParse("v3.2.4"),
wantErr: false,
},
{
name: "version without v prefix with build info",
version: "3.2.4+ge29ce2a",
want: semver.MustParse("v3.2.4+ge29ce2a"),
wantErr: false,
},
{
name: "version without v prefix with spaces",
version: " 3.2.4 ",
want: semver.MustParse("v3.2.4"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {