Skip to content

Commit

Permalink
Merge #25863
Browse files Browse the repository at this point in the history
25863: ui: use build version for docs links r=couchand a=couchand

The first commit is reviewed separately here: #25823

#### ui: extract dataFromServer global from login file

Since the dataFromServer will hold more than just login state, move it out to a
new file.

Release note: None

#### ui: use build version for docs links

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

Co-authored-by: Andrew Couch <[email protected]>
  • Loading branch information
craig[bot] and couchand committed May 23, 2018
2 parents aa02b29 + 2224056 commit c0c6e90
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 23 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
21 changes: 6 additions & 15 deletions pkg/ui/src/redux/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,11 @@ import { Action } from "redux";
import { userLogin } from "src/util/api";
import { AdminUIState } from "src/redux/state";
import { cockroach } from "src/js/protos";
import { getDataFromServer } from "src/util/dataFromServer";

import UserLoginRequest = cockroach.server.serverpb.UserLoginRequest;

// Tell TypeScript about `window.loggedInUser`, which is set in a script
// tag in index.html, the contents of which are generated in a Go template
// server-side.
declare global {
interface Window {
dataFromServer: {
ExperimentalUseLogin: boolean;
LoginEnabled: boolean;
LoggedInUser: string;
};
}
}
const dataFromServer = getDataFromServer();

// State for application use.

Expand Down Expand Up @@ -94,11 +85,11 @@ class NoLoginState {
export const selectLoginState = createSelector(
(state: AdminUIState) => state.login,
(login: LoginAPIState) => {
if (!window.dataFromServer || !window.dataFromServer.ExperimentalUseLogin) {
if (!dataFromServer.ExperimentalUseLogin) {
return new NoLoginState();
}

if (!window.dataFromServer.LoginEnabled) {
if (!dataFromServer.LoginEnabled) {
return new LoginDisabledState();
}

Expand All @@ -117,7 +108,7 @@ export interface LoginAPIState {
}

const emptyLoginState: LoginAPIState = {
loggedInUser: window.dataFromServer && window.dataFromServer.LoggedInUser,
loggedInUser: dataFromServer.LoggedInUser,
error: null,
inProgress: false,
};
Expand Down
19 changes: 19 additions & 0 deletions pkg/ui/src/util/dataFromServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface DataFromServer {
ExperimentalUseLogin: boolean;
LoginEnabled: boolean;
LoggedInUser: string;
Version: string;
}

// Tell TypeScript about `window.dataFromServer`, which is set in a script
// tag in index.html, the contents of which are generated in a Go template
// server-side.
declare global {
interface Window {
dataFromServer: DataFromServer;
}
}

export function getDataFromServer(): DataFromServer {
return window.dataFromServer || {};
}
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 c0c6e90

Please sign in to comment.