Skip to content

Commit

Permalink
Discover: Respect serverless-x.y.z branch names in sorting too (#291)
Browse files Browse the repository at this point in the history
* Discover: Respect serverless-x.y.z branch names in sorting too

* Run gofmt and goimports
  • Loading branch information
creydr authored Sep 19, 2024
1 parent 971a178 commit d8c6ae7
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 14 deletions.
42 changes: 28 additions & 14 deletions pkg/prowgen/prowgen_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"slices"
"strings"

"github.com/openshift-knative/hack/pkg/soversion"

"github.com/coreos/go-semver/semver"
)

Expand Down Expand Up @@ -67,26 +69,38 @@ func CmpBranches(a string, b string) int {
}
}

a = strings.ReplaceAll(a, "release-v", "")
a = strings.ReplaceAll(a, "release-", "")
var av, bv *semver.Version
var err error

if strings.HasPrefix(a, "serverless-") {
av = soversion.ToUpstreamVersion(a)
} else {
av, err = semverFromReleaseBranch(a)
if err != nil {
return -1 // this is equivalent to ignoring branches that aren't parseable
}
}

if strings.HasPrefix(b, "serverless-") {
bv = soversion.ToUpstreamVersion(b)
} else {
bv, err = semverFromReleaseBranch(b)
if err != nil {
return 1 // this is equivalent to ignoring branches that aren't parseable
}
}

return av.Compare(*bv)
}

func semverFromReleaseBranch(b string) (*semver.Version, error) {
b = strings.ReplaceAll(b, "release-v", "")
b = strings.ReplaceAll(b, "release-", "")
if strings.Count(a, ".") == 1 {
a += ".0"
}
if strings.Count(b, ".") == 1 {
b += ".0"
}
av, err := semver.NewVersion(a)
if err != nil {
return -1 // this is equivalent to ignoring branches that aren't parseable
}
bv, err := semver.NewVersion(b)
if err != nil {
return 1 // this is equivalent to ignoring branches that aren't parseable
}

return av.Compare(*bv)
return semver.NewVersion(b)
}

func gitClone(ctx context.Context, r Repository, mirror bool) error {
Expand Down
38 changes: 38 additions & 0 deletions pkg/prowgen/prowgen_git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package prowgen

import (
"slices"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestCmpBranches(t *testing.T) {
tests := []struct {
versions []string
expectedSorted []string
}{
{
versions: []string{"release-v1.15", "release-v1.14", "release-v1.16"},
expectedSorted: []string{"release-v1.14", "release-v1.15", "release-v1.16"},
},
{
versions: []string{"release-v1.15", "release-v1.16", "serverless-1.35"},
expectedSorted: []string{"release-v1.15", "serverless-1.35", "release-v1.16"},
},
{
versions: []string{"release-v1.15", "release-v1.16", "serverless-1.34"},
expectedSorted: []string{"serverless-1.34", "release-v1.15", "release-v1.16"},
},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
sorted := tt.versions
//testing indirect via stort func, as this is easier to read
slices.SortFunc(sorted, CmpBranches)
if diff := cmp.Diff(tt.expectedSorted, sorted); diff != "" {
t.Error("CmpBranches() (-want, +got):", diff)
}
})
}
}
26 changes: 26 additions & 0 deletions pkg/soversion/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,32 @@ func FromUpstreamVersion(upstream string) *semver.Version {
return soVersion
}

func ToUpstreamVersion(soversion string) *semver.Version {
soversion = strings.Replace(soversion, "serverless-v", "", 1)
soversion = strings.Replace(soversion, "serverless-", "", 1)
soversion = strings.Replace(soversion, "v", "", 1)

dotParts := strings.SplitN(soversion, ".", 3)
if len(dotParts) == 2 {
soversion = soversion + ".0"
}
upstreamVersion := semver.New(soversion)
upstreamVersion.Patch = 0
for i := 0; i < 21; i++ { // Example 1.32 -> 1.11
upstreamVersion.Minor--
}

soVersion := semver.New(soversion)
if soVersion.Compare(*semver.New("1.34.0")) >= 0 { //so >= xy
// As 1.12 was actually 1.13
// 1.33 -> 1.12
// 1.34 -> 1.14
upstreamVersion.Minor++
}

return upstreamVersion
}

func BranchName(soVersion *semver.Version) string {
return fmt.Sprintf("release-%d.%d", soVersion.Major, soVersion.Minor)
}
29 changes: 29 additions & 0 deletions pkg/soversion/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,32 @@ func TestFromUpstreamVersion(t *testing.T) {
})
}
}

func TestToUpstreamVersion(t *testing.T) {
tests := []struct {
soVersion string
want string
}{
{
soVersion: "1.32.0",
want: "1.11.0",
}, {
soVersion: "1.33.0",
want: "1.12.0",
}, {
soVersion: "1.33.1",
want: "1.12.0",
}, {
soVersion: "1.34.0",
want: "1.14.0",
},
}

for _, tt := range tests {
t.Run(fmt.Sprintf("%q -> %q", tt.soVersion, tt.want), func(t *testing.T) {
if got := ToUpstreamVersion(tt.soVersion); got.String() != semver.New(tt.want).String() {
t.Errorf("ToUpstreamVersion() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit d8c6ae7

Please sign in to comment.