Skip to content

Commit

Permalink
Merge pull request fluxcd#769 from souleb/fix-semver-string-compar
Browse files Browse the repository at this point in the history
Fix sorting semver from OCI repository tags
  • Loading branch information
stefanprodan committed Jun 8, 2022
2 parents e55c0ce + a163ea1 commit 2031c84
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 4 additions & 4 deletions internal/helm/repository/oci_chart_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func getLastMatchingVersionOrConstraint(cvs []string, ver string) (string, error
}
}

matchingVersions := make([]string, 0, len(cvs))
matchingVersions := make([]*semver.Version, 0, len(cvs))
for _, cv := range cvs {
v, err := version.ParseVersion(cv)
if err != nil {
Expand All @@ -239,14 +239,14 @@ func getLastMatchingVersionOrConstraint(cvs []string, ver string) (string, error
continue
}

matchingVersions = append(matchingVersions, cv)
matchingVersions = append(matchingVersions, v)
}
if len(matchingVersions) == 0 {
return "", fmt.Errorf("could not locate a version matching provided version string %s", ver)
}

// Sort versions
sort.Sort(sort.Reverse(sort.StringSlice(matchingVersions)))
sort.Sort(sort.Reverse(semver.Collection(matchingVersions)))

return matchingVersions[0], nil
return matchingVersions[0].Original(), nil
}
7 changes: 7 additions & 0 deletions internal/helm/repository/oci_chart_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ func TestOCIChartRepoisitory_Get(t *testing.T) {
"0.1.5+a.min.hour",
"0.1.5+c.now",
"0.2.0",
"0.9.0",
"0.10.0",
"1.0.0",
"1.1.0-rc.1",
},
Expand Down Expand Up @@ -144,6 +146,11 @@ func TestOCIChartRepoisitory_Get(t *testing.T) {
version: "0.1.0",
expected: "0.1.0",
},
{
name: "should return 0.10.0",
version: "0.*",
expected: "0.10.0",
},
{
name: "should an error for unfunfilled range",
version: ">2.0.0",
Expand Down

0 comments on commit 2031c84

Please sign in to comment.