Skip to content

Commit ce0df77

Browse files
authored
Merge pull request #78 from grafana/77-support-for-import-of-extensions-from-other-registry
feat: Support for import extension properties from other registry
2 parents e6d51f8 + 76a1b42 commit ce0df77

File tree

13 files changed

+357
-195
lines changed

13 files changed

+357
-195
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ mdcode update README.md
6868
## custom - Update custom example
6969

7070
```bash
71-
go run ./cmd/k6registry --lint -o docs/custom.json docs/custom.yaml
72-
go run ./cmd/k6registry --lint --catalog -o docs/custom-catalog.json docs/custom.yaml
71+
export ORIGIN=https://registry.k6.io/registry.json
72+
go run ./cmd/k6registry --lint -o docs/custom.json --origin $ORIGIN docs/custom.yaml
73+
go run ./cmd/k6registry --lint --catalog -o docs/custom-catalog.json --origin $ORIGIN docs/custom.yaml
7374
```
7475

7576
## readme - Update README.md

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ lint | no | `false` | enable built-in linter
479479
compact| no | `false` | compact instead of pretty-printed output
480480
catalog| no | `false` | generate catalog instead of registry
481481
ref | no | | reference output URL for change detection
482+
origin | no | | external registry URL for default values
482483

483484
In GitHub action mode, the change can be indicated by comparing the output to a reference output. The reference output URL can be passed in the `ref` action parameter. The `changed` output variable will be `true` or `false` depending on whether the output has changed or not compared to the reference output.
484485

@@ -534,17 +535,18 @@ k6registry [flags] [source-file]
534535
### Flags
535536
536537
```
537-
-o, --out string write output to file instead of stdout
538-
--api string write outputs to directory instead of stdout
539-
--test strings test api path(s) (example: /registry.json,/catalog.json)
540-
-q, --quiet no output, only validation
541-
--loose skip JSON schema validation
542-
--lint enable built-in linter
543-
-c, --compact compact instead of pretty-printed output
544-
--catalog generate catalog instead of registry
545-
-v, --verbose verbose logging
546-
-V, --version print version
547-
-h, --help help for k6registry
538+
-o, --out string write output to file instead of stdout
539+
--api string write outputs to directory instead of stdout
540+
--origin string external registry URL for default values
541+
--test strings test api path(s) (example: /registry.json,/catalog.json)
542+
-q, --quiet no output, only validation
543+
--loose skip JSON schema validation
544+
--lint enable built-in linter
545+
-c, --compact compact instead of pretty-printed output
546+
--catalog generate catalog instead of registry
547+
-v, --verbose verbose logging
548+
-V, --version print version
549+
-h, --help help for k6registry
548550
```
549551
550552
<!-- #endregion cli -->

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ inputs:
4343
description: generate catalog instead of registry
4444
required: false
4545

46+
origin:
47+
description: external registry URL for default values
48+
required: false
49+
4650
ref:
4751
description: reference output URL for change detection
4852
required: false

cmd/cmd.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type options struct {
2727
lint bool
2828
api string
2929
test []string
30+
origin string
3031
}
3132

3233
// New creates new cobra command for exec command.
@@ -70,6 +71,7 @@ func New(levelVar *slog.LevelVar) (*cobra.Command, error) {
7071

7172
flags.StringVarP(&opts.out, "out", "o", "", "write output to file instead of stdout")
7273
flags.StringVar(&opts.api, "api", "", "write outputs to directory instead of stdout")
74+
flags.StringVar(&opts.origin, "origin", "", "external registry URL for default values")
7375
flags.StringSliceVar(&opts.test, "test", []string{}, "test api path(s) (example: /registry.json,/catalog.json)")
7476
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "no output, only validation")
7577
flags.BoolVar(&opts.loose, "loose", false, "skip JSON schema validation")
@@ -136,7 +138,7 @@ func run(ctx context.Context, args []string, opts *options) (result error) {
136138
output = file
137139
}
138140

139-
registry, err := load(ctx, input, opts.loose, opts.lint)
141+
registry, err := load(ctx, input, opts.loose, opts.lint, opts.origin)
140142
if err != nil {
141143
return err
142144
}

cmd/k6registry/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ func getArgs() []string {
113113
args = append(args, "--test", paths)
114114
}
115115

116+
if origin := getenv("INPUT_ORIGIN", ""); len(origin) != 0 {
117+
args = append(args, "--origin", origin)
118+
}
119+
116120
args = append(args, getenv("INPUT_IN", ""))
117121

118122
return args

cmd/load.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func k6AsExtension() k6registry.Extension {
3131
}
3232
}
3333

34-
func load(ctx context.Context, in io.Reader, loose bool, lint bool) (k6registry.Registry, error) {
34+
func loadSource(in io.Reader, loose bool) (k6registry.Registry, error) {
3535
var (
3636
raw []byte
3737
err error
@@ -58,10 +58,40 @@ func load(ctx context.Context, in io.Reader, loose bool, lint bool) (k6registry.
5858
return nil, err
5959
}
6060

61-
registry = append(registry, k6AsExtension())
61+
k6 := false
62+
63+
for idx := range registry {
64+
if registry[idx].Module == k6Module {
65+
k6 = true
66+
break
67+
}
68+
}
69+
70+
if !k6 {
71+
registry = append(registry, k6AsExtension())
72+
}
73+
74+
return registry, nil
75+
}
76+
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
81+
}
82+
83+
orig, err := loadOrigin(ctx, origin)
84+
if err != nil {
85+
return nil, err
86+
}
6287

6388
for idx, ext := range registry {
6489
slog.Debug("Process extension", "module", ext.Module)
90+
91+
if fromOrigin(&registry[idx], orig, origin) {
92+
continue
93+
}
94+
6595
if len(ext.Tier) == 0 {
6696
registry[idx].Tier = k6registry.TierCommunity
6797
}
@@ -87,7 +117,7 @@ func load(ctx context.Context, in io.Reader, loose bool, lint bool) (k6registry.
87117
registry[idx].Versions = filterVersions(tags)
88118
}
89119

90-
if lint && ext.Module != k6Module {
120+
if lint && ext.Module != k6Module && ext.Compliance == nil && ext.Repo != nil {
91121
compliance, err := checkCompliance(ctx, ext.Module, repo.CloneURL, repo.Timestamp)
92122
if err != nil {
93123
return nil, err

cmd/origin.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"io"
7+
"log/slog"
8+
"net/http"
9+
"time"
10+
11+
"github.com/grafana/k6registry"
12+
)
13+
14+
func loadOrigin(ctx context.Context, from string) (map[string]k6registry.Extension, error) {
15+
dict := make(map[string]k6registry.Extension, 0)
16+
17+
if len(from) == 0 {
18+
return dict, nil
19+
}
20+
21+
client := &http.Client{Timeout: httpTimeout}
22+
23+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, from, nil)
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
resp, err := client.Do(req)
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
defer resp.Body.Close() //nolint:errcheck
34+
35+
data, err := io.ReadAll(resp.Body)
36+
if err != nil {
37+
return nil, err
38+
}
39+
40+
var reg k6registry.Registry
41+
42+
err = json.Unmarshal(data, &reg)
43+
if err != nil {
44+
return nil, err
45+
}
46+
47+
for _, ext := range reg {
48+
dict[ext.Module] = ext
49+
}
50+
51+
return dict, nil
52+
}
53+
54+
func fromOrigin(ext *k6registry.Extension, origin map[string]k6registry.Extension, loc string) bool {
55+
oext, found := origin[ext.Module]
56+
if !found {
57+
return false
58+
}
59+
60+
slog.Debug("Import extension", "module", ext.Module, "origin", loc)
61+
62+
if ext.Compliance == nil {
63+
ext.Compliance = oext.Compliance
64+
}
65+
66+
if ext.Repo == nil {
67+
ext.Repo = oext.Repo
68+
}
69+
70+
if len(ext.Versions) == 0 {
71+
ext.Versions = oext.Versions
72+
}
73+
74+
if len(ext.Description) == 0 {
75+
ext.Description = oext.Description
76+
}
77+
78+
if len(ext.Tier) == 0 {
79+
ext.Tier = oext.Tier
80+
}
81+
82+
if len(ext.Categories) == 0 {
83+
ext.Categories = oext.Categories
84+
}
85+
86+
if len(ext.Products) == 0 {
87+
ext.Products = oext.Products
88+
}
89+
90+
if len(ext.Imports) == 0 {
91+
ext.Imports = oext.Imports
92+
}
93+
94+
if len(ext.Outputs) == 0 {
95+
ext.Outputs = oext.Outputs
96+
}
97+
98+
return true
99+
}
100+
101+
const httpTimeout = 10 * time.Second

0 commit comments

Comments
 (0)