Skip to content

Revert "Revert "Redirect on workspace stop" (#19323)" #19325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion components/dashboard/src/service/service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export class IDEFrontendService implements IDEFrontendDashboardService.IServer {
this.workspace = workspaceResponse.workspace!;
this.user = user;
this.ideCredentials = ideCredentials;
const reconcile = (status?: WorkspaceStatus) => {
const reconcile = async (status?: WorkspaceStatus) => {
const info = this.parseInfo(status ?? this.workspace.status!);
this.latestInfo = info;
const oldInstanceID = this.instanceID;
Expand All @@ -231,6 +231,15 @@ export class IDEFrontendService implements IDEFrontendDashboardService.IServer {
if (info.instanceId && oldInstanceID !== info.instanceId) {
this.auth();
}

// Redirect to custom url
if (
(info.statusPhase === "stopping" || info.statusPhase === "stopped") &&
info.workspaceType === "regular"
) {
await this.redirectToCustomUrl(info);
}

this.sendInfoUpdate(this.latestInfo);
};
reconcile();
Expand All @@ -255,6 +264,40 @@ export class IDEFrontendService implements IDEFrontendDashboardService.IServer {
};
}

private async redirectToCustomUrl(info: IDEFrontendDashboardService.Info) {
const isDataOps = await getExperimentsClient().getValueAsync("dataops", false, {
user: { id: this.user!.id },
gitpodHost: gitpodHostUrl.toString(),
});
const dataOpsRedirectUrl = await getExperimentsClient().getValueAsync("dataops_redirect_url", "undefined", {
user: { id: this.user!.id },
gitpodHost: gitpodHostUrl.toString(),
});

if (!isDataOps) {
return;
}

try {
const params: Record<string, string> = { workspaceID: info.workspaceID };
let redirectURL: string;
if (dataOpsRedirectUrl === "undefined") {
redirectURL = this.workspace.metadata?.originalContextUrl ?? "";
} else {
redirectURL = dataOpsRedirectUrl;
params.contextURL = this.workspace.metadata?.originalContextUrl ?? "";
}
const url = new URL(redirectURL);
url.search = new URLSearchParams([
...Array.from(url.searchParams.entries()),
...Object.entries(params),
]).toString();
this.relocate(url.toString());
} catch {
console.error("Invalid redirect URL");
}
}

// implements

private async auth() {
Expand Down
7 changes: 6 additions & 1 deletion components/supervisor/frontend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ IDEWebSocket.install();
const ideService = IDEFrontendService.create();
const loadingIDE = new Promise((resolve) => window.addEventListener("DOMContentLoaded", resolve, { once: true }));
const toStop = new DisposableCollection();
let willRedirect = false;

document.body.style.visibility = "hidden";
LoadingFrame.load().then(async (loading) => {
Expand All @@ -73,6 +74,10 @@ LoadingFrame.load().then(async (loading) => {
return;
}

frontendDashboardServiceClient.onWillRedirect(() => {
willRedirect = true;
});

document.title = frontendDashboardServiceClient.latestInfo.workspaceDescription ?? "gitpod";
window.gitpod.loggedUserID = frontendDashboardServiceClient.latestInfo.loggedUserId;
window.gitpod.openDesktopIDE = frontendDashboardServiceClient.openDesktopIDE.bind(frontendDashboardServiceClient);
Expand Down Expand Up @@ -159,7 +164,7 @@ LoadingFrame.load().then(async (loading) => {
};
const updateCurrentFrame = () => {
const newCurrent = nextFrame();
if (current === newCurrent) {
if (current === newCurrent || willRedirect) {
return;
}
current.style.visibility = "hidden";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export class FrontendDashboardServiceClient implements IDEFrontendDashboardServi
private readonly onOpenBrowserIDEEmitter = new Emitter<void>();
readonly onOpenBrowserIDE = this.onOpenBrowserIDEEmitter.event;

private readonly onWillRedirectEmitter = new Emitter<void>();
readonly onWillRedirect = this.onWillRedirectEmitter.event;

private resolveInit!: () => void;
private initPromise = new Promise<void>((resolve) => (this.resolveInit = resolve));

Expand Down Expand Up @@ -50,6 +53,7 @@ export class FrontendDashboardServiceClient implements IDEFrontendDashboardServi
this.onDidChangeEmitter.fire(this.latestInfo);
}
if (IDEFrontendDashboardService.isRelocateEventData(event.data)) {
this.onWillRedirectEmitter.fire();
window.location.href = event.data.url;
}
if (IDEFrontendDashboardService.isOpenBrowserIDE(event.data)) {
Expand Down