aboutsummaryrefslogtreecommitdiff
path: root/internal/api
diff options
context:
space:
mode:
authorEthan Lee <ethanalee@google.com>2026-04-07 14:44:16 +0000
committerEthan Lee <ethanalee@google.com>2026-04-07 09:23:15 -0700
commit3c8abd6a69a5031ca55e55d16e725476f1d69cba (patch)
treec089fea9b2c6168836cadb7ff46d95e82c060760 /internal/api
parentd1eedfe77fdd85e4ec9dcef7e4a4411116eddbb2 (diff)
downloadgo-x-pkgsite-3c8abd6a69a5031ca55e55d16e725476f1d69cba.tar.xz
internal/api: add support for readme and licenses in ServeModule
- Utilize conditional fieldset in GetUnit to efficiently retrieve readme and license fields. - Modify Module type to include HasGoMod. Change-Id: Id22ac3f2485392749742332701d2e913f047b3da Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/763401 kokoro-CI: kokoro <noreply+kokoro@google.com> Auto-Submit: Ethan Lee <ethanalee@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jonathan Amsterdam <jba@google.com>
Diffstat (limited to 'internal/api')
-rw-r--r--internal/api/api.go36
-rw-r--r--internal/api/api_test.go80
-rw-r--r--internal/api/types.go1
3 files changed, 97 insertions, 20 deletions
diff --git a/internal/api/api.go b/internal/api/api.go
index 49873c9b..5bf14eec 100644
--- a/internal/api/api.go
+++ b/internal/api/api.go
@@ -100,21 +100,45 @@ func ServeModule(w http.ResponseWriter, r *http.Request, ds internal.DataSource)
resp := Module{
Path: um.ModulePath,
Version: um.Version,
+ IsLatest: um.Version == um.LatestVersion,
IsStandardLibrary: stdlib.Contains(um.ModulePath),
IsRedistributable: um.IsRedistributable,
+ HasGoMod: um.HasGoMod,
}
// RepoURL needs to be extracted from source info if available
if um.SourceInfo != nil {
resp.RepoURL = um.SourceInfo.RepoURL()
}
+ if !params.Readme && !params.Licenses {
+ return serveJSON(w, http.StatusOK, resp)
+ }
+
+ fs := internal.MinimalFields
if params.Readme {
- readme, err := ds.GetModuleReadme(r.Context(), um.ModulePath, um.Version)
- if err == nil && readme != nil {
- resp.Readme = &Readme{
- Filepath: readme.Filepath,
- Contents: readme.Contents,
- }
+ fs |= internal.WithMain // WithMain includes Readme in GetUnit
+ }
+ if params.Licenses {
+ fs |= internal.WithLicenses
+ }
+ unit, err := ds.GetUnit(r.Context(), um, fs, internal.BuildContext{})
+ if err != nil {
+ return serveJSON(w, http.StatusOK, resp)
+ }
+
+ if params.Readme && unit.Readme != nil {
+ resp.Readme = &Readme{
+ Filepath: unit.Readme.Filepath,
+ Contents: unit.Readme.Contents,
+ }
+ }
+ if params.Licenses {
+ for _, l := range unit.LicenseContents {
+ resp.Licenses = append(resp.Licenses, License{
+ Types: l.Metadata.Types,
+ FilePath: l.Metadata.FilePath,
+ Contents: string(l.Contents),
+ })
}
}
diff --git a/internal/api/api_test.go b/internal/api/api_test.go
index 15659c37..36066f46 100644
--- a/internal/api/api_test.go
+++ b/internal/api/api_test.go
@@ -230,20 +230,34 @@ func TestServeModule(t *testing.T) {
version = "v1.2.3"
)
+ mi1 := sample.ModuleInfo(modulePath, version)
+ mi1.LatestVersion = "v1.2.4"
+ mi1.HasGoMod = true
+
ds.MustInsertModule(ctx, &internal.Module{
- ModuleInfo: internal.ModuleInfo{
- ModulePath: modulePath,
- Version: version,
- },
+ ModuleInfo: *mi1,
+ Licenses: sample.Licenses(),
Units: []*internal.Unit{{
UnitMeta: internal.UnitMeta{
- Path: modulePath,
- ModuleInfo: internal.ModuleInfo{
- ModulePath: modulePath,
- Version: version,
- },
+ Path: modulePath,
+ ModuleInfo: *mi1,
+ },
+ Readme: &internal.Readme{Filepath: "README.md", Contents: "Hello world"},
+ Licenses: sample.LicenseMetadata(),
+ }},
+ })
+
+ mi2 := sample.ModuleInfo(modulePath, "v1.2.4")
+ mi2.LatestVersion = "v1.2.4"
+ mi2.HasGoMod = true
+
+ ds.MustInsertModule(ctx, &internal.Module{
+ ModuleInfo: *mi2,
+ Units: []*internal.Unit{{
+ UnitMeta: internal.UnitMeta{
+ Path: modulePath,
+ ModuleInfo: *mi2,
},
- Readme: &internal.Readme{Filepath: "README.md", Contents: "Hello world"},
}},
})
@@ -258,8 +272,24 @@ func TestServeModule(t *testing.T) {
url: "/v1/module/example.com?version=v1.2.3",
wantStatus: http.StatusOK,
want: &Module{
- Path: modulePath,
- Version: version,
+ Path: modulePath,
+ Version: version,
+ IsRedistributable: true,
+ HasGoMod: true,
+ RepoURL: "https://example.com",
+ },
+ },
+ {
+ name: "latest module metadata",
+ url: "/v1/module/example.com?version=v1.2.4",
+ wantStatus: http.StatusOK,
+ want: &Module{
+ Path: modulePath,
+ Version: "v1.2.4",
+ IsLatest: true,
+ IsRedistributable: true,
+ HasGoMod: true,
+ RepoURL: "https://example.com",
},
},
{
@@ -273,14 +303,36 @@ func TestServeModule(t *testing.T) {
url: "/v1/module/example.com?version=v1.2.3&readme=true",
wantStatus: http.StatusOK,
want: &Module{
- Path: modulePath,
- Version: version,
+ Path: modulePath,
+ Version: version,
+ IsRedistributable: true,
+ HasGoMod: true,
+ RepoURL: "https://example.com",
Readme: &Readme{
Filepath: "README.md",
Contents: "Hello world",
},
},
},
+ {
+ name: "module with licenses",
+ url: "/v1/module/example.com?version=v1.2.3&licenses=true",
+ wantStatus: http.StatusOK,
+ want: &Module{
+ Path: modulePath,
+ Version: version,
+ IsRedistributable: true,
+ HasGoMod: true,
+ RepoURL: "https://example.com",
+ Licenses: []License{
+ {
+ Types: []string{"MIT"},
+ FilePath: "LICENSE",
+ Contents: "Lorem Ipsum",
+ },
+ },
+ },
+ },
} {
t.Run(test.name, func(t *testing.T) {
r := httptest.NewRequest("GET", test.url, nil)
diff --git a/internal/api/types.go b/internal/api/types.go
index 6af65e58..be923e4c 100644
--- a/internal/api/types.go
+++ b/internal/api/types.go
@@ -47,6 +47,7 @@ type Module struct {
IsLatest bool `json:"isLatest"`
IsRedistributable bool `json:"isRedistributable"`
IsStandardLibrary bool `json:"isStandardLibrary"`
+ HasGoMod bool `json:"hasGoMod"`
RepoURL string `json:"repoUrl"`
GoModContents string `json:"goModContents,omitempty"`
Readme *Readme `json:"readme,omitempty"`