internal/frontend: use unversioned module links in overview

If the request URL is unversioned, the module link on the overview
page should be too.

Updates b/144217401.

Change-Id: I4d10555639a1ffd44948206694b2cea3cb2a036b
Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/601915
CI-Result: Cloud Build <devtools-proctor-result-processor@system.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Jonathan Amsterdam 2019-11-19 16:58:37 -05:00 коммит произвёл Julie Qiu
Родитель 4d4332b5bc
Коммит 401f3d548c
5 изменённых файлов: 25 добавлений и 13 удалений

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

@ -75,7 +75,7 @@ func (s *Server) serveDirectoryPage(w http.ResponseWriter, r *http.Request, dirP
header.URL = constructDirectoryURL(dbDir.Path, dbDir.ModulePath, internal.LatestVersion)
}
details, err := constructDetailsForDirectory(r, tab, s.ds, dbDir, licenses)
details, err := constructDetailsForDirectory(r, tab, dbDir, licenses)
if err != nil {
log.Errorf("serveDirectoryPage for %s@%s: %v", dirPath, version, err)
s.serveErrorPage(w, r, http.StatusInternalServerError, nil)

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

@ -33,11 +33,18 @@ type OverviewDetails struct {
RepositoryURL string
}
// versionedLinks says whether the constructed URLs should have versions.
// constructOverviewDetails uses the given version to construct an OverviewDetails.
func constructOverviewDetails(vi *internal.VersionInfo, licmetas []*license.Metadata) *OverviewDetails {
func constructOverviewDetails(vi *internal.VersionInfo, licmetas []*license.Metadata, versionedLinks bool) *OverviewDetails {
var lv string
if versionedLinks {
lv = linkableVersion(vi.Version, vi.ModulePath)
} else {
lv = internal.LatestVersion
}
overview := &OverviewDetails{
ModulePath: vi.ModulePath,
ModuleURL: constructModuleURL(vi.ModulePath, linkableVersion(vi.Version, vi.ModulePath)),
ModuleURL: constructModuleURL(vi.ModulePath, lv),
RepositoryURL: vi.SourceInfo.RepoURL(),
Redistributable: license.AreRedistributable(licmetas),
}
@ -49,8 +56,8 @@ func constructOverviewDetails(vi *internal.VersionInfo, licmetas []*license.Meta
}
// constructPackageOverviewDetails uses data for the given package to return an OverviewDetails.
func constructPackageOverviewDetails(pkg *internal.VersionedPackage) *OverviewDetails {
od := constructOverviewDetails(&pkg.VersionInfo, pkg.Licenses)
func constructPackageOverviewDetails(pkg *internal.VersionedPackage, versionedLinks bool) *OverviewDetails {
od := constructOverviewDetails(&pkg.VersionInfo, pkg.Licenses, versionedLinks)
od.PackageSourceURL = pkg.SourceInfo.DirectoryURL(packageSubdir(pkg.Path, pkg.ModulePath))
if !pkg.IsRedistributable() {
od.Redistributable = false

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

@ -45,7 +45,7 @@ func TestFetchOverviewDetails(t *testing.T) {
t.Fatal(err)
}
got := constructOverviewDetails(&tc.version.VersionInfo, sample.LicenseMetadata)
got := constructOverviewDetails(&tc.version.VersionInfo, sample.LicenseMetadata, true)
if diff := cmp.Diff(tc.wantDetails, got); diff != "" {
t.Errorf("constructOverviewDetails(%q, %q) mismatch (-want +got):\n%s", tc.version.Packages[0].Path, tc.version.Version, diff)
}

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

@ -411,7 +411,7 @@ func TestServer(t *testing.T) {
in(".Overview-module",
text("Module"),
in("a",
href("/mod/github.com/valid_module_name@v1.0.0"),
href("/mod/github.com/valid_module_name"),
text("github.com/valid_module_name"))),
in(".Overview-sourceCodeLink",
text("Repository"),
@ -495,8 +495,7 @@ func TestServer(t *testing.T) {
want: in("",
pagecheck.ModuleHeader(mod, unversioned),
in(".Overview-module a",
// TODO(b/144217401): should be unversioned
href(fmt.Sprintf("/mod/%s@%s", sample.ModulePath, sample.VersionString)),
href("/mod/"+sample.ModulePath),
text("^"+sample.ModulePath+"$")),
in(".Overview-readmeContent", text(`readme`))),
},

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

@ -8,6 +8,8 @@ import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"golang.org/x/discovery/internal"
"golang.org/x/discovery/internal/license"
@ -154,11 +156,15 @@ func fetchDetailsForPackage(ctx context.Context, r *http.Request, tab string, ds
case "licenses":
return fetchPackageLicensesDetails(ctx, ds, pkg)
case "overview":
return constructPackageOverviewDetails(pkg), nil
return constructPackageOverviewDetails(pkg, urlIsVersioned(r.URL)), nil
}
return nil, fmt.Errorf("BUG: unable to fetch details: unknown tab %q", tab)
}
func urlIsVersioned(url *url.URL) bool {
return strings.ContainsRune(url.Path, '@')
}
// fetchDetailsForModule returns tab details by delegating to the correct detail
// handler.
func fetchDetailsForModule(ctx context.Context, r *http.Request, tab string, ds DataSource, vi *internal.VersionInfo, licenses []*license.License) (interface{}, error) {
@ -171,17 +177,17 @@ func fetchDetailsForModule(ctx context.Context, r *http.Request, tab string, ds
return fetchModuleVersionsDetails(ctx, ds, vi)
case "overview":
// TODO(b/138448402): implement remaining module views.
return constructOverviewDetails(vi, license.ToMetadatas(licenses)), nil
return constructOverviewDetails(vi, license.ToMetadatas(licenses), urlIsVersioned(r.URL)), nil
}
return nil, fmt.Errorf("BUG: unable to fetch details: unknown tab %q", tab)
}
// constructDetailsForDirectory returns tab details by delegating to the correct
// detail handler.
func constructDetailsForDirectory(r *http.Request, tab string, ds DataSource, dir *internal.Directory, licenses []*license.License) (interface{}, error) {
func constructDetailsForDirectory(r *http.Request, tab string, dir *internal.Directory, licenses []*license.License) (interface{}, error) {
switch tab {
case "overview":
return constructOverviewDetails(&dir.VersionInfo, license.ToMetadatas(licenses)), nil
return constructOverviewDetails(&dir.VersionInfo, license.ToMetadatas(licenses), urlIsVersioned(r.URL)), nil
case "subdirectories":
// Ideally we would just use fetchDirectoryDetails here so that it
// follows the same code path as fetchDetailsForModule and