aboutsummaryrefslogtreecommitdiff
path: root/internal/api
diff options
context:
space:
mode:
authorEthan Lee <ethanalee@google.com>2026-03-24 20:45:28 +0000
committerGopher Robot <gobot@golang.org>2026-04-02 08:01:06 -0700
commit2d0d60d0e456af02dfc52d79053d5a3a20fb11ff (patch)
tree62a8e4c6534dd434e8eb2b45fa152033624e4b41 /internal/api
parentaed8d2b9be2361625f8b305b91885f7c1a710f07 (diff)
downloadgo-x-pkgsite-2d0d60d0e456af02dfc52d79053d5a3a20fb11ff.tar.xz
internal/api: implement filtering logic based on query params
Change-Id: Ica73fe1f12b2c65d08240479d1a135a51c3ae566 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/758822 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> kokoro-CI: kokoro <noreply+kokoro@google.com> Auto-Submit: Ethan Lee <ethanalee@google.com> Reviewed-by: Jonathan Amsterdam <jba@google.com>
Diffstat (limited to 'internal/api')
-rw-r--r--internal/api/api.go44
-rw-r--r--internal/api/api_test.go28
2 files changed, 63 insertions, 9 deletions
diff --git a/internal/api/api.go b/internal/api/api.go
index 31b325ba..867d1d1d 100644
--- a/internal/api/api.go
+++ b/internal/api/api.go
@@ -214,6 +214,16 @@ func ServeModuleVersions(w http.ResponseWriter, r *http.Request, ds internal.Dat
return err
}
+ if params.Filter != "" {
+ var filtered []*internal.ModuleInfo
+ for _, info := range infos {
+ if strings.Contains(info.Version, params.Filter) {
+ filtered = append(filtered, info)
+ }
+ }
+ infos = filtered
+ }
+
resp, err := paginate(infos, params.ListParams, 100)
if err != nil {
return err
@@ -250,6 +260,9 @@ func ServeModulePackages(w http.ResponseWriter, r *http.Request, ds internal.Dat
// For now, we just use params.Limit to limit the number of packages returned.
var items []Package
for _, m := range metas {
+ if params.Filter != "" && !strings.Contains(m.Path, params.Filter) && !strings.Contains(m.Synopsis, params.Filter) {
+ continue
+ }
items = append(items, Package{
Path: m.Path,
ModulePath: modulePath,
@@ -339,6 +352,9 @@ func ServePackageSymbols(w http.ResponseWriter, r *http.Request, ds internal.Dat
var items []Symbol
for _, s := range syms {
+ if params.Filter != "" && !strings.Contains(s.Name, params.Filter) && !strings.Contains(s.Synopsis, params.Filter) {
+ continue
+ }
items = append(items, Symbol{
ModulePath: um.ModulePath,
Version: um.Version,
@@ -382,17 +398,27 @@ func ServePackageImportedBy(w http.ResponseWriter, r *http.Request, ds internal.
}
modulePath := um.ModulePath
- limit := params.Limit
- if limit <= 0 {
- limit = 100
+ importedBy, err := ds.GetImportedBy(r.Context(), pkgPath, modulePath, 1000)
+ if err != nil {
+ return err
}
- importedBy, err := ds.GetImportedBy(r.Context(), pkgPath, modulePath, limit)
+ count, err := ds.GetImportedByCount(r.Context(), pkgPath, modulePath)
if err != nil {
return err
}
- count, err := ds.GetImportedByCount(r.Context(), pkgPath, modulePath)
+ if params.Filter != "" {
+ var filtered []string
+ for _, p := range importedBy {
+ if strings.Contains(p, params.Filter) {
+ filtered = append(filtered, p)
+ }
+ }
+ importedBy = filtered
+ }
+
+ paged, err := paginate(importedBy, params.ListParams, 100)
if err != nil {
return err
}
@@ -401,8 +427,9 @@ func ServePackageImportedBy(w http.ResponseWriter, r *http.Request, ds internal.
ModulePath: modulePath,
Version: requestedVersion,
ImportedBy: PaginatedResponse[string]{
- Items: importedBy,
- Total: count,
+ Items: paged.Items,
+ Total: count,
+ NextPageToken: paged.NextPageToken,
},
}
@@ -439,6 +466,9 @@ func ServeVulnerabilities(vc *vuln.Client) func(w http.ResponseWriter, r *http.R
var items []Vulnerability
for _, v := range vulns {
+ if params.Filter != "" && !strings.Contains(v.ID, params.Filter) && !strings.Contains(v.Details, params.Filter) {
+ continue
+ }
items = append(items, Vulnerability{
ID: v.ID,
Details: v.Details,
diff --git a/internal/api/api_test.go b/internal/api/api_test.go
index daf49910..15659c37 100644
--- a/internal/api/api_test.go
+++ b/internal/api/api_test.go
@@ -387,8 +387,18 @@ func TestServeModulePackages(t *testing.T) {
ds.MustInsertModule(ctx, &internal.Module{
ModuleInfo: internal.ModuleInfo{ModulePath: modulePath, Version: version},
Units: []*internal.Unit{
- {UnitMeta: internal.UnitMeta{Path: modulePath, Name: "pkg1"}},
- {UnitMeta: internal.UnitMeta{Path: modulePath + "/sub", Name: "pkg2"}},
+ {
+ UnitMeta: internal.UnitMeta{Path: modulePath, Name: "pkg1"},
+ Documentation: []*internal.Documentation{
+ {Synopsis: "synopsis for pkg1"},
+ },
+ },
+ {
+ UnitMeta: internal.UnitMeta{Path: modulePath + "/sub", Name: "pkg2"},
+ Documentation: []*internal.Documentation{
+ {Synopsis: "synopsis for pkg2"},
+ },
+ },
},
})
for _, test := range []struct {
@@ -408,6 +418,20 @@ func TestServeModulePackages(t *testing.T) {
wantTotal: 2,
},
{
+ name: "filtering",
+ url: "/v1/packages/example.com?version=v1.0.0&filter=sub",
+ wantStatus: http.StatusOK,
+ wantCount: 1,
+ wantTotal: 1,
+ },
+ {
+ name: "filtering synopsis",
+ url: "/v1/packages/example.com?version=v1.0.0&filter=pkg2",
+ wantStatus: http.StatusOK,
+ wantCount: 1,
+ wantTotal: 1,
+ },
+ {
name: "limit and token",
url: "/v1/packages/example.com?version=v1.0.0&limit=1",
wantStatus: http.StatusOK,