Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions fetch/packagist_resolver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package fetch

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/git-pkgs/registries"
_ "github.com/git-pkgs/registries/internal/packagist"
)

func TestResolvePackagistDistURL(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/packages/monolog/monolog.json" {
http.NotFound(w, r)
return
}
_, _ = w.Write([]byte(`{"package":{"name":"monolog/monolog","versions":{"3.5.0":{"version":"3.5.0",
"dist":{"type":"zip","url":"https://api.github.com/repos/Seldaek/monolog/zipball/c915e2634718dbc8a4a15c61b0e62e7a44e14448","shasum":"abc123"}}}}}`))
}))
defer server.Close()

reg, err := registries.New("composer", server.URL, registries.DefaultClient())
if err != nil {
t.Fatal(err)
}
r := NewResolver()
r.RegisterRegistry(reg)

info, err := r.Resolve(context.Background(), "composer", "monolog/monolog", "3.5.0")
if err != nil {
t.Fatalf("Resolve failed: %v", err)
}
if want := "https://api.github.com/repos/Seldaek/monolog/zipball/c915e2634718dbc8a4a15c61b0e62e7a44e14448"; info.URL != want {
t.Errorf("URL = %q, want %q", info.URL, want)
}
if want := "c915e2634718dbc8a4a15c61b0e62e7a44e14448.zip"; info.Filename != want {
t.Errorf("Filename = %q, want %q", info.Filename, want)
}
if info.Integrity != "sha1-abc123" {
t.Errorf("Integrity = %q, want sha1-abc123", info.Integrity)
}
}
26 changes: 26 additions & 0 deletions internal/packagist/packagist.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package packagist
import (
"context"
"fmt"
"net/url"
"path"
"strings"
"time"

Expand Down Expand Up @@ -176,12 +178,22 @@ func (r *Registry) FetchVersions(ctx context.Context, name string) ([]core.Versi
status = core.StatusDeprecated
}

var artifacts []core.Artifact
if v.Dist.URL != "" {
artifacts = []core.Artifact{{
URL: v.Dist.URL,
Filename: distFilename(v.Dist.URL, v.Dist.Type),
Integrity: integrity,
}}
}

versions = append(versions, core.Version{
Number: v.Version,
PublishedAt: publishedAt,
Licenses: strings.Join(v.License, ","),
Integrity: integrity,
Status: status,
Artifacts: artifacts,
Metadata: map[string]any{
"dist_url": v.Dist.URL,
"dist_type": v.Dist.Type,
Expand All @@ -192,6 +204,20 @@ func (r *Registry) FetchVersions(ctx context.Context, name string) ([]core.Versi
return versions, nil
}

// distFilename is the last path segment of a dist URL. GitHub zipball URLs
// end in a bare commit hash, so zip dists without an extension get ".zip".
func distFilename(rawURL, distType string) string {
name := rawURL
if u, err := url.Parse(rawURL); err == nil {
name = u.Path
}
name = path.Base(name)
if path.Ext(name) == "" && distType == "zip" {
name += ".zip"
}
return name
}

func (r *Registry) FetchDependencies(ctx context.Context, name, version string) ([]core.Dependency, error) {
url := fmt.Sprintf("%s/packages/%s.json", r.baseURL, name)

Expand Down
34 changes: 34 additions & 0 deletions internal/packagist/packagist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"

"github.com/git-pkgs/registries/internal/core"
Expand Down Expand Up @@ -231,3 +232,36 @@ func TestEcosystem(t *testing.T) {
t.Errorf("expected ecosystem 'composer', got %q", reg.Ecosystem())
}
}

func TestFetchVersionsArtifacts(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`{"package":{"name":"monolog/monolog","versions":{
"3.5.0":{"version":"3.5.0","dist":{"type":"zip","url":"https://api.github.com/repos/Seldaek/monolog/zipball/c915e2634718dbc8a4a15c61b0e62e7a44e14448","shasum":"abc123"}},
"3.4.0":{"version":"3.4.0","dist":{"type":"tar","url":"https://example.com/dist/monolog-3.4.0.tar?token=x"}},
"dev-main":{"version":"dev-main"}}}}`))
}))
defer server.Close()

versions, err := New(server.URL, core.DefaultClient()).FetchVersions(context.Background(), "monolog/monolog")
if err != nil {
t.Fatalf("FetchVersions failed: %v", err)
}

want := map[string][]core.Artifact{
"3.5.0": {{
URL: "https://api.github.com/repos/Seldaek/monolog/zipball/c915e2634718dbc8a4a15c61b0e62e7a44e14448",
Filename: "c915e2634718dbc8a4a15c61b0e62e7a44e14448.zip",
Integrity: "sha1-abc123",
}},
"3.4.0": {{
URL: "https://example.com/dist/monolog-3.4.0.tar?token=x",
Filename: "monolog-3.4.0.tar",
}},
"dev-main": nil,
}
for _, v := range versions {
if !reflect.DeepEqual(v.Artifacts, want[v.Number]) {
t.Errorf("%s artifacts = %+v, want %+v", v.Number, v.Artifacts, want[v.Number])
}
}
}
Loading