Skip to content

Commit 493b3ba

Browse files
committed
Redirect to custom url when workspace stops
1 parent f458160 commit 493b3ba

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

components/supervisor/frontend/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"stream-browserify": "^2.0.1",
1313
"url": "^0.11.1",
1414
"util": "^0.11.1",
15-
"uuid": "8.3.2"
15+
"uuid": "8.3.2",
16+
"configcat-js": "^6.0.0"
1617
},
1718
"devDependencies": {
1819
"@types/sharedworker": "^0.0.29",
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License.AGPL.txt in the project root for license information.
5+
*/
6+
7+
import * as configcat from "configcat-js";
8+
import { ConfigCatClient } from "@gitpod/gitpod-protocol/lib/experiments/configcat";
9+
import { Client } from "@gitpod/gitpod-protocol/lib/experiments/types";
10+
import { LogLevel } from "configcat-common";
11+
import { serverUrl } from "../shared/urls";
12+
13+
let client: Client | undefined;
14+
15+
export function getExperimentsClient(): Client {
16+
// We have already instantiated a client, we can just re-use it.
17+
if (client !== undefined) {
18+
return client;
19+
}
20+
21+
client = newProxyConfigCatClient();
22+
return client;
23+
}
24+
25+
function newProxyConfigCatClient(): Client {
26+
const clientKey = "gitpod"; // the client key is populated by the proxy
27+
const client = configcat.createClientWithLazyLoad(clientKey, {
28+
logger: configcat.createConsoleLogger(LogLevel.Error),
29+
cacheTimeToLiveSeconds: 60 * 3, // 3 minutes
30+
requestTimeoutMs: 1500,
31+
baseUrl: serverUrl.with(() => ({ pathname: "/configcat" })).toString(),
32+
});
33+
34+
return new ConfigCatClient(client);
35+
}

components/supervisor/frontend/src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ import * as IDEWebSocket from "./ide/ide-web-socket";
5656
import { SupervisorServiceClient } from "./ide/supervisor-service-client";
5757
import * as LoadingFrame from "./shared/loading-frame";
5858
import { workspaceUrl } from "./shared/urls";
59+
import { getExperimentsClient } from "./experiments/client";
5960

6061
window.gitpod = {} as any;
6162
IDEWorker.install();
6263
IDEWebSocket.install();
6364
const ideService = IDEFrontendService.create();
6465
const loadingIDE = new Promise((resolve) => window.addEventListener("DOMContentLoaded", resolve, { once: true }));
6566
const toStop = new DisposableCollection();
67+
const experimentsClient = getExperimentsClient();
6668

6769
document.body.style.visibility = "hidden";
6870
LoadingFrame.load().then(async (loading) => {
@@ -284,6 +286,7 @@ LoadingFrame.load().then(async (loading) => {
284286
IDEWebSocket.connectWorkspace(),
285287
frontendDashboardServiceClient.onInfoUpdate((status) => {
286288
if (status.statusPhase === "stopping" || status.statusPhase === "stopped") {
289+
maybeRedirectToCustomUrl();
287290
toStop.dispose();
288291
}
289292
}),
@@ -295,3 +298,17 @@ LoadingFrame.load().then(async (loading) => {
295298
//#endregion
296299
})();
297300
});
301+
302+
async function maybeRedirectToCustomUrl() {
303+
const redirectURL = await experimentsClient.getValueAsync("dataops", "", {});
304+
if (!redirectURL) {
305+
return;
306+
}
307+
308+
try {
309+
const url = new URL(redirectURL);
310+
window.location.href = url.toString();
311+
} catch {
312+
console.error("Invalid redirect URL");
313+
}
314+
}

0 commit comments

Comments
 (0)