aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/api/api.go66
-rw-r--r--internal/api/api_test.go74
-rw-r--r--internal/datasource.go2
-rw-r--r--internal/fetchdatasource/fetchdatasource.go5
-rw-r--r--internal/frontend/server.go1
-rw-r--r--internal/testing/fakedatasource/fakedatasource.go50
6 files changed, 161 insertions, 37 deletions
diff --git a/internal/api/api.go b/internal/api/api.go
index 2b0f3f4f..3b653fd8 100644
--- a/internal/api/api.go
+++ b/internal/api/api.go
@@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"net/http"
+ "strconv"
"strings"
"golang.org/x/pkgsite/internal"
@@ -210,7 +211,35 @@ func ServeModule(w http.ResponseWriter, r *http.Request, ds internal.DataSource)
}
}
- // Future: handle licenses param.
+ return serveJSON(w, http.StatusOK, resp)
+}
+
+// ServeModuleVersions handles requests for the v1 module versions endpoint.
+func ServeModuleVersions(w http.ResponseWriter, r *http.Request, ds internal.DataSource) (err error) {
+ defer derrors.Wrap(&err, "ServeModuleVersions")
+
+ path := strings.TrimPrefix(r.URL.Path, "/v1/versions/")
+ if path == "" {
+ return serveErrorJSON(w, http.StatusBadRequest, "missing path", nil)
+ }
+
+ var params VersionsParams
+ if err := ParseParams(r.URL.Query(), &params); err != nil {
+ return serveErrorJSON(w, http.StatusBadRequest, err.Error(), nil)
+ }
+
+ infos, err := ds.GetVersionsForPath(r.Context(), path)
+ if err != nil {
+ if errors.Is(err, derrors.NotFound) {
+ return serveErrorJSON(w, http.StatusNotFound, err.Error(), nil)
+ }
+ return err
+ }
+
+ resp, err := paginate(infos, params.ListParams, 100)
+ if err != nil {
+ return serveErrorJSON(w, http.StatusBadRequest, err.Error(), nil)
+ }
return serveJSON(w, http.StatusOK, resp)
}
@@ -238,3 +267,38 @@ func serveErrorJSON(w http.ResponseWriter, status int, message string, candidate
Candidates: candidates,
})
}
+
+func paginate[T any](all []T, lp ListParams, defaultLimit int) (PaginatedResponse[T], error) {
+ limit := lp.Limit
+ if limit <= 0 {
+ limit = defaultLimit
+ }
+
+ offset := 0
+ if lp.Token != "" {
+ var err error
+ offset, err = strconv.Atoi(lp.Token)
+ if err != nil || offset < 0 {
+ return PaginatedResponse[T]{}, errors.New("invalid token")
+ }
+ }
+
+ if offset > len(all) {
+ offset = len(all)
+ }
+ end := offset + limit
+ if end > len(all) {
+ end = len(all)
+ }
+
+ var nextToken string
+ if end < len(all) {
+ nextToken = strconv.Itoa(end)
+ }
+
+ return PaginatedResponse[T]{
+ Items: all[offset:end],
+ Total: len(all),
+ NextPageToken: nextToken,
+ }, nil
+}
diff --git a/internal/api/api_test.go b/internal/api/api_test.go
index f61c8581..9112c576 100644
--- a/internal/api/api_test.go
+++ b/internal/api/api_test.go
@@ -247,3 +247,77 @@ func TestServeModule(t *testing.T) {
})
}
}
+
+func TestServeModuleVersions(t *testing.T) {
+ ctx := context.Background()
+ ds := fakedatasource.New()
+
+ ds.MustInsertModule(ctx, &internal.Module{
+ ModuleInfo: internal.ModuleInfo{ModulePath: "example.com", Version: "v1.0.0"},
+ Units: []*internal.Unit{{UnitMeta: internal.UnitMeta{Path: "example.com"}}},
+ })
+ ds.MustInsertModule(ctx, &internal.Module{
+ ModuleInfo: internal.ModuleInfo{ModulePath: "example.com", Version: "v1.1.0"},
+ Units: []*internal.Unit{{UnitMeta: internal.UnitMeta{Path: "example.com"}}},
+ })
+ ds.MustInsertModule(ctx, &internal.Module{
+ ModuleInfo: internal.ModuleInfo{ModulePath: "example.com/v2", Version: "v2.0.0"},
+ Units: []*internal.Unit{{UnitMeta: internal.UnitMeta{Path: "example.com/v2"}}},
+ })
+
+ for _, test := range []struct {
+ name string
+ url string
+ wantStatus int
+ wantCount int
+ }{
+ {
+ name: "all versions (cross-major)",
+ url: "/v1/versions/example.com",
+ wantStatus: http.StatusOK,
+ wantCount: 3,
+ },
+ {
+ name: "with limit",
+ url: "/v1/versions/example.com?limit=1",
+ wantStatus: http.StatusOK,
+ wantCount: 1,
+ },
+ {
+ name: "pagination",
+ url: "/v1/versions/example.com?limit=1&token=1",
+ wantStatus: http.StatusOK,
+ wantCount: 1,
+ },
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ r := httptest.NewRequest("GET", test.url, nil)
+ w := httptest.NewRecorder()
+
+ err := ServeModuleVersions(w, r, ds)
+ if err != nil {
+ t.Fatalf("ServeModuleVersions returned error: %v", err)
+ }
+
+ if w.Code != test.wantStatus {
+ t.Errorf("status = %d, want %d", w.Code, test.wantStatus)
+ }
+
+ if test.wantStatus == http.StatusOK {
+ var got PaginatedResponse[internal.ModuleInfo]
+ if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
+ t.Fatalf("json.Unmarshal: %v", err)
+ }
+ if len(got.Items) != test.wantCount {
+ t.Errorf("count = %d, want %d", len(got.Items), test.wantCount)
+ }
+ if test.name == "with limit" && got.NextPageToken != "1" {
+ t.Errorf("nextPageToken = %q, want %q", got.NextPageToken, "1")
+ }
+ if test.name == "pagination" && got.NextPageToken != "2" {
+ t.Errorf("nextPageToken = %q, want %q", got.NextPageToken, "2")
+ }
+ }
+ })
+ }
+}
diff --git a/internal/datasource.go b/internal/datasource.go
index 48117c4a..912b54de 100644
--- a/internal/datasource.go
+++ b/internal/datasource.go
@@ -91,6 +91,8 @@ type DataSource interface {
// GetLatestInfo gets information about the latest versions of a unit and module.
// See LatestInfo for documentation.
GetLatestInfo(ctx context.Context, unitPath, modulePath string, latestUnitMeta *UnitMeta) (LatestInfo, error)
+ // GetVersionsForPath returns a list of versions for the given path.
+ GetVersionsForPath(ctx context.Context, path string) ([]*ModuleInfo, error)
// SearchSupport reports the search types supported by this datasource.
SearchSupport() SearchSupport
diff --git a/internal/fetchdatasource/fetchdatasource.go b/internal/fetchdatasource/fetchdatasource.go
index 6676d89f..41573ae5 100644
--- a/internal/fetchdatasource/fetchdatasource.go
+++ b/internal/fetchdatasource/fetchdatasource.go
@@ -350,6 +350,11 @@ func (ds *FetchDataSource) GetNestedModules(ctx context.Context, modulePath stri
return nil, nil
}
+// GetVersionsForPath is not implemented.
+func (ds *FetchDataSource) GetVersionsForPath(ctx context.Context, path string) ([]*internal.ModuleInfo, error) {
+ return nil, nil
+}
+
// GetModuleReadme is not implemented.
func (*FetchDataSource) GetModuleReadme(ctx context.Context, modulePath, resolvedVersion string) (*internal.Readme, error) {
return nil, nil
diff --git a/internal/frontend/server.go b/internal/frontend/server.go
index 00dc7ad6..b229af0d 100644
--- a/internal/frontend/server.go
+++ b/internal/frontend/server.go
@@ -238,6 +238,7 @@ func (s *Server) Install(handle func(string, http.Handler), cacher Cacher, authV
handle("GET /vuln/", vulnHandler)
handle("GET /v1/package/", s.errorHandler(api.ServePackage))
handle("GET /v1/module/", s.errorHandler(api.ServeModule))
+ handle("GET /v1/versions/", s.errorHandler(api.ServeModuleVersions))
handle("/opensearch.xml", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
serveFileFS(w, r, s.staticFS, "shared/opensearch.xml")
}))
diff --git a/internal/testing/fakedatasource/fakedatasource.go b/internal/testing/fakedatasource/fakedatasource.go
index 30c71a18..46de2a8d 100644
--- a/internal/testing/fakedatasource/fakedatasource.go
+++ b/internal/testing/fakedatasource/fakedatasource.go
@@ -244,7 +244,7 @@ func findUnit(m *internal.Module, path string) *internal.Unit {
return nil
}
-// GetModuleReadme gets the readme for the module.
+// GetModuleReadme is not implemented.
func (ds *FakeDataSource) GetModuleReadme(ctx context.Context, modulePath, resolvedVersion string) (*internal.Readme, error) {
m := ds.getModule(modulePath, resolvedVersion)
if m == nil {
@@ -397,26 +397,23 @@ func (ds *FakeDataSource) GetVersionMaps(ctx context.Context, paths []string, re
// descending semver order if any exist. If none, it returns the 10 most
// recent from a list of pseudo-versions sorted in descending semver order.
func (ds *FakeDataSource) GetVersionsForPath(ctx context.Context, path string) ([]*internal.ModuleInfo, error) {
- var infos []*internal.ModuleInfo
-
+ var targetV1Path string
for _, m := range ds.modules {
- if m.ModulePath == "std" {
- for _, u := range m.Units {
- if u.Path == path {
- infos = append(infos, &m.ModuleInfo)
- continue
- }
- }
- }
- prefix, _, _ := module.SplitPathVersion(m.ModulePath)
- if !strings.HasPrefix(path, prefix) {
- continue // different module
+ if findUnit(m, path) != nil {
+ targetV1Path = internal.V1Path(path, m.ModulePath)
+ break
}
- pathSuffix := trimSlashVersionPrefix(strings.TrimPrefix(path, prefix))
+ }
+ if targetV1Path == "" {
+ return nil, nil
+ }
+
+ var infos []*internal.ModuleInfo
+ for _, m := range ds.modules {
for _, u := range m.Units {
- unitSuffix := trimSlashVersionPrefix(strings.TrimPrefix(u.Path, prefix))
- if unitSuffix == pathSuffix {
+ if internal.V1Path(u.Path, m.ModulePath) == targetV1Path {
infos = append(infos, &m.ModuleInfo)
+ break
}
}
}
@@ -443,25 +440,6 @@ func (ds *FakeDataSource) GetVersionsForPath(ctx context.Context, path string) (
return infos, nil
}
-// trimSlashVersionPrefix trims a /vN path component prefix if one is present in path,
-// and returns path unchanged otherwise.
-func trimSlashVersionPrefix(path string) string {
- if !strings.HasPrefix(path, "/v") {
- return path
- }
- trimSlash := path[len("/"):]
- endOfPathComponent := strings.Index(trimSlash, "/")
- if endOfPathComponent == -1 {
- endOfPathComponent = len(trimSlash)
- }
- vComponent := trimSlash[:endOfPathComponent] // first component of the path
- if m := semver.Major(vComponent); m == "" || m != vComponent {
- return path
- }
- return trimSlash[endOfPathComponent:]
-
-}
-
// InsertModule inserts m into the FakeDataSource. It is only implemented for
// lmv == nil.
func (ds *FakeDataSource) InsertModule(ctx context.Context, m *internal.Module, lmv *internal.LatestModuleVersions) (isLatest bool, err error) {