Skip to content

Commit

Permalink
Fix bug when selecting release from CLI version (#7513)
Browse files Browse the repository at this point in the history
Comparison on same pre-release and ordered build metadata should only be
done if the build metadata in the provided version is "latest".
  • Loading branch information
g-gaston authored Feb 7, 2024
1 parent c084194 commit a8a706c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
25 changes: 13 additions & 12 deletions pkg/manifests/releases/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,24 @@ func ReleaseForVersion(releases *releasev1.Release, version string) (*releasev1.
return &release, nil
}

// If we are looking for the latest pre-release, we need to compare the build metadata
// Else we continue to look for an exact match.
if !wantLatestPreRelease {
continue
}

releaseVersion, err := semver.New(release.Version)
if err != nil {
return nil, fmt.Errorf("invalid version for release %d: %v", release.Number, err)
}

if semVer.SamePrerelease(releaseVersion) {
if wantLatestPreRelease {
// If we are looking for the latest pre-release, we need to compare the build metadata
// to find the latest one. CompareBuildMetadata will compare the build identifiers
// in order. For example: v0.19.0-dev+build.10 > v0.19.0-dev+build.9
if latestPreReleaseVersion == nil || releaseVersion.CompareBuildMetadata(latestPreReleaseVersion) > 0 {
latestPreRelease = &release
latestPreReleaseVersion = releaseVersion
}
} else {
return &release, nil
}
if semVer.SamePrerelease(releaseVersion) &&
(latestPreReleaseVersion == nil || releaseVersion.CompareBuildMetadata(latestPreReleaseVersion) > 0) {
// If we are looking for the latest pre-release, we need to compare the build metadata
// to find the latest one. CompareBuildMetadata will compare the build identifiers
// in order. For example: v0.19.0-dev+build.10 > v0.19.0-dev+build.9
latestPreRelease = &release
latestPreReleaseVersion = releaseVersion
}
}

Expand Down
33 changes: 18 additions & 15 deletions pkg/manifests/releases/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestReleaseForVersionSuccess(t *testing.T) {
want: &releasev1.EksARelease{Version: "v0.0.1-alpha", Number: 3},
},
{
name: "multiple releases same patch, same prerelease, different build metadata",
name: "version doesn't exist",
releases: &releasev1.Release{
Spec: releasev1.ReleaseSpec{
Releases: []releasev1.EksARelease{
Expand All @@ -102,38 +102,39 @@ func TestReleaseForVersionSuccess(t *testing.T) {
},
},
},
version: "v0.0.1-alpha",
want: &releasev1.EksARelease{Version: "v0.0.1-alpha+werwe", Number: 1},
version: "v0.0.2-alpha",
want: nil,
},
{
name: "version doesn't exist",
name: "want latest prerelease",
releases: &releasev1.Release{
Spec: releasev1.ReleaseSpec{
Releases: []releasev1.EksARelease{
{Version: "v0.0.1-alpha+werwe", Number: 1},
{Version: "v0.0.1-alpha+f4fe", Number: 2},
{Version: "v0.0.1-alpha+f43fs", Number: 3},
{Version: "v0.0.1-alpha+f234f", Number: 4},
{Version: "v0.0.1-alpha+build.3", Number: 1},
{Version: "v0.0.1-alpha+build.1", Number: 2},
{Version: "v0.0.1-alpha+build.10", Number: 3},
{Version: "v0.0.1-alpha+build.9", Number: 4},
},
},
},
version: "v0.0.2-alpha",
want: nil,
version: "v0.0.1-alpha+latest",
want: &releasev1.EksARelease{Version: "v0.0.1-alpha+build.10", Number: 3},
},
{
name: "want latest prerelease",
name: "want exact match with versions in same pre-release",
releases: &releasev1.Release{
Spec: releasev1.ReleaseSpec{
Releases: []releasev1.EksARelease{
{Version: "v0.0.1-alpha+build.3", Number: 1},
{Version: "v0.0.1-alpha+build.1", Number: 2},
{Version: "v0.0.1-alpha+build.10", Number: 3},
{Version: "v0.0.1-alpha+build.9", Number: 4},
{Version: "v0.0.1-alpha+build.4", Number: 4},
{Version: "v0.0.1-alpha+build.9", Number: 5},
},
},
},
version: "v0.0.1-alpha+latest",
want: &releasev1.EksARelease{Version: "v0.0.1-alpha+build.10", Number: 3},
version: "v0.0.1-alpha+build.4",
want: &releasev1.EksARelease{Version: "v0.0.1-alpha+build.4", Number: 4},
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -166,7 +167,9 @@ func TestReleaseForVersionError(t *testing.T) {
},
},
},
version: "1.1.1",
// We need to ask fo the latest pre-release if we want to trigger
// the version parsing
version: "1.1.1-dev+latest",
want: "invalid version for release 2",
},
}
Expand Down

0 comments on commit a8a706c

Please sign in to comment.