Skip to content

Commit 87d35d8

Browse files
authored
Merge pull request #80 from grafana/79-add-version-constraints-support
feat: added version constraints support
2 parents ce0df77 + d2dc493 commit 87d35d8

File tree

12 files changed

+125
-76
lines changed

12 files changed

+125
-76
lines changed

cmd/api.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"errors"
67
"fmt"
@@ -41,12 +42,19 @@ func writeData(filename string, data []byte) error {
4142
}
4243

4344
func writeJSON(filename string, source interface{}) error {
44-
data, err := json.MarshalIndent(source, "", " ")
45+
var buff bytes.Buffer
46+
47+
encoder := json.NewEncoder(&buff)
48+
49+
encoder.SetIndent("", " ")
50+
encoder.SetEscapeHTML(false)
51+
52+
err := encoder.Encode(source)
4553
if err != nil {
4654
return err
4755
}
4856

49-
return writeData(filename, data)
57+
return writeData(filename, buff.Bytes())
5058
}
5159

5260
func writeAPIGroupGlobal(registry k6registry.Registry, target string) error {

cmd/cmd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ func writeOutput(registry k6registry.Registry, output io.Writer, opts *options)
165165
encoder.SetIndent("", " ")
166166
}
167167

168+
encoder.SetEscapeHTML(false)
169+
168170
var source interface{} = registry
169171

170172
if opts.catalog {

cmd/legacy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func legacyConvert(ctx context.Context) error {
8787

8888
tmp := *ext
8989
tmp.Repo = repo
90-
tmp.Versions = filterVersions(tags)
90+
tmp.Versions = tagsToVersions(tags)
9191

9292
if ok, _ := lintExtension(tmp); ok {
9393
reg = append(reg, ext)

cmd/load.go

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -74,56 +74,72 @@ func loadSource(in io.Reader, loose bool) (k6registry.Registry, error) {
7474
return registry, nil
7575
}
7676

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

83-
orig, err := loadOrigin(ctx, origin)
82+
if len(ext.Products) == 0 {
83+
ext.Products = append(ext.Products, k6registry.ProductOSS)
84+
}
85+
86+
if len(ext.Categories) == 0 {
87+
ext.Categories = append(ext.Categories, k6registry.CategoryMisc)
88+
}
89+
90+
repo, tags, err := loadRepository(ctx, ext)
8491
if err != nil {
85-
return nil, err
92+
return err
8693
}
8794

88-
for idx, ext := range registry {
89-
slog.Debug("Process extension", "module", ext.Module)
95+
ext.Repo = repo
9096

91-
if fromOrigin(&registry[idx], orig, origin) {
92-
continue
93-
}
97+
if len(ext.Versions) == 0 {
98+
ext.Versions = tagsToVersions(tags)
99+
}
94100

95-
if len(ext.Tier) == 0 {
96-
registry[idx].Tier = k6registry.TierCommunity
101+
if lint && ext.Module != k6Module && ext.Compliance == nil && ext.Repo != nil {
102+
compliance, err := checkCompliance(ctx, ext.Module, repo.CloneURL, repo.Timestamp)
103+
if err != nil {
104+
return err
97105
}
98106

99-
if len(ext.Products) == 0 {
100-
registry[idx].Products = append(registry[idx].Products, k6registry.ProductOSS)
101-
}
107+
ext.Compliance = &k6registry.Compliance{Grade: k6registry.Grade(compliance.Grade), Level: compliance.Level}
108+
}
102109

103-
if len(ext.Categories) == 0 {
104-
registry[idx].Categories = append(registry[idx].Categories, k6registry.CategoryMisc)
105-
}
110+
return nil
111+
}
106112

107-
ext := ext
113+
func load(ctx context.Context, in io.Reader, loose bool, lint bool, origin string) (k6registry.Registry, error) {
114+
registry, err := loadSource(in, loose)
115+
if err != nil {
116+
return nil, err
117+
}
108118

109-
repo, tags, err := loadRepository(ctx, &ext)
110-
if err != nil {
111-
return nil, err
112-
}
119+
orig, err := loadOrigin(ctx, origin)
120+
if err != nil {
121+
return nil, err
122+
}
113123

114-
registry[idx].Repo = repo
124+
for idx := range registry {
125+
ext := &registry[idx]
115126

116-
if len(registry[idx].Versions) == 0 {
117-
registry[idx].Versions = filterVersions(tags)
127+
slog.Debug("Process extension", "module", ext.Module)
128+
129+
if !fromOrigin(ext, orig, origin) {
130+
err := loadOne(ctx, ext, lint)
131+
if err != nil {
132+
return nil, err
133+
}
118134
}
119135

120-
if lint && ext.Module != k6Module && ext.Compliance == nil && ext.Repo != nil {
121-
compliance, err := checkCompliance(ctx, ext.Module, repo.CloneURL, repo.Timestamp)
136+
if len(ext.Constraints) > 0 {
137+
constraints, err := semver.NewConstraint(ext.Constraints)
122138
if err != nil {
123139
return nil, err
124140
}
125141

126-
registry[idx].Compliance = &k6registry.Compliance{Grade: k6registry.Grade(compliance.Grade), Level: compliance.Level}
142+
ext.Versions = filterVersions(ext.Versions, constraints)
127143
}
128144
}
129145

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

183-
func filterVersions(tags []string) []string {
199+
func tagsToVersions(tags []string) []string {
184200
versions := make([]string, 0, len(tags))
185201

186202
for _, tag := range tags {
187-
if _, err := semver.NewVersion(tag); err != nil {
203+
_, err := semver.NewVersion(tag)
204+
if err != nil {
188205
continue
189206
}
190207

@@ -194,6 +211,23 @@ func filterVersions(tags []string) []string {
194211
return versions
195212
}
196213

214+
func filterVersions(tags []string, constraints *semver.Constraints) []string {
215+
versions := make([]string, 0, len(tags))
216+
217+
for _, tag := range tags {
218+
version, err := semver.NewVersion(tag)
219+
if err != nil {
220+
continue
221+
}
222+
223+
if constraints.Check(version) {
224+
versions = append(versions, tag)
225+
}
226+
}
227+
228+
return versions
229+
}
230+
197231
func loadGitHub(ctx context.Context, module string) (*k6registry.Repository, []string, error) {
198232
slog.Debug("Loading GitHub repository", "module", module)
199233

cmd/origin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func fromOrigin(ext *k6registry.Extension, origin map[string]k6registry.Extensio
7171
ext.Versions = oext.Versions
7272
}
7373

74+
if len(ext.Constraints) == 0 {
75+
ext.Constraints = oext.Constraints
76+
}
77+
7478
if len(ext.Description) == 0 {
7579
ext.Description = oext.Description
7680
}

docs/custom-catalog.json

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"categories": [
44
"misc"
55
],
6+
"constraints": ">=v0.50.0",
67
"description": "A modern load testing tool, using Go and JavaScript",
78
"imports": [
89
"k6"
@@ -35,7 +36,8 @@
3536
"versions": [
3637
"v0.53.0",
3738
"v0.52.0",
38-
"v0.51.0"
39+
"v0.51.0",
40+
"v0.50.0"
3941
]
4042
},
4143
"k6/x/codename": {
@@ -69,10 +71,7 @@
6971
"categories": [
7072
"data"
7173
],
72-
"compliance": {
73-
"grade": "A",
74-
"level": 100
75-
},
74+
"constraints": ">=v0.4.0",
7675
"description": "Generate random fake data",
7776
"imports": [
7877
"k6/x/faker"
@@ -133,10 +132,6 @@
133132
"categories": [
134133
"data"
135134
],
136-
"compliance": {
137-
"grade": "A",
138-
"level": 100
139-
},
140135
"description": "Load-test SQL Servers",
141136
"imports": [
142137
"k6/x/sql"
@@ -153,7 +148,7 @@
153148
"name": "xk6-sql",
154149
"owner": "grafana",
155150
"public": true,
156-
"stars": 108,
151+
"stars": 109,
157152
"timestamp": 1725979901,
158153
"topics": [
159154
"k6",

docs/custom.json

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@
5858
"categories": [
5959
"data"
6060
],
61-
"compliance": {
62-
"grade": "A",
63-
"level": 100
64-
},
61+
"constraints": ">=v0.4.0",
6562
"description": "Generate random fake data",
6663
"imports": [
6764
"k6/x/faker"
@@ -94,10 +91,6 @@
9491
"categories": [
9592
"data"
9693
],
97-
"compliance": {
98-
"grade": "A",
99-
"level": 100
100-
},
10194
"description": "Load-test SQL Servers",
10295
"imports": [
10396
"k6/x/sql"
@@ -114,7 +107,7 @@
114107
"name": "xk6-sql",
115108
"owner": "grafana",
116109
"public": true,
117-
"stars": 108,
110+
"stars": 109,
118111
"timestamp": 1725979901,
119112
"topics": [
120113
"k6",
@@ -139,6 +132,7 @@
139132
"categories": [
140133
"misc"
141134
],
135+
"constraints": ">=v0.50.0",
142136
"description": "A modern load testing tool, using Go and JavaScript",
143137
"imports": [
144138
"k6"
@@ -171,7 +165,8 @@
171165
"versions": [
172166
"v0.53.0",
173167
"v0.52.0",
174-
"v0.51.0"
168+
"v0.51.0",
169+
"v0.50.0"
175170
]
176171
}
177172
]

docs/custom.yaml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,9 @@
2525
clone_url: git@bitbucket.org:szkiba/xk6-sqids.git
2626

2727
- module: github.com/grafana/xk6-faker
28-
versions:
29-
- v0.4.0
28+
constraints: ">=v0.4.0"
3029

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

3332
- module: go.k6.io/k6
34-
versions:
35-
- v0.53.0
36-
- v0.52.0
37-
- v0.51.0
33+
constraints: ">=v0.50.0"

docs/registry.schema.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
},
6464
"description": {
6565
"type": "string",
66+
"default": "",
6667
"description": "Brief description of the extension.\n",
6768
"examples": [
6869
"This is a very cool extension, it displays the message 'Hello World!'"
@@ -82,6 +83,18 @@
8283
]
8384
]
8485
},
86+
"constraints": {
87+
"type": "string",
88+
"pattern": "[vxX*|,&\\^0-9.+-><=, ~]+",
89+
"default": "",
90+
"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",
91+
"examples": [
92+
[
93+
">=v0.4.0",
94+
">v0.50.0"
95+
]
96+
]
97+
},
8598
"repo": {
8699
"$ref": "#/$defs/repository",
87100
"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"

registry.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,3 @@ func RegistryToCatalog(reg Registry) Catalog {
6565

6666
return catalog
6767
}
68-
69-
// ModuleReference returns true if only the "Module" property has value.
70-
func (ext *Extension) ModuleReference() bool {
71-
return len(ext.Module) > 0 &&
72-
len(ext.Description) == 0 &&
73-
len(ext.Imports) == 0 &&
74-
len(ext.Outputs) == 0 &&
75-
len(ext.Versions) == 0 &&
76-
len(ext.Categories) == 0 &&
77-
len(ext.Products) == 0 &&
78-
len(ext.Tier) == 0 &&
79-
ext.Repo == nil &&
80-
ext.Compliance == nil
81-
}

registry_gen.go

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

releases/v0.1.28.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
k6registry `v0.1.28` is here 🎉!
2+
3+
This is an internal maintenance release.
4+
5+
**Add version constraints support**
6+
7+
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 commit comments

Comments
 (0)