Skip to content

Commit

Permalink
ui: use build version for docs links
Browse files Browse the repository at this point in the history
Previously, the current version was hard-coded into the docs links,
requiring manual effort to keep in sync with the released version
of the binary.  This change interpolates the `build.VersionPrefix()`
instead, so that it's always correct.

Fixes: #19024
Release note: None
  • Loading branch information
couchand committed May 23, 2018
1 parent ebeeb4f commit 2224056
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
4 changes: 3 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (

"github.com/cockroachdb/cmux"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/build"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/kv"
Expand Down Expand Up @@ -1923,8 +1924,9 @@ func serveUIAssets(fileServer http.Handler, cfg Config) http.Handler {

// Construct arguments for template.
tmplArgs := ui.IndexHTMLArgs{
LoginEnabled: cfg.RequireWebSession(),
ExperimentalUseLogin: cfg.EnableWebSessionAuthentication,
LoginEnabled: cfg.RequireWebSession(),
Version: build.VersionPrefix(),
}
loggedInUser, ok := request.Context().Value(loggedInUserKey{}).(string)
if ok && loggedInUser != "" {
Expand Down
25 changes: 22 additions & 3 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"time"

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/build"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/keys"
Expand Down Expand Up @@ -988,7 +989,13 @@ func TestServeIndexHTML(t *testing.T) {
t.Fatal(err)
}
respString := string(respBytes)
expected := fmt.Sprintf(htmlTemplate, `{"ExperimentalUseLogin":false,"LoginEnabled":false,"LoggedInUser":null}`)
expected := fmt.Sprintf(
htmlTemplate,
fmt.Sprintf(
`{"ExperimentalUseLogin":false,"LoginEnabled":false,"LoggedInUser":null,"Version":"%s"}`,
build.VersionPrefix(),
),
)
if respString != expected {
t.Fatalf("expected %s; got %s", expected, respString)
}
Expand All @@ -1012,8 +1019,20 @@ func TestServeIndexHTML(t *testing.T) {
client http.Client
json string
}{
{loggedInClient, `{"ExperimentalUseLogin":true,"LoginEnabled":true,"LoggedInUser":"authentic_user"}`},
{loggedOutClient, `{"ExperimentalUseLogin":true,"LoginEnabled":true,"LoggedInUser":null}`},
{
loggedInClient,
fmt.Sprintf(
`{"ExperimentalUseLogin":true,"LoginEnabled":true,"LoggedInUser":"authentic_user","Version":"%s"}`,
build.VersionPrefix(),
),
},
{
loggedOutClient,
fmt.Sprintf(
`{"ExperimentalUseLogin":true,"LoginEnabled":true,"LoggedInUser":null,"Version":"%s"}`,
build.VersionPrefix(),
),
},
}

for _, testCase := range cases {
Expand Down
1 change: 1 addition & 0 deletions pkg/ui/src/util/dataFromServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface DataFromServer {
ExperimentalUseLogin: boolean;
LoginEnabled: boolean;
LoggedInUser: string;
Version: string;
}

// Tell TypeScript about `window.dataFromServer`, which is set in a script
Expand Down
8 changes: 4 additions & 4 deletions pkg/ui/src/util/docs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// TODO(benesch): Derive this URL from build.VersionPrefix() rather than
// hardcoding it. This is harder than it sounds, since we don't want to encode
// the branch name in embedded.go.
const docsURLBase = "https://www.cockroachlabs.com/docs/v2.0";
import { getDataFromServer } from "src/util/dataFromServer";

const version = getDataFromServer().Version || "stable";
const docsURLBase = "https://www.cockroachlabs.com/docs/" + version;

export default function docsURL(pageName: string): string {
return `${docsURLBase}/${pageName}`;
Expand Down
1 change: 1 addition & 0 deletions pkg/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type IndexHTMLArgs struct {
ExperimentalUseLogin bool
LoginEnabled bool
LoggedInUser *string
Version string
}

func init() {
Expand Down

0 comments on commit 2224056

Please sign in to comment.