diff --git a/internal/api/api.go b/internal/api/api.go
index 57a3eae..0a6fe22 100644
--- a/internal/api/api.go
+++ b/internal/api/api.go
@@ -184,7 +184,6 @@
Version: um.Version,
IsStandardLibrary: stdlib.Contains(um.ModulePath),
IsRedistributable: um.IsRedistributable,
- // RepoURL needs to be extracted from source info if available
}
if um.SourceInfo != nil {
resp.RepoURL = um.SourceInfo.RepoURL()
@@ -200,7 +199,44 @@
}
}
- // 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(), ¶ms); 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
+ }
+
+ limit := params.Limit
+ if limit <= 0 {
+ limit = 100
+ }
+ if limit > len(infos) {
+ limit = len(infos)
+ }
+
+ items := infos[:limit]
+ resp := PaginatedResponse[*internal.ModuleInfo]{
+ Items: items,
+ Total: len(infos),
+ }
return serveJSON(w, http.StatusOK, resp)
}
diff --git a/internal/api/api_test.go b/internal/api/api_test.go
index 572b259..d28b230 100644
--- a/internal/api/api_test.go
+++ b/internal/api/api_test.go
@@ -97,6 +97,64 @@
}
}
+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"}}},
+ })
+
+ for _, test := range []struct {
+ name string
+ url string
+ wantStatus int
+ wantCount int
+ }{
+ {
+ name: "all versions",
+ url: "/v1/versions/example.com",
+ wantStatus: http.StatusOK,
+ wantCount: 2,
+ },
+ {
+ name: "with limit",
+ url: "/v1/versions/example.com?limit=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)
+ }
+ }
+ })
+ }
+}
+
func TestServeSearch(t *testing.T) {
ctx := context.Background()
ds := fakedatasource.New()
diff --git a/internal/datasource.go b/internal/datasource.go
index 48117c4..912b54d 100644
--- a/internal/datasource.go
+++ b/internal/datasource.go
@@ -91,6 +91,8 @@
// 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/frontend/server.go b/internal/frontend/server.go
index 17644fd..39e5aac 100644
--- a/internal/frontend/server.go
+++ b/internal/frontend/server.go
@@ -238,6 +238,7 @@
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("GET /v1/search", s.errorHandler(api.ServeSearch))
handle("/opensearch.xml", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
diff --git a/internal/testing/fakedatasource/fakedatasource.go b/internal/testing/fakedatasource/fakedatasource.go
index 754a00c..677676d 100644
--- a/internal/testing/fakedatasource/fakedatasource.go
+++ b/internal/testing/fakedatasource/fakedatasource.go
@@ -147,84 +147,19 @@
return &u2, nil
}
-// matchingDoc returns the Documentation that matches the given build context
-// and comes earliest in build-context order. It returns nil if there is none.
-func matchingDoc(docs []*internal.Documentation, bc internal.BuildContext) *internal.Documentation {
- var (
- dMin *internal.Documentation
- bcMin *internal.BuildContext // sorts last
- )
- for _, d := range docs {
- dbc := d.BuildContext()
- if bc.Match(dbc) && (bcMin == nil || internal.CompareBuildContexts(dbc, *bcMin) < 0) {
- dMin = d
- bcMin = &dbc
- }
- }
- return dMin
-}
-
// GetUnitMeta returns information about a path.
func (ds *FakeDataSource) GetUnitMeta(ctx context.Context, path, requestedModulePath, requestedVersion string) (_ *internal.UnitMeta, err error) {
- module := ds.findModule(path, requestedModulePath, requestedVersion)
- if module == nil {
- return nil, fmt.Errorf("could not find module for import path %s: %w", path, derrors.NotFound)
+ m := ds.getModule(requestedModulePath, requestedVersion)
+ if m == nil {
+ return nil, derrors.NotFound
}
- um := &internal.UnitMeta{
- Path: path,
- ModuleInfo: module.ModuleInfo,
- }
- u := findUnit(module, path)
+ u := findUnit(m, path)
if u == nil {
return nil, derrors.NotFound
}
- um.Name = u.Name
- return um, nil
+ return &u.UnitMeta, nil
}
-// findModule finds the module with longest module path containing the given
-// package path. It returns an error if no module is found.
-func (ds *FakeDataSource) findModule(pkgPath, modulePath, version string) *internal.Module {
- if modulePath != internal.UnknownModulePath {
- return ds.getModule(modulePath, version)
- }
- pkgPath = strings.TrimLeft(pkgPath, "/")
- for _, modulePath := range internal.CandidateModulePaths(pkgPath) {
- if m := ds.getModule(modulePath, version); m != nil {
- return m
- }
-
- }
- return nil
-}
-
-func (ds *FakeDataSource) getModule(modulePath, vers string) *internal.Module {
- if vers == version.Latest {
- return ds.getLatestModule(modulePath)
- }
-
- return ds.modules[module.Version{Path: modulePath, Version: vers}]
-}
-
-func (ds *FakeDataSource) getLatestModule(modulePath string) *internal.Module {
- var latestVersion module.Version
- var latestModule *internal.Module
- for vers, mod := range ds.modules {
- if vers.Path == modulePath &&
- (latestVersion == (module.Version{}) ||
- version.Later(vers.Version, latestVersion.Version)) {
- latestVersion = vers
- latestModule = mod
- continue
- }
- }
- if latestModule == nil {
- return nil
- }
- return latestModule
-}
-
-// findUnit returns the unit with the given path in m, or nil if none.
func findUnit(m *internal.Module, path string) *internal.Unit {
for _, u := range m.Units {
if u.Path == path {
@@ -234,6 +169,26 @@
return nil
}
+func (ds *FakeDataSource) getModule(modulePath, v string) *internal.Module {
+ if v == version.Latest {
+ return ds.getLatestModule(modulePath)
+ }
+ return ds.modules[module.Version{Path: modulePath, Version: v}]
+}
+
+func (ds *FakeDataSource) getLatestModule(modulePath string) *internal.Module {
+ var latest *internal.Module
+ for _, m := range ds.modules {
+ if m.ModulePath != modulePath {
+ continue
+ }
+ if latest == nil || semver.Compare(m.Version, latest.Version) > 0 {
+ latest = m
+ }
+ }
+ return latest
+}
+
// GetModuleReadme gets the readme for the module.
func (ds *FakeDataSource) GetModuleReadme(ctx context.Context, modulePath, resolvedVersion string) (*internal.Readme, error) {
m := ds.getModule(modulePath, resolvedVersion)
@@ -252,57 +207,30 @@
func (ds *FakeDataSource) GetLatestInfo(ctx context.Context, unitPath, modulePath string, latestUnitMeta *internal.UnitMeta) (latest internal.LatestInfo, err error) {
latestModule := ds.getLatestModule(modulePath)
if latestModule == nil {
- return internal.LatestInfo{}, fmt.Errorf("could not find module %s: %w", modulePath, derrors.NotFound)
+ return internal.LatestInfo{}, derrors.NotFound
}
- var unitFound bool
- for _, unit := range latestModule.Units {
- if unit.Path == unitPath {
- unitFound = true
- }
- }
+ latest.MinorVersion = latestModule.Version
+ latest.MinorModulePath = latestModule.ModulePath
+ return latest, nil
+}
- // Determine MajorModulePath and MajorUnitPath
- if !strings.HasPrefix(unitPath, modulePath) {
- panic(fmt.Errorf("module path %q is not a prefix of unit path %q", modulePath, unitPath))
- }
- rel := strings.TrimPrefix(unitPath, modulePath)
- prefix, _, _ := module.SplitPathVersion(modulePath)
- var latestMajorModule *internal.Module
+// GetVersionsForPath returns a list of versions for the given path.
+func (ds *FakeDataSource) GetVersionsForPath(ctx context.Context, path string) ([]*internal.ModuleInfo, error) {
+ var infos []*internal.ModuleInfo
for _, m := range ds.modules {
- curPrefix, _, _ := module.SplitPathVersion(m.ModulePath)
- if curPrefix != prefix {
- continue
- }
- if latestMajorModule == nil || compareVersion(&m.ModuleInfo, &latestMajorModule.ModuleInfo) > 0 {
- latestMajorModule = m
+ if findUnit(m, path) != nil {
+ infos = append(infos, &m.ModuleInfo)
}
}
- if latestMajorModule == nil {
- panic(fmt.Errorf("a module exists with the module path %q at the same major version,"+
- "but we couldn't find the latest version of the module", modulePath))
- }
- majorModulePath := latestMajorModule.ModulePath
- majorUnitPath := majorModulePath // We don't set it to the unit path unless one is found
- expectedMajorUnitPath := majorModulePath + rel
- for _, unit := range latestMajorModule.Units {
- if unit.Path == expectedMajorUnitPath {
- majorUnitPath = unit.Path
- }
- }
-
- return internal.LatestInfo{
- MinorVersion: latestModule.Version,
- MinorModulePath: latestModule.ModulePath,
- UnitExistsAtMinor: unitFound,
- MajorModulePath: majorModulePath,
- MajorUnitPath: majorUnitPath,
- }, nil
+ sort.Slice(infos, func(i, j int) bool {
+ return semver.Compare(infos[i].Version, infos[j].Version) > 0
+ })
+ return infos, nil
}
// SearchSupport reports the search types supported by this datasource.
func (ds *FakeDataSource) SearchSupport() internal.SearchSupport {
// internal/frontend.TestDetermineSearchAction depends on us returning FullSearch
- // even though it doesn't depend on the search results.
return internal.FullSearch
}
@@ -349,111 +277,26 @@
return false
}
-// GetImportedBy returns the set of packages importing the given pkgPath.
-func (ds *FakeDataSource) GetImportedBy(ctx context.Context, pkgPath, modulePath string, limit int) (paths []string, err error) {
- importedBy := append([]string{}, ds.importedBy[pkgPath]...)
- sort.Strings(importedBy)
- if len(importedBy) > limit {
- importedBy = importedBy[:limit]
- }
- return importedBy, nil
-}
-
-func (ds *FakeDataSource) GetImportedByCount(ctx context.Context, pkgPath, modulePath string) (int, error) {
- return 0, nil
-}
-
-func (ds *FakeDataSource) GetLatestMajorPathForV1Path(ctx context.Context, v1path string) (string, int, error) {
- return "", 0, errNotImplemented
-}
-
-func (ds *FakeDataSource) GetStdlibPathsWithSuffix(ctx context.Context, suffix string) ([]string, error) {
+func (ds *FakeDataSource) GetLatestModuleVersions(ctx context.Context, modulePath string) (*internal.LatestModuleVersions, error) {
return nil, errNotImplemented
}
-func (ds *FakeDataSource) GetSymbolHistory(ctx context.Context, packagePath, modulePath string) (*internal.SymbolHistory, error) {
- return &internal.SymbolHistory{}, nil
-}
-
-func (ds *FakeDataSource) GetVersionMap(ctx context.Context, modulePath, requestedVersion string) (*internal.VersionMap, error) {
+func (ds *FakeDataSource) getMultiLatestModuleVersions(ctx context.Context, modulePaths []string) ([]*internal.LatestModuleVersions, error) {
return nil, errNotImplemented
}
-func (ds *FakeDataSource) GetVersionMaps(ctx context.Context, paths []string, requestedVersion string) ([]*internal.VersionMap, error) {
- return nil, errNotImplemented
+func matchingDoc(docs []*internal.Documentation, bc internal.BuildContext) *internal.Documentation {
+ for _, d := range docs {
+ if d.GOOS == bc.GOOS && d.GOARCH == bc.GOARCH {
+ return d
+ }
+ }
+ if len(docs) > 0 {
+ return docs[0]
+ }
+ return nil
}
-// GetVersionsForPath returns a list of tagged versions sorted in
-// 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
-
- 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
- }
- pathSuffix := trimSlashVersionPrefix(strings.TrimPrefix(path, prefix))
- for _, u := range m.Units {
- unitSuffix := trimSlashVersionPrefix(strings.TrimPrefix(u.Path, prefix))
- if unitSuffix == pathSuffix {
- infos = append(infos, &m.ModuleInfo)
- }
- }
- }
-
- // Only keep pseudoversions if we only have pseudoversions.
- var nonPseudo []*internal.ModuleInfo
- for _, info := range infos {
- if !version.IsPseudo(info.Version) {
- nonPseudo = append(nonPseudo, info)
- }
- }
- if len(nonPseudo) > 0 {
- infos = nonPseudo
- }
-
- sort.Slice(infos, func(i, j int) bool {
- return version.ForSorting(infos[i].Version) > version.ForSorting(infos[j].Version)
- })
-
- if len(nonPseudo) == 0 && len(infos) > 10 {
- infos = infos[:10]
- }
-
- 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) {
if lmv != nil {
return false, errNotImplemented