Fix sorting semver from OCI repository tags

If implemented this fix the issue where we previously did a string
ordering of matching semver versions when retrieving a list of tags from
an OCI registry.

Signed-off-by: Soule BA <soule@weave.works>
This commit is contained in:
Soule BA 2022-06-07 22:05:28 +02:00
parent e55c0ceb5d
commit a163ea1dff
No known key found for this signature in database
GPG Key ID: 4D40965192802994
2 changed files with 11 additions and 4 deletions

View File

@ -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 {
@ -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
}

View File

@ -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",
},
@ -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",