From 8514eebca6ca7b3213e879faa2a83c7e9ea6e181 Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Wed, 11 Mar 2026 21:20:21 +0000 Subject: internal/api: implement package imported-by endpoint - Implement ServePackageImportedBy handler and GetImportedBy and GetImportedByCount datasource methods. Change-Id: I8c4cc65fbff7172eaf48e5426e4f3f41c82bd38e Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/754865 Reviewed-by: Jonathan Amsterdam kokoro-CI: kokoro LUCI-TryBot-Result: Go LUCI Auto-Submit: Ethan Lee --- internal/api/api.go | 61 +++++++++++++++++++++++ internal/api/api_test.go | 60 ++++++++++++++++++++++ internal/datasource.go | 4 ++ internal/fetchdatasource/fetchdatasource.go | 10 ++++ internal/frontend/imports_test.go | 6 +-- internal/frontend/server.go | 1 + internal/testing/fakedatasource/fakedatasource.go | 18 +++---- 7 files changed, 148 insertions(+), 12 deletions(-) diff --git a/internal/api/api.go b/internal/api/api.go index b2be2327..8cfe2151 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -399,6 +399,67 @@ func ServePackageSymbols(w http.ResponseWriter, r *http.Request, ds internal.Dat return serveJSON(w, http.StatusOK, resp) } +// ServePackageImportedBy handles requests for the v1 package imported-by endpoint. +func ServePackageImportedBy(w http.ResponseWriter, r *http.Request, ds internal.DataSource) (err error) { + defer derrors.Wrap(&err, "ServePackageImportedBy") + + pkgPath := strings.TrimPrefix(r.URL.Path, "/v1/imported-by/") + if pkgPath == "" { + return serveErrorJSON(w, http.StatusBadRequest, "missing package path", nil) + } + + var params ImportedByParams + if err := ParseParams(r.URL.Query(), ¶ms); err != nil { + return serveErrorJSON(w, http.StatusBadRequest, err.Error(), nil) + } + + requestedVersion := params.Version + if requestedVersion == "" { + requestedVersion = version.Latest + } + + um, candidates, err := resolveModulePath(r, ds, pkgPath, params.Module, requestedVersion) + if err != nil { + if errors.Is(err, derrors.NotFound) { + return serveErrorJSON(w, http.StatusNotFound, err.Error(), nil) + } + return err + } + if len(candidates) > 0 { + return serveErrorJSON(w, http.StatusBadRequest, "ambiguous package path", candidates) + } + modulePath := um.ModulePath + + limit := params.Limit + if limit <= 0 { + limit = 100 + } + + importedBy, err := ds.GetImportedBy(r.Context(), pkgPath, modulePath, limit) + if err != nil { + if errors.Is(err, derrors.NotFound) { + return serveErrorJSON(w, http.StatusNotFound, err.Error(), nil) + } + return err + } + + count, err := ds.GetImportedByCount(r.Context(), pkgPath, modulePath) + if err != nil { + return err + } + + resp := PackageImportedBy{ + ModulePath: modulePath, + Version: requestedVersion, + ImportedBy: PaginatedResponse[string]{ + Items: importedBy, + Total: count, + }, + } + + return serveJSON(w, http.StatusOK, resp) +} + // resolveModulePath determines the correct module path for a given package path and version. // If the module path is not provided, it searches through potential candidate module paths // derived from the package path. If multiple valid modules contain the package, it returns diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 90373b66..a0ded1d2 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -708,6 +708,66 @@ func TestServePackageSymbols(t *testing.T) { } } +func TestServePackageImportedBy(t *testing.T) { + ctx := context.Background() + ds := fakedatasource.New() + + const ( + pkgPath = "example.com/pkg" + modulePath = "example.com" + version = "v1.0.0" + ) + + ds.MustInsertModule(ctx, &internal.Module{ + ModuleInfo: internal.ModuleInfo{ModulePath: modulePath, Version: version}, + Units: []*internal.Unit{ + {UnitMeta: internal.UnitMeta{Path: pkgPath, ModuleInfo: internal.ModuleInfo{ModulePath: modulePath, Version: version}}}, + { + UnitMeta: internal.UnitMeta{Path: "example.com/other", ModuleInfo: internal.ModuleInfo{ModulePath: modulePath, Version: version}}, + Imports: []string{pkgPath}, + }, + }, + }) + + for _, test := range []struct { + name string + url string + wantStatus int + wantCount int + }{ + { + name: "all imported by", + url: "/v1/imported-by/example.com/pkg?version=v1.0.0", + wantStatus: http.StatusOK, + wantCount: 1, + }, + } { + t.Run(test.name, func(t *testing.T) { + r := httptest.NewRequest("GET", test.url, nil) + w := httptest.NewRecorder() + + err := ServePackageImportedBy(w, r, ds) + if err != nil && w.Code != test.wantStatus { + t.Fatalf("ServePackageImportedBy 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 PackageImportedBy + if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { + t.Fatalf("json.Unmarshal: %v", err) + } + if len(got.ImportedBy.Items) != test.wantCount { + t.Errorf("count = %d, want %d", len(got.ImportedBy.Items), test.wantCount) + } + } + }) + } +} + // unmarshalResponse unmarshals an API response into either // a *T or an *Error. func unmarshalResponse[T any](data []byte) (any, error) { diff --git a/internal/datasource.go b/internal/datasource.go index 59f0afa1..6cfc2110 100644 --- a/internal/datasource.go +++ b/internal/datasource.go @@ -97,6 +97,10 @@ type DataSource interface { GetModulePackages(ctx context.Context, modulePath, version string) ([]*PackageMeta, error) // GetSymbols returns symbols for the given unit and build context. GetSymbols(ctx context.Context, pkgPath, modulePath, version string, bc BuildContext) ([]*Symbol, error) + // GetImportedBy returns the paths of packages that import the given package. + GetImportedBy(ctx context.Context, pkgPath, modulePath string, limit int) ([]string, error) + // GetImportedByCount returns the number of packages that import the given package. + GetImportedByCount(ctx context.Context, pkgPath, modulePath string) (int, 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 dfe30fc9..af03c733 100644 --- a/internal/fetchdatasource/fetchdatasource.go +++ b/internal/fetchdatasource/fetchdatasource.go @@ -436,3 +436,13 @@ func (ds *FetchDataSource) GetSymbols(ctx context.Context, pkgPath, modulePath, } return doc.API, nil } + +// GetImportedBy is not implemented. +func (ds *FetchDataSource) GetImportedBy(ctx context.Context, pkgPath, modulePath string, limit int) ([]string, error) { + return nil, nil +} + +// GetImportedByCount is not implemented. +func (ds *FetchDataSource) GetImportedByCount(ctx context.Context, pkgPath, modulePath string) (int, error) { + return 0, nil +} diff --git a/internal/frontend/imports_test.go b/internal/frontend/imports_test.go index bef1934c..c130a028 100644 --- a/internal/frontend/imports_test.go +++ b/internal/frontend/imports_test.go @@ -112,7 +112,7 @@ func TestFetchImportedByDetails(t *testing.T) { pkg: pkg2, wantDetails: &ImportedByDetails{ ImportedBy: []*Section{{Prefix: pkg3.Path, NumLines: 0}}, - NumImportedByDisplay: "0 (displaying 1 package, including internal and invalid packages)", + NumImportedByDisplay: "1", Total: 1, }, }, @@ -123,7 +123,7 @@ func TestFetchImportedByDetails(t *testing.T) { {Prefix: pkg2.Path, NumLines: 0}, {Prefix: pkg3.Path, NumLines: 0}, }, - NumImportedByDisplay: "0 (displaying 2 packages, including internal and invalid packages)", + NumImportedByDisplay: "2", Total: 2, }, }, @@ -161,7 +161,7 @@ func TestFetchImportedByDetails_ExceedsLimit(t *testing.T) { {Prefix: "m2.com/a/p"}, }, - NumImportedByDisplay: "0 (displaying more than 2 packages, including internal and invalid packages)", + NumImportedByDisplay: "3 (displaying more than 2 packages, including internal and invalid packages)", Total: 3, } checkFetchImportedByDetails(ctx, fds, t, m.Packages()[0], wantDetails) diff --git a/internal/frontend/server.go b/internal/frontend/server.go index 6f4e5a9d..d6a7fa01 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/symbols/", s.errorHandler(api.ServePackageSymbols)) + handle("GET /v1/imported-by/", s.errorHandler(api.ServePackageImportedBy)) handle("GET /v1/module/", s.errorHandler(api.ServeModule)) handle("GET /v1/versions/", s.errorHandler(api.ServeModuleVersions)) handle("GET /v1/packages/", s.errorHandler(api.ServeModulePackages)) diff --git a/internal/testing/fakedatasource/fakedatasource.go b/internal/testing/fakedatasource/fakedatasource.go index 1075d147..3da3b3bf 100644 --- a/internal/testing/fakedatasource/fakedatasource.go +++ b/internal/testing/fakedatasource/fakedatasource.go @@ -393,18 +393,18 @@ func (ds *FakeDataSource) IsExcluded(ctx context.Context, path, version string) 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 +// GetImportedBy returns the paths of packages that import the given package. +func (ds *FakeDataSource) GetImportedBy(ctx context.Context, pkgPath, modulePath string, limit int) ([]string, error) { + paths := ds.importedBy[pkgPath] + if len(paths) > limit { + return paths[:limit], nil + } + return paths, nil } +// GetImportedByCount returns the number of packages that import the given package. func (ds *FakeDataSource) GetImportedByCount(ctx context.Context, pkgPath, modulePath string) (int, error) { - return 0, nil + return len(ds.importedBy[pkgPath]), nil } func (ds *FakeDataSource) GetLatestMajorPathForV1Path(ctx context.Context, v1path string) (string, int, error) { -- cgit v1.3