Skip to content

Commit a198fca

Browse files
committed
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>
1 parent e55c0ce commit a198fca

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

internal/helm/repository/oci_chart_repository.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func getLastMatchingVersionOrConstraint(cvs []string, ver string) (string, error
228228
}
229229
}
230230

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

242-
matchingVersions = append(matchingVersions, cv)
242+
matchingVersions = append(matchingVersions, v)
243243
}
244244
if len(matchingVersions) == 0 {
245245
return "", fmt.Errorf("could not locate a version matching provided version string %s", ver)
246246
}
247247

248248
// Sort versions
249-
sort.Sort(sort.Reverse(sort.StringSlice(matchingVersions)))
249+
sort.Sort(sort.Reverse(semver.Collection(matchingVersions)))
250250

251-
return matchingVersions[0], nil
251+
return matchingVersions[0].Original(), nil
252252
}

internal/helm/repository/oci_chart_repository_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ func TestOCIChartRepoisitory_Get(t *testing.T) {
101101
"0.1.5+a.min.hour",
102102
"0.1.5+c.now",
103103
"0.2.0",
104+
"0.9.0",
105+
"0.10.0",
104106
"1.0.0",
105107
"1.1.0-rc.1",
106108
},
@@ -144,6 +146,11 @@ func TestOCIChartRepoisitory_Get(t *testing.T) {
144146
version: "0.1.0",
145147
expected: "0.1.0",
146148
},
149+
{
150+
name: "should return a perfect match",
151+
version: "0.*",
152+
expected: "0.10.0",
153+
},
147154
{
148155
name: "should an error for unfunfilled range",
149156
version: ">2.0.0",

0 commit comments

Comments
 (0)