Skip to content

Commit

Permalink
Merge pull request #86 from grafana/85-sort-the-versions-property
Browse files Browse the repository at this point in the history
sort the versions property
  • Loading branch information
szkiba authored Oct 1, 2024
2 parents 045249a + fe1b715 commit 3648f0e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 32 deletions.
36 changes: 4 additions & 32 deletions cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func load(ctx context.Context, in io.Reader, loose bool, lint bool, origin strin

ext.Versions = filterVersions(ext.Versions, constraints)
}

if err := sortVersions(ext.Versions); err != nil {
return nil, err
}
}

if lint {
Expand Down Expand Up @@ -197,38 +201,6 @@ func loadRepository(ctx context.Context, ext *k6registry.Extension) (*k6registry
return nil, nil, fmt.Errorf("%w: %s", errUnsupportedModule, module)
}

func tagsToVersions(tags []string) []string {
versions := make([]string, 0, len(tags))

for _, tag := range tags {
_, err := semver.NewVersion(tag)
if err != nil {
continue
}

versions = append(versions, tag)
}

return versions
}

func filterVersions(tags []string, constraints *semver.Constraints) []string {
versions := make([]string, 0, len(tags))

for _, tag := range tags {
version, err := semver.NewVersion(tag)
if err != nil {
continue
}

if constraints.Check(version) {
versions = append(versions, tag)
}
}

return versions
}

func loadGitHub(ctx context.Context, module string) (*k6registry.Repository, []string, error) {
slog.Debug("Loading GitHub repository", "module", module)

Expand Down
65 changes: 65 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd

import (
"sort"

"github.com/Masterminds/semver/v3"
)

func tagsToVersions(tags []string) []string {
versions := make([]string, 0, len(tags))

for _, tag := range tags {
_, err := semver.NewVersion(tag)
if err != nil {
continue
}

versions = append(versions, tag)
}

return versions
}

func filterVersions(tags []string, constraints *semver.Constraints) []string {
versions := make([]string, 0, len(tags))

for _, tag := range tags {
version, err := semver.NewVersion(tag)
if err != nil {
continue
}

if constraints.Check(version) {
versions = append(versions, tag)
}
}

return versions
}

func sortVersions(versions []string) error {
type version struct {
source string
parsed *semver.Version
}

all := make([]*version, 0, len(versions))

for _, source := range versions {
parsed, err := semver.NewVersion(source)
if err != nil {
return err
}

all = append(all, &version{source: source, parsed: parsed})
}

sort.Slice(all, func(i, j int) bool { return all[i].parsed.Compare(all[j].parsed) > 0 })

for idx := range all {
versions[idx] = all[idx].source
}

return nil
}
9 changes: 9 additions & 0 deletions releases/v0.1.31.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
k6registry `v0.1.31` is here 🎉!

This is an internal maintenance release.

**Sort the versions property**

The tags queried via the repository manager API happen to be in good order, the most recently created tag is at the beginning of the list. However, this does not guarantee that the list is always ordered according to semantic versioning.

After loading and detecting the versions of the extension during registry generation, the versions property now sorted according to semantic versioning.

0 comments on commit 3648f0e

Please sign in to comment.