-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.go
395 lines (303 loc) · 7.94 KB
/
load.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package cmd
import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"path/filepath"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/google/go-github/v62/github"
"github.com/grafana/k6registry"
"github.com/xanzy/go-gitlab"
"gopkg.in/yaml.v3"
)
func k6AsExtension() k6registry.Extension {
return k6registry.Extension{
Module: k6Module,
Description: k6Description,
Tier: k6registry.TierOfficial,
Products: []k6registry.Product{
k6registry.ProductCloud,
k6registry.ProductSynthetic,
k6registry.ProductOSS,
},
Imports: []string{k6ImportPath},
}
}
func loadSource(in io.Reader, loose bool) (k6registry.Registry, error) {
var (
raw []byte
err error
)
if loose {
slog.Debug("Read source")
raw, err = io.ReadAll(in)
} else {
slog.Debug("Validate source")
raw, err = validateWithSchema(in)
}
if err != nil {
return nil, err
}
var registry k6registry.Registry
slog.Debug("Unmarshal source")
if err := yaml.Unmarshal(raw, ®istry); err != nil {
return nil, err
}
k6 := false
for idx := range registry {
if registry[idx].Module == k6Module {
k6 = true
break
}
}
if !k6 {
registry = append(registry, k6AsExtension())
}
return registry, nil
}
func loadOne(ctx context.Context, ext *k6registry.Extension, lint bool) error {
if len(ext.Tier) == 0 {
ext.Tier = k6registry.TierCommunity
}
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 err
}
ext.Repo = repo
if len(ext.Versions) == 0 {
ext.Versions = tagsToVersions(tags)
}
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
}
var issues []string
for _, check := range compliance.Checks {
if !check.Passed {
issues = append(issues, string(check.ID))
}
}
ext.Compliance = &k6registry.Compliance{
Grade: k6registry.Grade(compliance.Grade),
Level: compliance.Level,
Issues: issues,
}
}
return 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
}
orig, err := loadOrigin(ctx, origin)
if err != nil {
return nil, err
}
for idx := range registry {
ext := ®istry[idx]
slog.Debug("Process extension", "module", ext.Module)
if !fromOrigin(ext, orig, origin) {
err := loadOne(ctx, ext, lint)
if err != nil {
return nil, err
}
}
if len(ext.Constraints) > 0 {
constraints, err := semver.NewConstraint(ext.Constraints)
if err != nil {
return nil, err
}
ext.Versions = filterVersions(ext.Versions, constraints)
}
if err := sortVersions(ext.Versions); err != nil {
return nil, err
}
}
if lint {
if err := validateWithLinter(registry); err != nil {
return nil, err
}
}
return registry, nil
}
func loadRepository(ctx context.Context, ext *k6registry.Extension) (*k6registry.Repository, []string, error) {
if ext.Versions != nil {
return ext.Repo, ext.Versions, nil
}
module := ext.Module
if ext.Repo != nil && len(ext.Repo.CloneURL) > 0 {
versions, err := loadGit(ctx, module, ext.Repo.CloneURL)
if err != nil {
return nil, nil, err
}
return ext.Repo, versions, nil
}
if strings.HasPrefix(module, k6Module) || strings.HasPrefix(module, ghModulePrefix) {
repo, tags, err := loadGitHub(ctx, module)
if err != nil {
return nil, nil, err
}
// Some unused metadata in the k6 repository changes too often
if strings.HasPrefix(module, k6Module) {
repo.Stars = 0
repo.Timestamp = 0
repo.CloneURL = ""
}
return repo, tags, nil
}
if strings.HasPrefix(module, glModulePrefix) {
repo, tags, err := loadGitLab(ctx, module)
if err != nil {
return nil, nil, err
}
return repo, tags, nil
}
return nil, nil, fmt.Errorf("%w: %s", errUnsupportedModule, module)
}
func loadGitHub(ctx context.Context, module string) (*k6registry.Repository, []string, error) {
slog.Debug("Loading GitHub repository", "module", module)
client, err := contextGitHubClient(ctx)
if err != nil {
return nil, nil, err
}
var owner, name string
if module == k6Module {
owner = "grafana"
name = "k6"
} else {
parts := strings.SplitN(module, "/", 4)
owner = parts[1]
name = parts[2]
}
repo := new(k6registry.Repository)
rep, _, err := client.Repositories.Get(ctx, owner, name)
if err != nil {
return nil, nil, err
}
repo.Topics = rep.Topics
repo.URL = rep.GetHTMLURL()
repo.Name = rep.GetName()
repo.Owner = rep.GetOwner().GetLogin()
repo.Homepage = rep.GetHomepage()
if len(repo.Homepage) == 0 {
repo.Homepage = repo.URL
}
repo.Archived = rep.GetArchived()
repo.Description = rep.GetDescription()
repo.Stars = rep.GetStargazersCount()
if lic := rep.GetLicense(); lic != nil {
repo.License = lic.GetSPDXID()
}
repo.Public = rep.GetVisibility() == "public"
if ts := rep.GetPushedAt(); !ts.IsZero() {
repo.Timestamp = float64(ts.Unix())
}
repo.CloneURL = rep.GetCloneURL()
repoTags, _, err := client.Repositories.ListTags(ctx, owner, name, &github.ListOptions{PerPage: 100})
if err != nil {
return nil, nil, err
}
tags := make([]string, 0, len(repoTags))
for _, tag := range repoTags {
tags = append(tags, tag.GetName())
}
return repo, tags, nil
}
func loadGitLab(ctx context.Context, module string) (*k6registry.Repository, []string, error) {
slog.Debug("Loading GitLab repository", "module", module)
client, err := gitlab.NewClient("")
if err != nil {
return nil, nil, err
}
pid := strings.TrimPrefix(module, glModulePrefix)
lic := true
proj, _, err := client.Projects.GetProject(pid, &gitlab.GetProjectOptions{License: &lic}, gitlab.WithContext(ctx))
if err != nil {
return nil, nil, err
}
repo := new(k6registry.Repository)
repo.Owner = proj.Namespace.FullPath
repo.Name = proj.Name
repo.Description = proj.Description
repo.Stars = proj.StarCount
repo.Archived = proj.Archived
repo.URL = proj.WebURL
repo.Homepage = proj.WebURL
repo.Topics = proj.Topics
repo.Public = len(proj.Visibility) == 0 || proj.Visibility == gitlab.PublicVisibility
repo.CloneURL = proj.HTTPURLToRepo
if proj.LastActivityAt != nil {
repo.Timestamp = float64(proj.LastActivityAt.Unix())
}
if proj.License != nil {
for key := range validLicenses {
if strings.EqualFold(key, proj.License.Key) {
repo.License = key
}
}
}
rels, _, err := client.Releases.ListReleases(pid,
&gitlab.ListReleasesOptions{
ListOptions: gitlab.ListOptions{PerPage: 50},
})
if err != nil {
return nil, nil, err
}
tags := make([]string, 0, len(rels))
for _, rel := range rels {
tags = append(tags, rel.TagName)
}
return repo, tags, nil
}
func loadGit(ctx context.Context, module string, cloneURL string) ([]string, error) {
base, err := modulesDir(ctx)
if err != nil {
return nil, err
}
dir := filepath.Join(base, module)
if err := updateWorkdir(ctx, dir, cloneURL); err != nil {
return nil, err
}
repo, err := git.PlainOpen(dir)
if err != nil {
return nil, err
}
iter, err := repo.Tags()
if err != nil {
return nil, err
}
const tagPrefix = "refs/tags/"
versions := make([]string, 0)
err = iter.ForEach(func(ref *plumbing.Reference) error {
tag := strings.TrimPrefix(ref.Name().String(), tagPrefix)
if _, err := semver.NewVersion(tag); err == nil {
versions = append(versions, tag)
}
return nil
})
if err != nil {
return nil, err
}
return versions, nil
}
const (
ghModulePrefix = "github.com/"
glModulePrefix = "gitlab.com/"
k6Module = "go.k6.io/k6"
k6ImportPath = "k6"
k6Description = "A modern load testing tool, using Go and JavaScript"
)
var errUnsupportedModule = errors.New("unsupported module")