bug: Fix version list (#1323)

* 🐛 Fix version list

First version in the list is the latest one.
Alos moves the check for same version above the current place, so it can
check before asking if you want to update to the same version

Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>

* 🌱 Rework versioning for upgrade

Use the semver lib to parse the versions into a proper collection where
it can be parsed and versions compared and sorted properly

Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>

* 🤖 lint

Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>

---------

Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>
This commit is contained in:
Itxaka
2023-04-20 09:57:58 +02:00
parent ef0c14006b
commit 1f316097be
2 changed files with 36 additions and 3 deletions

View File

@@ -5,8 +5,10 @@ import (
"fmt"
"log"
"net/http"
"sort"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/google/go-github/v40/github"
"golang.org/x/oauth2"
)
@@ -19,7 +21,9 @@ func newHTTPClient(ctx context.Context, token string) *http.Client {
return oauth2.NewClient(ctx, src)
}
func FindReleases(ctx context.Context, token, slug string) ([]string, error) {
// FindReleases finds the releases from the given repo (slug) and returns a parsed semver.Collection
// where the first item is the highest version as its sorted.
func FindReleases(ctx context.Context, token, slug string) (semver.Collection, error) {
hc := newHTTPClient(ctx, token)
cli := github.NewClient(hc)
@@ -39,11 +43,13 @@ func FindReleases(ctx context.Context, token, slug string) ([]string, error) {
return nil, err
}
versions := []string{}
var versions semver.Collection
for _, rel := range rels {
if strings.HasPrefix(*rel.Name, "v") {
versions = append(versions, *rel.Name)
versions = append(versions, semver.MustParse(*rel.Name))
}
}
// Return them reversed sorted so the higher is the first one in the collection!
sort.Sort(sort.Reverse(versions))
return versions, nil
}

View File

@@ -0,0 +1,27 @@
package github_test
import (
"context"
"testing"
"github.com/kairos-io/kairos/v2/pkg/github"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestReleases(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Releases Suite")
}
var _ = Describe("Releases", func() {
It("can find the proper releases in order", func() {
releases, err := github.FindReleases(context.Background(), "", "kairos-io/kairos")
Expect(err).ToNot(HaveOccurred())
Expect(len(releases)).To(BeNumerically(">", 0))
// Expect the one at the bottom to be the first "real" release of kairos
Expect(releases[len(releases)-1].Original()).To(Equal("v1.0.0"))
// Expect the first one to be greater than the last one
Expect(releases[0].GreaterThan(releases[len(releases)-1]))
})
})