diff --git a/fetch/resolver.go b/fetch/resolver.go index 3c3ab6c..5d1a639 100644 --- a/fetch/resolver.go +++ b/fetch/resolver.go @@ -114,8 +114,8 @@ type ResolveOptions struct { // Qualifiers are PURL qualifiers. Those in selectionQualifiers pick among // the artifacts a registry lists for a version; an artifact that does not // record a qualifier is not ruled out by it. An artifact whose platform is - // universal serves any platform, after one built for the platform asked - // for. repository_url and channel + // universal, or ruby for a gem, serves any platform, after one built for + // the platform asked for. repository_url and channel // choose the registry, which is up to the caller, and are ignored here. Qualifiers map[string]string } @@ -128,6 +128,8 @@ const ( qualifierPlatform = "platform" // universalPlatform is the platform of a build that runs on every one. universalPlatform = "universal" + // rubyPlatform is RubyGems' universal platform: a gem built for none installs on any. + rubyPlatform = "ruby" ) func (o ResolveOptions) hasSelectionQualifier() bool { @@ -176,9 +178,8 @@ func (r *Resolver) ResolveWithOptions( // For ecosystems like PyPI, we need to fetch metadata to get the URL info, err := r.resolveFromMetadata(ctx, reg, name, version, options) if (errors.Is(err, ErrNoDownloadURL) || errors.Is(err, ErrNotFound)) && options.hasSelectionQualifier() { - // The registry lists no artifacts to pick among, or lists the - // version under another number as RubyGems does for a platform - // build, so the built URL is the only one there is. + // The registry lists no artifacts to pick among, or doesn't list + // the version, so the built URL is the only one there is. if url := reg.URLs().Download(name, version); url != "" { return matchSingleArtifact(&ArtifactInfo{URL: url, Filename: filenameFromURL(url)}, options, nil) } @@ -349,7 +350,7 @@ func qualifiersMatch(metadata map[string]any, qualifiers map[string]string, allo if !ok || got == "" || got == want { continue } - if key == qualifierPlatform && got == universalPlatform && allowUniversal { + if key == qualifierPlatform && (got == universalPlatform || got == rubyPlatform) && allowUniversal { continue } return false diff --git a/fetch/rubygems_resolver_test.go b/fetch/rubygems_resolver_test.go new file mode 100644 index 0000000..ba3f7df --- /dev/null +++ b/fetch/rubygems_resolver_test.go @@ -0,0 +1,73 @@ +package fetch + +import ( + "context" + "errors" + "io" + "net/http" + "strings" + "testing" + + "github.com/git-pkgs/registries" + "github.com/git-pkgs/registries/client" + _ "github.com/git-pkgs/registries/internal/rubygems" +) + +type rubyGemsVersions map[string]string + +func (v rubyGemsVersions) RoundTrip(req *http.Request) (*http.Response, error) { + name := strings.TrimSuffix(strings.TrimPrefix(req.URL.Path, "/api/v1/versions/"), ".json") + body, ok := v[name] + status := http.StatusOK + if !ok { + body, status = "Not Found", http.StatusNotFound + } + return &http.Response{StatusCode: status, Header: http.Header{"Content-Type": {"application/json"}}, + Body: io.NopCloser(strings.NewReader(body)), Request: req}, nil +} + +// The platform qualifier picks a gem's platform build. A ruby build installs +// on any platform, and a build for the platform asked for wins over it. +func TestResolveRubyGemsPlatform(t *testing.T) { + builds := rubyGemsVersions{ + "jruby-launcher": `[{"number": "1.1.2", "platform": "java"}]`, + "nokogiri": `[{"number": "1.13.6", "platform": "ruby"}, {"number": "1.13.6", "platform": "x86_64-linux"}]`, + "rake": `[{"number": "13.2.1", "platform": "ruby"}]`, + } + httpClient := client.NewClient(client.WithHTTPClient(&http.Client{Transport: builds})) + reg, err := registries.New("gem", "https://gems.example.test", httpClient) + if err != nil { + t.Fatal(err) + } + r := NewResolver() + r.RegisterRegistry(reg) + + tests := []struct { + name, gem, version, platform, want string + }{ + {"platform-only version", "jruby-launcher", "1.1.2", "java", "jruby-launcher-1.1.2-java.gem"}, + {"both, platform build wins", "nokogiri", "1.13.6", "x86_64-linux", "nokogiri-1.13.6-x86_64-linux.gem"}, + {"both, ruby asked for", "nokogiri", "1.13.6", "ruby", "nokogiri-1.13.6.gem"}, + {"both, other platform gets ruby", "nokogiri", "1.13.6", "arm64-darwin", "nokogiri-1.13.6.gem"}, + {"ruby-only version", "rake", "13.2.1", "x86_64-linux", "rake-13.2.1.gem"}, + {"no build for the platform", "jruby-launcher", "1.1.2", "x86_64-linux", ""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + info, err := r.ResolveWithOptions(context.Background(), "gem", tt.gem, tt.version, + ResolveOptions{Qualifiers: map[string]string{"platform": tt.platform}}) + if tt.want == "" { + if !errors.Is(err, ErrNoMatchingArtifact) { + t.Fatalf("Resolve = %+v, %v; want ErrNoMatchingArtifact", info, err) + } + return + } + if err != nil { + t.Fatalf("Resolve failed: %v", err) + } + if info.Filename != tt.want || info.URL != "https://gems.example.test/downloads/"+tt.want { + t.Errorf("got %s (%s), want %s", info.Filename, info.URL, tt.want) + } + }) + } +} diff --git a/internal/rubygems/rubygems.go b/internal/rubygems/rubygems.go index 8bac6f6..cd7d8ae 100644 --- a/internal/rubygems/rubygems.go +++ b/internal/rubygems/rubygems.go @@ -14,6 +14,8 @@ import ( const ( DefaultURL = "https://rubygems.org" ecosystem = "gem" + // rubyPlatform is the platform of a gem not built for any one, which installs on all of them. + rubyPlatform = "ruby" ) func init() { @@ -144,25 +146,32 @@ func (r *Registry) FetchVersions(ctx context.Context, name string) ([]core.Versi return nil, err } - versions := make([]core.Version, len(resp)) - for i, v := range resp { + // Each platform build of a number is an artifact of that version, as Open VSX + // builds are; the version's own fields come from the ruby build when there is one. + var versions []core.Version + index := make(map[string]int) + for _, v := range resp { var publishedAt time.Time if v.CreatedAt != "" { publishedAt, _ = time.Parse(time.RFC3339, v.CreatedAt) } - number := v.Number - if v.Platform != "" && v.Platform != "ruby" { - number = fmt.Sprintf("%s-%s", v.Number, v.Platform) - } - var integrity string if v.SHA != "" { integrity = "sha256-" + v.SHA } - versions[i] = core.Version{ - Number: number, + platform := v.Platform + if platform == "" { + platform = rubyPlatform + } + file := fmt.Sprintf("%s-%s.gem", name, v.Number) + if platform != rubyPlatform { + file = fmt.Sprintf("%s-%s-%s.gem", name, v.Number, platform) + } + + version := core.Version{ + Number: v.Number, PublishedAt: publishedAt, Licenses: strings.Join(v.Licenses, ","), Integrity: integrity, @@ -174,6 +183,21 @@ func (r *Registry) FetchVersions(ctx context.Context, name string) ([]core.Versi "prerelease": v.Prerelease, }, } + i, ok := index[v.Number] + if !ok { + i = len(versions) + index[v.Number] = i + versions = append(versions, version) + } else if platform == rubyPlatform { + version.Artifacts = versions[i].Artifacts + versions[i] = version + } + versions[i].Artifacts = append(versions[i].Artifacts, core.Artifact{ + URL: fmt.Sprintf("%s/downloads/%s", r.baseURL, file), + Filename: file, + Integrity: integrity, + Metadata: map[string]any{"platform": platform}, + }) } return versions, nil diff --git a/internal/rubygems/rubygems_test.go b/internal/rubygems/rubygems_test.go index 2bbeec3..cbebdf1 100644 --- a/internal/rubygems/rubygems_test.go +++ b/internal/rubygems/rubygems_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "reflect" "testing" "github.com/git-pkgs/registries/internal/core" @@ -140,6 +141,12 @@ func TestFetchVersions(t *testing.T) { Licenses: []string{"MIT"}, SHA: "3fa37b0c3b5744af45f9da3e4ae9cbd89480b35e12ae36b5e87a0452e0b38335", }, + { + Number: "1.13.5", + Platform: "java", + CreatedAt: "2022-04-12T10:00:00.000Z", + SHA: "0b0e5b2c9f1c8e9d3b5a0c7a6f4e2d1c0b9a8f7e6d5c4b3a2f1e0d9c8b7a6f5e", + }, } w.Header().Set("Content-Type", "application/json") @@ -157,15 +164,35 @@ func TestFetchVersions(t *testing.T) { t.Fatalf("expected 2 versions, got %d", len(versions)) } - if versions[0].Number != "1.13.6" { - t.Errorf("expected version '1.13.6', got %q", versions[0].Number) - } - if versions[1].Number != "1.13.6-x86_64-linux" { - t.Errorf("expected version '1.13.6-x86_64-linux', got %q", versions[1].Number) + if versions[0].Number != "1.13.6" || versions[1].Number != "1.13.5" { + t.Errorf("versions = %q, %q; want 1.13.6, 1.13.5", versions[0].Number, versions[1].Number) } if versions[0].Integrity != "sha256-b1512fdc0aba446e1ee30de3e0671518eb363e75fab53486e99e8891d44b8587" { t.Errorf("unexpected integrity: %q", versions[0].Integrity) } + + type build struct{ filename, platform, integrity string } + builds := func(v core.Version) []build { + var out []build + for _, a := range v.Artifacts { + if a.URL != server.URL+"/downloads/"+a.Filename { + t.Errorf("artifact URL %q for %s", a.URL, a.Filename) + } + out = append(out, build{a.Filename, a.Metadata["platform"].(string), a.Integrity}) + } + return out + } + want := []build{ + {"nokogiri-1.13.6.gem", "ruby", "sha256-b1512fdc0aba446e1ee30de3e0671518eb363e75fab53486e99e8891d44b8587"}, + {"nokogiri-1.13.6-x86_64-linux.gem", "x86_64-linux", "sha256-3fa37b0c3b5744af45f9da3e4ae9cbd89480b35e12ae36b5e87a0452e0b38335"}, + } + if got := builds(versions[0]); !reflect.DeepEqual(got, want) { + t.Errorf("1.13.6 artifacts = %+v, want %+v", got, want) + } + want = []build{{"nokogiri-1.13.5-java.gem", "java", "sha256-0b0e5b2c9f1c8e9d3b5a0c7a6f4e2d1c0b9a8f7e6d5c4b3a2f1e0d9c8b7a6f5e"}} + if got := builds(versions[1]); !reflect.DeepEqual(got, want) { + t.Errorf("1.13.5 artifacts = %+v, want %+v", got, want) + } } func TestFetchDependencies(t *testing.T) {