internal/frontend, content: add licenses page

Add content to the licenses page, along with anchored links to specific
licenses that apply to the package, as well as a disclaimer.

Updates b/124309095

Change-Id: Id394c8acd2ca60cf711f64bc15541429e77715d1
Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/450766
Reviewed-by: Julie Qiu <julieqiu@google.com>
This commit is contained in:
Rob Findley 2019-04-17 14:12:10 -04:00 коммит произвёл Julie Qiu
Родитель c5fba95a79
Коммит 2fc2cd3b6f
6 изменённых файлов: 92 добавлений и 12 удалений

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

@ -135,3 +135,15 @@
display: inline-flex;
width: 50%;
}
.License-contents {
background-color: var(--grey-10);
padding: 10px;
border: 1px solid var(--grey-8);
border-radius: 3px;
}
.License-disclaimer {
font-style: italic;
}
.Disclaimer-link {
font-style: italic;
}

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

@ -24,7 +24,10 @@
<p>import "{{ $header.Path }}"</p>
{{ end }}
<p>{{ $header.Synopsis }}</p>
<p>{{range $i, $e := $header.Licenses}}{{if $i}}, {{end}}{{ $e.Type }}{{end}} - {{ $header.CommitTime }}</p>
<p>{{ range $i, $e := $header.Licenses -}}{{ if $i }}, {{ end }}
<a href="/{{$header.Path}}?v={{$header.Version}}&tab=licenses#{{.Anchor}}">{{ $e.Type }}</a>
{{- end }}
<a href="/{{$header.Path}}?v={{$header.Version}}&tab=licenses#Disclaimer" class="Disclaimer-link">(This is not legal advice)</a> {{ $header.CommitTime }}</p>
</header>
<ul class="ModulesNav">

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

@ -5,7 +5,21 @@
-->
{{ define "content" }}
<div>
&lt;licenses here&gt;
</div>
<section id="Disclaimer">
<h3><a href="#Disclaimer">Disclaimer</a></h3>
<p class="License-disclaimer">
The go.dev website displays license information in order to help users
evaluate packages for their intended use. Licenses are detected based on
the name and contents of the linked license files to the best of our
ability. We hope this information is helpful, but this is not legal advice
and we do not make any guarantees regarding the accuracy of our license
detection.
</p>
</section>
{{ range .Licenses }}
<section class="License" id="{{ .Anchor }}">
<h3><a href="#{{ .Anchor }}">{{ .Type }} <small>({{ .FilePath }})</small></a></h3>
<pre class="License-contents">{{ printf "%s" .Contents }}</pre>
</section>
{{ end }}
{{ end }}

4
go.sum
Просмотреть файл

@ -166,6 +166,7 @@ github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Z
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.2.1-0.20190217060313-77b690bf6c10 h1:FU2U2BnzkC2uXC3CRksDPVW/935z95T/cZqCW1mL7z0=
github.com/google/go-cmp v0.2.1-0.20190217060313-77b690bf6c10/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-containerregistry v0.0.0-20190327192230-5296537b6d5d/go.mod h1:yZAFP63pRshzrEYLXLGPmUt0Ay+2zdjmMN1loCnRLUk=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
@ -566,9 +567,10 @@ google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmE
google.golang.org/grpc v1.15.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9MZMio=
google.golang.org/grpc v1.16.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9MZMio=
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
google.golang.org/grpc v1.18.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
google.golang.org/grpc v1.19.0 h1:cfg4PD8YEdSFnm7qLV4++93WcmhH2nIUhMjhdCvl3j8=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.19.1 h1:TrBcJ1yqAl1G++wO39nD/qtgpsW9/1+QGrluyMGEYgM=
google.golang.org/grpc v1.19.1/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
gopkg.in/asn1-ber.v1 v1.0.0-20170511165959-379148ca0225/go.mod h1:cuepJuh7vyXfUyUwEgHQXw849cJrilpS5NeIjOWESAw=
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d/go.mod h1:cuepJuh7vyXfUyUwEgHQXw849cJrilpS5NeIjOWESAw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

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

@ -10,6 +10,7 @@ import (
"html/template"
"log"
"net/http"
"net/url"
"path/filepath"
"strings"
"time"
@ -57,8 +58,21 @@ type ImportsDetails struct {
type ImportersDetails struct {
}
// License contains information used for a single license section.
type License struct {
*internal.License
Anchor string
}
// LicensesDetails contains license information for a package.
type LicensesDetails struct {
Licenses []License
}
// LicenseInfo contains license metadata that is used in the package header.
type LicenseInfo struct {
*internal.LicenseInfo
Anchor string
}
// VersionsDetails contains all the data that the versions tab
@ -92,7 +106,7 @@ type Package struct {
Synopsis string
CommitTime string
Title string
Licenses []*internal.LicenseInfo
Licenses []LicenseInfo
IsCommand bool
}
@ -101,6 +115,19 @@ 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 {
var licenseInfos []LicenseInfo
for _, l := range dbLicenses {
licenseInfos = append(licenseInfos, LicenseInfo{
LicenseInfo: l,
Anchor: licenseAnchor(l.FilePath),
})
}
return licenseInfos
}
// createPackageHeader returns a *Package based on the fields of the specified
// package. It assumes that pkg and pkg.Version are not nil.
func createPackageHeader(pkg *internal.Package) (*Package, error) {
@ -123,7 +150,7 @@ func createPackageHeader(pkg *internal.Package) (*Package, error) {
Version: pkg.Version.Version,
Path: pkg.Path,
Synopsis: pkg.Synopsis,
Licenses: pkg.Licenses,
Licenses: transformLicenseInfos(pkg.Licenses),
CommitTime: elapsedTime(pkg.Version.CommitTime),
}, nil
}
@ -245,7 +272,7 @@ func fetchModuleDetails(ctx context.Context, db *postgres.DB, pkgPath, pkgversio
Name: p.Name,
Path: p.Path,
Synopsis: p.Synopsis,
Licenses: p.Licenses,
Licenses: transformLicenseInfos(p.Licenses),
Version: version.Version,
ModulePath: version.Module.Path,
})
@ -364,6 +391,12 @@ func fetchVersionsDetails(ctx context.Context, db *postgres.DB, path, version st
}, nil
}
// licenseAnchor returns the anchor that should be used to jump to the specific
// license on the licenses page.
func licenseAnchor(filePath string) string {
return url.QueryEscape(filePath)
}
// fetchLicensesDetails fetches license data for the package version specified by
// path and version from the database and returns a LicensesDetails.
func fetchLicensesDetails(ctx context.Context, db *postgres.DB, path, version string) (*DetailsPage, error) {
@ -371,8 +404,24 @@ func fetchLicensesDetails(ctx context.Context, db *postgres.DB, path, version st
if err != nil {
return nil, fmt.Errorf("db.fetchPackageHeader(ctx, db, %q, %q): %v", path, version, err)
}
dbLicenses, err := db.GetLicenses(ctx, path, version)
if err != nil {
return nil, fmt.Errorf("db.GetLicenses(ctx, %q, %q): %v", path, version, err)
}
licenses := make([]License, len(dbLicenses))
for i, l := range dbLicenses {
licenses[i] = License{
Anchor: licenseAnchor(l.FilePath),
License: l,
}
}
return &DetailsPage{
PackageHeader: pkgHeader,
Details: &LicensesDetails{
Licenses: licenses,
},
}, nil
}

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

@ -31,7 +31,7 @@ var (
Version: "v1.0.0",
Path: "test.com/module/pkg_name",
Synopsis: "Test package synopsis",
Licenses: sampleLicenseInfos,
Licenses: transformLicenseInfos(sampleLicenseInfos),
CommitTime: "today",
}
sampleInternalPackage = &internal.Package{
@ -60,7 +60,7 @@ var (
Version: "v1.0.0",
Path: "test.com/module/pkg_name",
Synopsis: "Test package synopsis",
Licenses: sampleLicenseInfos,
Licenses: transformLicenseInfos(sampleLicenseInfos),
}
)
@ -299,7 +299,7 @@ func TestFetchVersionsDetails(t *testing.T) {
Version: "v1.2.1",
Path: pkg1Path,
Synopsis: "test synopsis",
Licenses: []*internal.LicenseInfo{{Type: "MIT", FilePath: "LICENSE"}},
Licenses: transformLicenseInfos([]*internal.LicenseInfo{{Type: "MIT", FilePath: "LICENSE"}}),
CommitTime: "today",
},
},
@ -353,7 +353,7 @@ func TestFetchVersionsDetails(t *testing.T) {
Version: "v0.0.0-20140414041501-3c2ca4d52544",
Path: pkg1Path,
Synopsis: "test synopsis",
Licenses: []*internal.LicenseInfo{{Type: "MIT", FilePath: "LICENSE"}},
Licenses: transformLicenseInfos([]*internal.LicenseInfo{{Type: "MIT", FilePath: "LICENSE"}}),
CommitTime: "today",
},
},