Skip to content

Commit

Permalink
Merge pull request #80 from grafana/79-add-version-constraints-support
Browse files Browse the repository at this point in the history
feat: added version constraints support
  • Loading branch information
szkiba authored Sep 25, 2024
2 parents ce0df77 + d2dc493 commit 87d35d8
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 76 deletions.
12 changes: 10 additions & 2 deletions cmd/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -41,12 +42,19 @@ func writeData(filename string, data []byte) error {
}

func writeJSON(filename string, source interface{}) error {
data, err := json.MarshalIndent(source, "", " ")
var buff bytes.Buffer

encoder := json.NewEncoder(&buff)

encoder.SetIndent("", " ")
encoder.SetEscapeHTML(false)

err := encoder.Encode(source)
if err != nil {
return err
}

return writeData(filename, data)
return writeData(filename, buff.Bytes())
}

func writeAPIGroupGlobal(registry k6registry.Registry, target string) error {
Expand Down
2 changes: 2 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ func writeOutput(registry k6registry.Registry, output io.Writer, opts *options)
encoder.SetIndent("", " ")
}

encoder.SetEscapeHTML(false)

var source interface{} = registry

if opts.catalog {
Expand Down
2 changes: 1 addition & 1 deletion cmd/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func legacyConvert(ctx context.Context) error {

tmp := *ext
tmp.Repo = repo
tmp.Versions = filterVersions(tags)
tmp.Versions = tagsToVersions(tags)

if ok, _ := lintExtension(tmp); ok {
reg = append(reg, ext)
Expand Down
98 changes: 66 additions & 32 deletions cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,56 +74,72 @@ func loadSource(in io.Reader, loose bool) (k6registry.Registry, error) {
return registry, nil
}

func load(ctx context.Context, in io.Reader, loose bool, lint bool, origin string) (k6registry.Registry, error) {
registry, err := loadSource(in, loose)
if err != nil {
return nil, err
func loadOne(ctx context.Context, ext *k6registry.Extension, lint bool) error {
if len(ext.Tier) == 0 {
ext.Tier = k6registry.TierCommunity
}

orig, err := loadOrigin(ctx, origin)
if len(ext.Products) == 0 {
ext.Products = append(ext.Products, k6registry.ProductOSS)
}

if len(ext.Categories) == 0 {
ext.Categories = append(ext.Categories, k6registry.CategoryMisc)
}

repo, tags, err := loadRepository(ctx, ext)
if err != nil {
return nil, err
return err
}

for idx, ext := range registry {
slog.Debug("Process extension", "module", ext.Module)
ext.Repo = repo

if fromOrigin(&registry[idx], orig, origin) {
continue
}
if len(ext.Versions) == 0 {
ext.Versions = tagsToVersions(tags)
}

if len(ext.Tier) == 0 {
registry[idx].Tier = k6registry.TierCommunity
if lint && ext.Module != k6Module && ext.Compliance == nil && ext.Repo != nil {
compliance, err := checkCompliance(ctx, ext.Module, repo.CloneURL, repo.Timestamp)
if err != nil {
return err
}

if len(ext.Products) == 0 {
registry[idx].Products = append(registry[idx].Products, k6registry.ProductOSS)
}
ext.Compliance = &k6registry.Compliance{Grade: k6registry.Grade(compliance.Grade), Level: compliance.Level}
}

if len(ext.Categories) == 0 {
registry[idx].Categories = append(registry[idx].Categories, k6registry.CategoryMisc)
}
return nil
}

ext := ext
func load(ctx context.Context, in io.Reader, loose bool, lint bool, origin string) (k6registry.Registry, error) {
registry, err := loadSource(in, loose)
if err != nil {
return nil, err
}

repo, tags, err := loadRepository(ctx, &ext)
if err != nil {
return nil, err
}
orig, err := loadOrigin(ctx, origin)
if err != nil {
return nil, err
}

registry[idx].Repo = repo
for idx := range registry {
ext := &registry[idx]

if len(registry[idx].Versions) == 0 {
registry[idx].Versions = filterVersions(tags)
slog.Debug("Process extension", "module", ext.Module)

if !fromOrigin(ext, orig, origin) {
err := loadOne(ctx, ext, lint)
if err != nil {
return nil, err
}
}

if lint && ext.Module != k6Module && ext.Compliance == nil && ext.Repo != nil {
compliance, err := checkCompliance(ctx, ext.Module, repo.CloneURL, repo.Timestamp)
if len(ext.Constraints) > 0 {
constraints, err := semver.NewConstraint(ext.Constraints)
if err != nil {
return nil, err
}

registry[idx].Compliance = &k6registry.Compliance{Grade: k6registry.Grade(compliance.Grade), Level: compliance.Level}
ext.Versions = filterVersions(ext.Versions, constraints)
}
}

Expand Down Expand Up @@ -180,11 +196,12 @@ func loadRepository(ctx context.Context, ext *k6registry.Extension) (*k6registry
return nil, nil, fmt.Errorf("%w: %s", errUnsupportedModule, module)
}

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

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

Expand All @@ -194,6 +211,23 @@ func filterVersions(tags []string) []string {
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
4 changes: 4 additions & 0 deletions cmd/origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func fromOrigin(ext *k6registry.Extension, origin map[string]k6registry.Extensio
ext.Versions = oext.Versions
}

if len(ext.Constraints) == 0 {
ext.Constraints = oext.Constraints
}

if len(ext.Description) == 0 {
ext.Description = oext.Description
}
Expand Down
15 changes: 5 additions & 10 deletions docs/custom-catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"categories": [
"misc"
],
"constraints": ">=v0.50.0",
"description": "A modern load testing tool, using Go and JavaScript",
"imports": [
"k6"
Expand Down Expand Up @@ -35,7 +36,8 @@
"versions": [
"v0.53.0",
"v0.52.0",
"v0.51.0"
"v0.51.0",
"v0.50.0"
]
},
"k6/x/codename": {
Expand Down Expand Up @@ -69,10 +71,7 @@
"categories": [
"data"
],
"compliance": {
"grade": "A",
"level": 100
},
"constraints": ">=v0.4.0",
"description": "Generate random fake data",
"imports": [
"k6/x/faker"
Expand Down Expand Up @@ -133,10 +132,6 @@
"categories": [
"data"
],
"compliance": {
"grade": "A",
"level": 100
},
"description": "Load-test SQL Servers",
"imports": [
"k6/x/sql"
Expand All @@ -153,7 +148,7 @@
"name": "xk6-sql",
"owner": "grafana",
"public": true,
"stars": 108,
"stars": 109,
"timestamp": 1725979901,
"topics": [
"k6",
Expand Down
15 changes: 5 additions & 10 deletions docs/custom.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@
"categories": [
"data"
],
"compliance": {
"grade": "A",
"level": 100
},
"constraints": ">=v0.4.0",
"description": "Generate random fake data",
"imports": [
"k6/x/faker"
Expand Down Expand Up @@ -94,10 +91,6 @@
"categories": [
"data"
],
"compliance": {
"grade": "A",
"level": 100
},
"description": "Load-test SQL Servers",
"imports": [
"k6/x/sql"
Expand All @@ -114,7 +107,7 @@
"name": "xk6-sql",
"owner": "grafana",
"public": true,
"stars": 108,
"stars": 109,
"timestamp": 1725979901,
"topics": [
"k6",
Expand All @@ -139,6 +132,7 @@
"categories": [
"misc"
],
"constraints": ">=v0.50.0",
"description": "A modern load testing tool, using Go and JavaScript",
"imports": [
"k6"
Expand Down Expand Up @@ -171,7 +165,8 @@
"versions": [
"v0.53.0",
"v0.52.0",
"v0.51.0"
"v0.51.0",
"v0.50.0"
]
}
]
8 changes: 2 additions & 6 deletions docs/custom.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@
clone_url: [email protected]:szkiba/xk6-sqids.git

- module: github.com/grafana/xk6-faker
versions:
- v0.4.0
constraints: ">=v0.4.0"

- module: github.com/grafana/xk6-sql

- module: go.k6.io/k6
versions:
- v0.53.0
- v0.52.0
- v0.51.0
constraints: ">=v0.50.0"
13 changes: 13 additions & 0 deletions docs/registry.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
},
"description": {
"type": "string",
"default": "",
"description": "Brief description of the extension.\n",
"examples": [
"This is a very cool extension, it displays the message 'Hello World!'"
Expand All @@ -82,6 +83,18 @@
]
]
},
"constraints": {
"type": "string",
"pattern": "[vxX*|,&\\^0-9.+-><=, ~]+",
"default": "",
"description": "Version constraints.\n\nVersion constraints are primarily used to filter automatically detected versions.\nIt can also be used to filter the versions property imported from the origin registry.\n",
"examples": [
[
">=v0.4.0",
">v0.50.0"
]
]
},
"repo": {
"$ref": "#/$defs/repository",
"description": "Repository metadata.\n\nMetadata provided by the extension's git repository manager. Repository metadata are not registered, they are queried at runtime using the repository manager API.\n"
Expand Down
14 changes: 0 additions & 14 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,3 @@ func RegistryToCatalog(reg Registry) Catalog {

return catalog
}

// ModuleReference returns true if only the "Module" property has value.
func (ext *Extension) ModuleReference() bool {
return len(ext.Module) > 0 &&
len(ext.Description) == 0 &&
len(ext.Imports) == 0 &&
len(ext.Outputs) == 0 &&
len(ext.Versions) == 0 &&
len(ext.Categories) == 0 &&
len(ext.Products) == 0 &&
len(ext.Tier) == 0 &&
ext.Repo == nil &&
ext.Compliance == nil
}
11 changes: 10 additions & 1 deletion registry_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

This is an internal maintenance release.

**Add version constraints support**

Version constraints are primarily used to filter automatically detected versions. It can also be used to filter the versions property imported from the origin registry.

0 comments on commit 87d35d8

Please sign in to comment.