Skip to content

Commit

Permalink
internal/middleware: change latestversion to latestminorversion
Browse files Browse the repository at this point in the history
With the inclusion of the latest major version function, this commit
changes current "latestversion" naming to "latestminorversion" in the
latestversion middleware to specify the difference between both
functions.

Updates golang/go#37765

Change-Id: I1c4d8edf6ac30431ef1278abaa334d96cd459258
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/251083
Reviewed-by: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
  • Loading branch information
Miguel Acero committed Aug 28, 2020
1 parent a457b9e commit 2a24ee1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion content/static/html/pages/details.tmpl
Expand Up @@ -50,7 +50,7 @@
{{$ppath = $header.Path}}
{{end}}
<!-- Do not reformat the data attributes of the following div: the server uses a regexp to extract them. -->
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTCLASS$$"
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTMINORCLASS$$"
data-version="{{$header.LinkVersion}}" data-mpath="{{$header.ModulePath}}" data-ppath="{{$ppath}}" data-pagetype="{{$pageType}}">
<span>Latest</span>
<a href="{{$header.LatestURL}}">Go to latest</a>
Expand Down
4 changes: 2 additions & 2 deletions internal/frontend/header.go
Expand Up @@ -69,7 +69,7 @@ func createPackage(pkg *internal.PackageMeta, mi *internal.ModuleInfo, latestReq
Licenses: transformLicenseMetadata(pkg.Licenses),
Module: *m,
URL: constructPackageURL(pkg.Path, mi.ModulePath, urlVersion),
LatestURL: constructPackageURL(pkg.Path, mi.ModulePath, middleware.LatestVersionPlaceholder),
LatestURL: constructPackageURL(pkg.Path, mi.ModulePath, middleware.LatestMinorVersionPlaceholder),
}, nil
}

Expand All @@ -92,7 +92,7 @@ func createModule(mi *internal.ModuleInfo, licmetas []*licenses.Metadata, latest
IsRedistributable: mi.IsRedistributable,
Licenses: transformLicenseMetadata(licmetas),
URL: constructModuleURL(mi.ModulePath, urlVersion),
LatestURL: constructModuleURL(mi.ModulePath, middleware.LatestVersionPlaceholder),
LatestURL: constructModuleURL(mi.ModulePath, middleware.LatestMinorVersionPlaceholder),
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/frontend/header_test.go
Expand Up @@ -35,8 +35,8 @@ func samplePackage(mutators ...func(*Package)) *Package {
}
p.URL = constructPackageURL(p.Path, p.ModulePath, p.LinkVersion)
p.Module.URL = constructModuleURL(p.ModulePath, p.LinkVersion)
p.LatestURL = constructPackageURL(p.Path, p.ModulePath, middleware.LatestVersionPlaceholder)
p.Module.LatestURL = constructModuleURL(p.ModulePath, middleware.LatestVersionPlaceholder)
p.LatestURL = constructPackageURL(p.Path, p.ModulePath, middleware.LatestMinorVersionPlaceholder)
p.Module.LatestURL = constructModuleURL(p.ModulePath, middleware.LatestMinorVersionPlaceholder)
p.Module.LinkVersion = linkVersion(sample.VersionString, sample.ModulePath)
return p
}
Expand Down
8 changes: 4 additions & 4 deletions internal/frontend/latest_version.go
Expand Up @@ -21,11 +21,11 @@ import (
func (s *Server) LatestVersion(ctx context.Context, packagePath, modulePath, pageType string) string {
// It is okay to use a different DataSource (DB connection) than the rest of the
// request, because this makes a self-contained call on the DB.
v, err := latestVersion(ctx, s.getDataSource(ctx), packagePath, modulePath, pageType)
v, err := latestMinorVersion(ctx, s.getDataSource(ctx), packagePath, modulePath, pageType)
if err != nil {
// We get NotFound errors from directories; they clutter the log.
if !errors.Is(err, derrors.NotFound) {
log.Errorf(ctx, "GetLatestVersion: %v", err)
log.Errorf(ctx, "GetLatestMinorVersion: %v", err)
}
return ""
}
Expand All @@ -34,8 +34,8 @@ func (s *Server) LatestVersion(ctx context.Context, packagePath, modulePath, pag

// TODO(https://github.com/golang/go/issues/40107): this is currently tested in server_test.go, but
// we should add tests for this function.
func latestVersion(ctx context.Context, ds internal.DataSource, packagePath, modulePath, pageType string) (_ string, err error) {
defer derrors.Wrap(&err, "latestVersion(ctx, %q, %q)", modulePath, packagePath)
func latestMinorVersion(ctx context.Context, ds internal.DataSource, packagePath, modulePath, pageType string) (_ string, err error) {
defer derrors.Wrap(&err, "latestMinorVersion(ctx, %q, %q)", modulePath, packagePath)
if experiment.IsActive(ctx, internal.ExperimentUsePathInfo) {
fullPath := packagePath
if pageType == pageTypeModule || pageType == pageTypeStdLib {
Expand Down
22 changes: 11 additions & 11 deletions internal/middleware/latestversion.go
Expand Up @@ -15,8 +15,8 @@ import (
)

const (
latestClassPlaceholder = "$$GODISCOVERY_LATESTCLASS$$"
LatestVersionPlaceholder = "$$GODISCOVERY_LATESTVERSION$$"
latestMinorClassPlaceholder = "$$GODISCOVERY_LATESTMINORCLASS$$"
LatestMinorVersionPlaceholder = "$$GODISCOVERY_LATESTMINORVERSION$$"
)

// latestInfoRegexp extracts values needed to determine the latest-version badge from a page's HTML.
Expand All @@ -40,18 +40,18 @@ func LatestVersion(latest latestFunc) Middleware {
modulePath := string(matches[2])
packagePath := string(matches[3])
pageType := string(matches[4])
latestVersion := latest(r.Context(), packagePath, modulePath, pageType)
latestClass := "DetailsHeader-badge"
latestMinorVersion := latest(r.Context(), packagePath, modulePath, pageType)
latestMinorClass := "DetailsHeader-badge"
switch {
case latestVersion == "":
latestClass += "--unknown"
case latestVersion == version:
latestClass += "--latest"
case latestMinorVersion == "":
latestMinorClass += "--unknown"
case latestMinorVersion == version:
latestMinorClass += "--latest"
default:
latestClass += "--goToLatest"
latestMinorClass += "--goToLatest"
}
body = bytes.ReplaceAll(body, []byte(latestClassPlaceholder), []byte(latestClass))
body = bytes.ReplaceAll(body, []byte(LatestVersionPlaceholder), []byte(latestVersion))
body = bytes.ReplaceAll(body, []byte(latestMinorClassPlaceholder), []byte(latestMinorClass))
body = bytes.ReplaceAll(body, []byte(LatestMinorVersionPlaceholder), []byte(latestMinorVersion))
}
if _, err := w.Write(body); err != nil {
log.Errorf(r.Context(), "LatestVersion, writing: %v", err)
Expand Down
32 changes: 16 additions & 16 deletions internal/middleware/latestversion_test.go
Expand Up @@ -24,10 +24,10 @@ func TestLatestVersion(t *testing.T) {
name: "package version is not latest",
latest: func(context.Context, string, string, string) string { return "v1.2.3" },
in: `
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTCLASS$$"
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTMINORCLASS$$"
data-version="v1.0.0" data-mpath="p1/p2" data-ppath="p1/p2/p3" data-pagetype="pkg">
<span>Latest</span>
<a href="p1/p2@$$GODISCOVERY_LATESTVERSION$$/p3">Go to latest</a>
<a href="p1/p2@$$GODISCOVERY_LATESTMINORVERSION$$/p3">Go to latest</a>
</div>`,
want: `
<div class="DetailsHeader-badge DetailsHeader-badge--goToLatest"
Expand All @@ -40,10 +40,10 @@ func TestLatestVersion(t *testing.T) {
name: "package version is latest",
latest: func(context.Context, string, string, string) string { return "v1.2.3" },
in: `
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTCLASS$$"
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTMINORCLASS$$"
data-version="v1.2.3" data-mpath="p1/p2" data-ppath="p1/p2/p3" data-pagetype="pkg">
<span>Latest</span>
<a href="p1/p2@$$GODISCOVERY_LATESTVERSION$$/p3">Go to latest</a>
<a href="p1/p2@$$GODISCOVERY_LATESTMINORVERSION$$/p3">Go to latest</a>
</div>`,
want: `
<div class="DetailsHeader-badge DetailsHeader-badge--latest"
Expand All @@ -56,10 +56,10 @@ func TestLatestVersion(t *testing.T) {
name: "package version with build is latest",
latest: func(context.Context, string, string, string) string { return "v1.2.3+build" },
in: `
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTCLASS$$"
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTMINORCLASS$$"
data-version="v1.2.3&#43;build" data-mpath="p1/p2" data-ppath="p1/p2/p3" data-pagetype="pkg">
<span>Latest</span>
<a href="p1/p2@$$GODISCOVERY_LATESTVERSION$$/p3">Go to latest</a>
<a href="p1/p2@$$GODISCOVERY_LATESTMINORVERSION$$/p3">Go to latest</a>
</div>`,
want: `
<div class="DetailsHeader-badge DetailsHeader-badge--latest"
Expand All @@ -72,10 +72,10 @@ func TestLatestVersion(t *testing.T) {
name: "module version is not latest",
latest: func(context.Context, string, string, string) string { return "v1.2.3" },
in: `
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTCLASS$$"
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTMINORCLASS$$"
data-version="v1.0.0" data-mpath="p1/p2" data-ppath="" data-pagetype="pkg">
<span>Latest</span>
<a href="mod/p1/p2@$$GODISCOVERY_LATESTVERSION$$">Go to latest</a>
<a href="mod/p1/p2@$$GODISCOVERY_LATESTMINORVERSION$$">Go to latest</a>
</div>`,
want: `
<div class="DetailsHeader-badge DetailsHeader-badge--goToLatest"
Expand All @@ -88,10 +88,10 @@ func TestLatestVersion(t *testing.T) {
name: "module version is latest",
latest: func(context.Context, string, string, string) string { return "v1.2.3" },
in: `
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTCLASS$$"
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTMINORCLASS$$"
data-version="v1.2.3" data-mpath="p1/p2" data-ppath="" data-pagetype="pkg">
<span>Latest</span>
<a href="mod/p1/p2@$$GODISCOVERY_LATESTVERSION$$">Go to latest</a>
<a href="mod/p1/p2@$$GODISCOVERY_LATESTMINORVERSION$$">Go to latest</a>
</div>`,
want: `
<div class="DetailsHeader-badge DetailsHeader-badge--latest"
Expand All @@ -104,10 +104,10 @@ func TestLatestVersion(t *testing.T) {
name: "latest func returns empty string",
latest: func(context.Context, string, string, string) string { return "" },
in: `
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTCLASS$$"
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTMINORCLASS$$"
data-version="v1.2.3" data-mpath="p1/p2" data-ppath="" data-pagetype="pkg">
<span>Latest</span>
<a href="mod/p1/p2@$$GODISCOVERY_LATESTVERSION$$">Go to latest</a>
<a href="mod/p1/p2@$$GODISCOVERY_LATESTMINORVERSION$$">Go to latest</a>
</div>`,
want: `
<div class="DetailsHeader-badge DetailsHeader-badge--unknown"
Expand All @@ -120,14 +120,14 @@ func TestLatestVersion(t *testing.T) {
name: "no regexp match",
latest: func(context.Context, string, string, string) string { return "v1.2.3" },
in: `
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTCLASS$$">
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTMINORCLASS$$">
<span>Latest</span>
<a href="mod/p1/p2@$$GODISCOVERY_LATESTVERSION$$">Go to latest</a>
<a href="mod/p1/p2@$$GODISCOVERY_LATESTMINORVERSION$$">Go to latest</a>
</div>`,
want: `
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTCLASS$$">
<div class="DetailsHeader-badge $$GODISCOVERY_LATESTMINORCLASS$$">
<span>Latest</span>
<a href="mod/p1/p2@$$GODISCOVERY_LATESTVERSION$$">Go to latest</a>
<a href="mod/p1/p2@$$GODISCOVERY_LATESTMINORVERSION$$">Go to latest</a>
</div>`,
},
} {
Expand Down

0 comments on commit 2a24ee1

Please sign in to comment.