From 67ad226e3fd9a449cd7b0f7c8a7dab4134676fec Mon Sep 17 00:00:00 2001 From: Julie Qiu Date: Tue, 30 Apr 2019 17:42:06 -0400 Subject: [PATCH] internal/frontend,internal/postgres: fetch documentation The following queries have been updated to fetch documentation from the packages table: * GetPackage * GetLatestPackage * GetVersionForPackage The documentation tab now displays that data. Fixes b/131706915 Change-Id: I591f374a355c55b8b937dbf2cfb2de6f105591cd Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/456568 Reviewed-by: Dmitri Shuralyov --- content/static/html/pages/doc.tmpl | 2 +- internal/fetch/fetch_test.go | 27 ++++++++------- internal/frontend/details.go | 10 ++++-- internal/postgres/postgres.go | 54 +++++++++++++++++------------- internal/postgres/postgres_test.go | 16 ++++----- 5 files changed, 61 insertions(+), 48 deletions(-) diff --git a/content/static/html/pages/doc.tmpl b/content/static/html/pages/doc.tmpl index 45d22048..0d7a7378 100644 --- a/content/static/html/pages/doc.tmpl +++ b/content/static/html/pages/doc.tmpl @@ -6,6 +6,6 @@ {{ define "content" }}
- <godoc of package {{ .ModulePath }} will go here> + {{ .Documentation }}
{{ end }} diff --git a/internal/fetch/fetch_test.go b/internal/fetch/fetch_test.go index 4a66147e..c7654555 100644 --- a/internal/fetch/fetch_test.go +++ b/internal/fetch/fetch_test.go @@ -55,10 +55,11 @@ func TestFetchAndInsertVersion(t *testing.T) { VersionType: "release", }, Package: internal.Package{ - Path: "my.mod/module/bar", - Name: "bar", - Synopsis: "package bar", - Suffix: "bar", + Path: "my.mod/module/bar", + Name: "bar", + Synopsis: "package bar", + Suffix: "bar", + DocumentationHTML: tmpDocumentationHTML, Licenses: []*internal.LicenseInfo{ {Type: "BSD-3-Clause", FilePath: "LICENSE"}, {Type: "MIT", FilePath: "bar/LICENSE"}, @@ -80,10 +81,11 @@ func TestFetchAndInsertVersion(t *testing.T) { VersionType: "release", }, Package: internal.Package{ - Path: "nonredistributable.mod/module/bar/baz", - Name: "baz", - Synopsis: "package baz", - Suffix: "bar/baz", + Path: "nonredistributable.mod/module/bar/baz", + Name: "baz", + Synopsis: "package baz", + Suffix: "bar/baz", + DocumentationHTML: tmpDocumentationHTML, Licenses: []*internal.LicenseInfo{ {Type: "BSD-3-Clause", FilePath: "LICENSE"}, {Type: "BSD-0-Clause", FilePath: "LICENSE.txt"}, @@ -108,10 +110,11 @@ func TestFetchAndInsertVersion(t *testing.T) { VersionType: "release", }, Package: internal.Package{ - Path: "nonredistributable.mod/module/foo", - Name: "foo", - Synopsis: "", - Suffix: "foo", + Path: "nonredistributable.mod/module/foo", + Name: "foo", + Synopsis: "", + Suffix: "foo", + DocumentationHTML: tmpDocumentationHTML, Licenses: []*internal.LicenseInfo{ {Type: "BSD-3-Clause", FilePath: "LICENSE"}, {Type: "BSD-0-Clause", FilePath: "LICENSE.txt"}, diff --git a/internal/frontend/details.go b/internal/frontend/details.go index c606dd2e..653e6dba 100644 --- a/internal/frontend/details.go +++ b/internal/frontend/details.go @@ -41,7 +41,8 @@ type OverviewDetails struct { // DocumentationDetails contains data for the doc template. type DocumentationDetails struct { - ModulePath string + ModulePath string + Documentation template.HTML } // ModuleDetails contains all of the data that the module template @@ -236,7 +237,10 @@ func fetchOverviewDetails(ctx context.Context, db *postgres.DB, pkg *internal.Ve // fetchDocumentationDetails fetches data for the package specified by path and version // from the database and returns a DocumentationDetails. func fetchDocumentationDetails(ctx context.Context, db *postgres.DB, pkg *internal.VersionedPackage) (*DocumentationDetails, error) { - return &DocumentationDetails{pkg.VersionInfo.ModulePath}, nil + return &DocumentationDetails{ + ModulePath: pkg.VersionInfo.ModulePath, + Documentation: template.HTML(pkg.DocumentationHTML), + }, nil } // fetchModuleDetails fetches data for the module version specified by pkgPath and pkgversion @@ -429,7 +433,7 @@ func readmeHTML(readmeFilePath string, readmeContents []byte) template.HTML { // not whitelist iframes, object, embed, styles, script, etc. p := bluemonday.UGCPolicy() unsafe := blackfriday.Run(readmeContents) - return template.HTML(string(p.SanitizeBytes(unsafe))) + return template.HTML(p.SanitizeBytes(unsafe)) } // tabSettings defines rendering options associated to each tab. Any tab value diff --git a/internal/postgres/postgres.go b/internal/postgres/postgres.go index dec0d354..c0cb6c74 100644 --- a/internal/postgres/postgres.go +++ b/internal/postgres/postgres.go @@ -350,7 +350,7 @@ func (db *DB) GetPackage(ctx context.Context, path string, version string) (*int var ( commitTime time.Time name, synopsis, seriesPath, modulePath, suffix, readmeFilePath, versionType string - readmeContents []byte + readmeContents, documentation []byte licenseTypes, licensePaths []string ) query := ` @@ -365,7 +365,8 @@ func (db *DB) GetPackage(ctx context.Context, path string, version string) (*int p.name, p.synopsis, p.suffix, - v.version_type + v.version_type, + p.documentation FROM versions v INNER JOIN @@ -385,7 +386,7 @@ func (db *DB) GetPackage(ctx context.Context, path string, version string) (*int row := db.QueryRowContext(ctx, query, path, version) if err := row.Scan(&commitTime, pq.Array(&licenseTypes), pq.Array(&licensePaths), &readmeFilePath, &readmeContents, &seriesPath, &modulePath, - &name, &synopsis, &suffix, &versionType); err != nil { + &name, &synopsis, &suffix, &versionType, &documentation); err != nil { if err == sql.ErrNoRows { return nil, derrors.NotFound(fmt.Sprintf("package %s@%s not found", path, version)) } @@ -399,11 +400,12 @@ func (db *DB) GetPackage(ctx context.Context, path string, version string) (*int return &internal.VersionedPackage{ Package: internal.Package{ - Name: name, - Path: path, - Synopsis: synopsis, - Licenses: lics, - Suffix: suffix, + Name: name, + Path: path, + Synopsis: synopsis, + Licenses: lics, + Suffix: suffix, + DocumentationHTML: documentation, }, VersionInfo: internal.VersionInfo{ SeriesPath: seriesPath, @@ -557,7 +559,7 @@ func (db *DB) GetLatestPackage(ctx context.Context, path string) (*internal.Vers commitTime time.Time seriesPath, modulePath, name, synopsis, version, suffix, readmeFilePath string licenseTypes, licensePaths []string - readmeContents []byte + readmeContents, documentation []byte ) query := ` SELECT @@ -571,7 +573,8 @@ func (db *DB) GetLatestPackage(ctx context.Context, path string) (*internal.Vers p.synopsis, p.suffix, v.readme_file_path, - v.readme_contents + v.readme_contents, + p.documentation FROM versions v INNER JOIN @@ -594,7 +597,7 @@ func (db *DB) GetLatestPackage(ctx context.Context, path string) (*internal.Vers LIMIT 1;` row := db.QueryRowContext(ctx, query, path) - if err := row.Scan(&seriesPath, &modulePath, pq.Array(&licenseTypes), pq.Array(&licensePaths), &version, &commitTime, &name, &synopsis, &suffix, &readmeFilePath, &readmeContents); err != nil { + if err := row.Scan(&seriesPath, &modulePath, pq.Array(&licenseTypes), pq.Array(&licensePaths), &version, &commitTime, &name, &synopsis, &suffix, &readmeFilePath, &readmeContents, &documentation); err != nil { if err == sql.ErrNoRows { return nil, derrors.NotFound(fmt.Sprintf("package %s@%s not found", path, version)) } @@ -608,11 +611,12 @@ func (db *DB) GetLatestPackage(ctx context.Context, path string) (*internal.Vers return &internal.VersionedPackage{ Package: internal.Package{ - Name: name, - Path: path, - Synopsis: synopsis, - Licenses: lics, - Suffix: suffix, + Name: name, + Path: path, + Synopsis: synopsis, + Licenses: lics, + Suffix: suffix, + DocumentationHTML: documentation, }, VersionInfo: internal.VersionInfo{ SeriesPath: seriesPath, @@ -640,7 +644,8 @@ func (db *DB) GetVersionForPackage(ctx context.Context, path, version string) (* v.readme_file_path, v.readme_contents, v.commit_time, - v.version_type + v.version_type, + p.documentation FROM vw_licensed_packages p INNER JOIN @@ -664,7 +669,7 @@ func (db *DB) GetVersionForPackage(ctx context.Context, path, version string) (* var ( pkgPath, seriesPath, modulePath, pkgName string synopsis, suffix, readmeFilePath, versionType string - readmeContents []byte + readmeContents, documentation []byte commitTime time.Time licenseTypes, licensePaths []string ) @@ -680,7 +685,7 @@ func (db *DB) GetVersionForPackage(ctx context.Context, path, version string) (* for rows.Next() { if err := rows.Scan(&pkgPath, &seriesPath, &modulePath, &pkgName, &synopsis, &suffix, pq.Array(&licenseTypes), pq.Array(&licensePaths), &readmeFilePath, - &readmeContents, &commitTime, &versionType); err != nil { + &readmeContents, &commitTime, &versionType, &documentation); err != nil { return nil, fmt.Errorf("row.Scan(): %v", err) } lics, err := zipLicenseInfo(licenseTypes, licensePaths) @@ -694,11 +699,12 @@ func (db *DB) GetVersionForPackage(ctx context.Context, path, version string) (* v.CommitTime = commitTime v.VersionType = internal.VersionType(versionType) v.Packages = append(v.Packages, &internal.Package{ - Path: pkgPath, - Name: pkgName, - Synopsis: synopsis, - Licenses: lics, - Suffix: suffix, + Path: pkgPath, + Name: pkgName, + Synopsis: synopsis, + Licenses: lics, + Suffix: suffix, + DocumentationHTML: documentation, }) } diff --git a/internal/postgres/postgres_test.go b/internal/postgres/postgres_test.go index 830ec24a..d029e1c4 100644 --- a/internal/postgres/postgres_test.go +++ b/internal/postgres/postgres_test.go @@ -49,10 +49,11 @@ func sampleVersion(mutators ...func(*internal.Version)) *internal.Version { }, Packages: []*internal.Package{ &internal.Package{ - Name: "foo", - Synopsis: "This is a package synopsis", - Path: "path.to/foo", - Licenses: sampleLicenseInfos, + Name: "foo", + Synopsis: "This is a package synopsis", + Path: "path.to/foo", + Licenses: sampleLicenseInfos, + DocumentationHTML: []byte("This is the documentation HTML"), Imports: []*internal.Import{ &internal.Import{ Name: "bar", @@ -463,9 +464,8 @@ func TestPostgres_GetLatestPackage(t *testing.T) { t.Errorf("testDB.GetLatestPackage(ctx, %q): %v", tc.path, err) } - if diff := cmp.Diff(gotPkg, tc.wantPkg); diff != "" { - t.Errorf("testDB.GetLatestPackage(ctx, %q) mismatch (-got +want):\n%s", - tc.path, diff) + if diff := cmp.Diff(tc.wantPkg, gotPkg, cmpopts.EquateEmpty()); diff != "" { + t.Errorf("testDB.GetLatestPackage(ctx, %q) mismatch (-want +got):\n%s", tc.path, diff) } }) } @@ -1093,7 +1093,7 @@ func TestGetVersionForPackage(t *testing.T) { if err != nil { t.Errorf("testDB.GetVersionForPackage(ctx, %q, %q): %v", tc.path, tc.version, err) } - if diff := cmp.Diff(tc.wantVersion, got); diff != "" { + if diff := cmp.Diff(tc.wantVersion, got, cmpopts.EquateEmpty()); diff != "" { t.Errorf("testDB.GetVersionForPackage(ctx, %q, %q) mismatch (-want +got):\n%s", tc.path, tc.version, diff) } })