content,internal/frontend: update imports, importedby and modules templates

The imports, importedby and module are updated to match the alpha designs.

Imports and ImportedBy now link to the latest version of each package and
display the package name.

A bug is fixed in HandleDetails, where an panic would occur if GetLatestPackage
returned an error and pkg.VersionInfo.Version was accessed immediately after.

Updates b/131593473

Change-Id: Icb42d1752f2d274cccc44cf5d47f994a3eaf2f62
Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/455286
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Julie Qiu 2019-04-27 10:51:34 -04:00
Родитель 2e1a49407d
Коммит 1445dcf18a
5 изменённых файлов: 44 добавлений и 39 удалений

Просмотреть файл

@ -7,8 +7,9 @@
{{ define "content" }}
<div>
<h3>Imported by</h3>
<p><b>Known importers: </b>{{ len .ImportedBy }} packages</p>
{{ range .ImportedBy }}
<p>{{ . }}</p>
<p><b>{{ .Name }}</b><span> - </span><a href="/{{.Path}}">{{ .Path }}</a></p>
{{ end }}
</div>
{{ end }}

Просмотреть файл

@ -6,16 +6,11 @@
{{ define "content" }}
<div>
{{ if .Imports }}
<h3>Imports</h3>
{{ range .Imports }}
<p>
<div><b>{{ .Name }}</b></div>
<div>{{ .Path }}</div>
</p>
{{ end }}
<h3>Imports</h3>
<p><b>Packages: </b>{{ len .Imports }}</p>
{{ range .Imports }}
<p><b>{{ .Name }}</b><span> - </span><a href="/{{.Path}}">{{ .Path }}</a></p>
{{ end }}
{{ if .StdLib }}
<h3>Standard Library</h3>
{{ range .StdLib }}

Просмотреть файл

@ -6,24 +6,17 @@
{{ define "content" }}
<div class="ModuleContent-list">
<h2>Module {{ .ModulePath }} @ {{ .Version }} contains packages:</h2>
<div>
<h3 class="ModuleContent-package">Package</h3>
<h3 class="ModuleContent-synopsis">Synopsis</h3>
</div>
<hr>
<h2>Packages in this module</h2>
{{ range .Packages }}
<div>
<div class="ModuleContent-package">
<a href="/{{ .Path }}@{{ .Version }}">
<b>{{ .Dir }}</b>
<p>
<div>
<b>{{ .Name }} - </b>
<a href="/{{ .Path }}?v={{ .Version }}">
<b>{{ .Suffix }}</b>
</a>
</div>
<div class="ModuleContent-synopsis">{{ .Synopsis }}</div>
<hr>
</div>
<div>{{ .Synopsis }}</div>
</p>
{{ end }}
<br>
{{ .ReadMe }}
</div>
{{ end }}

Просмотреть файл

@ -63,12 +63,12 @@ type ImportsDetails struct {
StdLib []*internal.Import
}
// ImportedByDetails contains information for all packages that import a given
// package.
// ImportedByDetails contains information for the collection of packages that
// import a given package.
type ImportedByDetails struct {
// ImportedBy is an array of the package paths that import a given
// ImportedBy is the collection of packages that import the given
// package.
ImportedBy []string
ImportedBy []*internal.Import
}
// License contains information used for a single license section.
@ -119,15 +119,11 @@ type Package struct {
Synopsis string
CommitTime string
Title string
Suffix string
Licenses []LicenseInfo
IsCommand bool
}
// Dir returns the directory of the package relative to the root of the module.
func (p *Package) Dir() string {
return strings.TrimPrefix(p.Path, fmt.Sprintf("%s/", p.ModulePath))
}
// transformLicenseInfos transforms an internal.LicenseInfo into a LicenseInfo,
// by adding an anchor field.
func transformLicenseInfos(dbLicenses []*internal.LicenseInfo) []LicenseInfo {
@ -160,6 +156,7 @@ func createPackageHeader(pkg *internal.VersionedPackage) (*Package, error) {
Version: pkg.VersionInfo.Version,
Path: pkg.Path,
Synopsis: pkg.Package.Synopsis,
Suffix: pkg.Package.Suffix,
Licenses: transformLicenseInfos(pkg.Licenses),
CommitTime: elapsedTime(pkg.VersionInfo.CommitTime),
}, nil
@ -249,6 +246,11 @@ func fetchModuleDetails(ctx context.Context, db *postgres.DB, pkg *internal.Vers
var packages []*Package
for _, p := range version.Packages {
if p.Suffix == "" {
// Display the package name if the package is at the
// root of the module.
p.Suffix = p.Name
}
packages = append(packages, &Package{
Name: p.Name,
Path: p.Path,
@ -256,6 +258,7 @@ func fetchModuleDetails(ctx context.Context, db *postgres.DB, pkg *internal.Vers
Licenses: transformLicenseInfos(p.Licenses),
Version: version.Version,
ModulePath: version.ModulePath,
Suffix: p.Suffix,
})
}
@ -392,12 +395,19 @@ func fetchImportsDetails(ctx context.Context, db *postgres.DB, pkg *internal.Ver
}
// fetchImportedByDetails fetches importers for the package version specified by
// path and version from the database and returns a ImportersDetails.
// path and version from the database and returns a ImportedByDetails.
func fetchImportedByDetails(ctx context.Context, db *postgres.DB, pkg *internal.Package) (*ImportedByDetails, error) {
importedBy, err := db.GetImportedBy(ctx, pkg.Path)
importedByPaths, err := db.GetImportedBy(ctx, pkg.Path)
if err != nil {
return nil, fmt.Errorf("db.GetImportedBy(ctx, %q): %v", pkg.Path, err)
}
var importedBy []*internal.Import
for _, p := range importedByPaths {
importedBy = append(importedBy, &internal.Import{
Name: packageName("main", p),
Path: p,
})
}
return &ImportedByDetails{
ImportedBy: importedBy,
}, nil
@ -450,7 +460,6 @@ func (c *Controller) HandleDetails(w http.ResponseWriter, r *http.Request) {
if version == "" {
pkg, err = c.db.GetLatestPackage(ctx, path)
version = pkg.VersionInfo.Version
} else {
pkg, err = c.db.GetPackage(ctx, path, version)
}
@ -464,6 +473,7 @@ func (c *Controller) HandleDetails(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
version = pkg.VersionInfo.Version
pkgHeader, err := createPackageHeader(pkg)
if err != nil {
log.Printf("error creating package header for %s@%s: %v", path, version, err)

Просмотреть файл

@ -54,6 +54,7 @@ var (
ModulePath: "test.com/module",
Version: "v1.0.0",
Path: "test.com/module/pkg_name",
Suffix: "pkg_name",
Synopsis: "Test package synopsis",
Licenses: transformLicenseInfos(sampleLicenseInfos),
}
@ -660,13 +661,18 @@ func TestFetchImportedByDetails(t *testing.T) {
{
pkg: pkg2,
wantDetails: &ImportedByDetails{
ImportedBy: []string{pkg3.Path},
ImportedBy: []*internal.Import{
{Path: pkg3.Path, Name: pkg3.Name},
},
},
},
{
pkg: pkg1,
wantDetails: &ImportedByDetails{
ImportedBy: []string{pkg2.Path, pkg3.Path},
ImportedBy: []*internal.Import{
{Name: pkg2.Name, Path: pkg2.Path},
{Name: pkg3.Name, Path: pkg3.Path},
},
},
},
} {