Skip to content

Commit 53a66d2

Browse files
committed
[server] add editor prefix to choose custom IDE
1 parent beb02b6 commit 53a66d2

File tree

5 files changed

+69
-10
lines changed

5 files changed

+69
-10
lines changed

components/gitpod-protocol/src/context-url.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export namespace ContextURL {
2020
export const IMAGEBUILD_PREFIX = "imagebuild";
2121
export const SNAPSHOT_PREFIX = "snapshot";
2222
export const REFERRER_PREFIX = "referrer:";
23+
export const EDITOR_PREFIX = "editor:";
2324

2425
/**
2526
* This function will (try to) return the HTTP(S) URL of the context the user originally created this workspace on.
@@ -94,7 +95,8 @@ export namespace ContextURL {
9495
firstSegment === INCREMENTAL_PREBUILD_PREFIX ||
9596
firstSegment === IMAGEBUILD_PREFIX ||
9697
firstSegment === SNAPSHOT_PREFIX ||
97-
firstSegment.startsWith(REFERRER_PREFIX)
98+
firstSegment.startsWith(REFERRER_PREFIX) ||
99+
firstSegment.startsWith(EDITOR_PREFIX)
98100
) {
99101
return segmentsToURL(1);
100102
}

components/gitpod-protocol/src/protocol.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,14 +863,26 @@ export namespace PrebuiltWorkspaceContext {
863863
}
864864
}
865865

866+
export interface WithEditorContext extends WorkspaceContext {
867+
ide: string;
868+
useLatest?: boolean;
869+
referrer?: string;
870+
}
871+
872+
export namespace WithEditorContext {
873+
export function is(context: any): context is WithEditorContext {
874+
return context && "ide" in context;
875+
}
876+
}
877+
866878
export interface WithReferrerContext extends WorkspaceContext {
867879
referrer: string;
868880
referrerIde?: string;
869881
}
870882

871883
export namespace WithReferrerContext {
872884
export function is(context: any): context is WithReferrerContext {
873-
return context && "referrer" in context;
885+
return context && "referrer" in context && !("ide" in context);
874886
}
875887
}
876888

components/server/src/container-module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ import { DebugApp } from "./debug-app";
9696
import { LocalMessageBroker, LocalRabbitMQBackedMessageBroker } from "./messaging/local-message-broker";
9797
import { contentServiceBinder } from "@gitpod/content-service/lib/sugar";
9898
import { ReferrerPrefixParser } from "./workspace/referrer-prefix-context-parser";
99+
import { EditorPrefixParser } from "./workspace/editor-prefix-context-parser";
99100
import { InstallationAdminTelemetryDataProvider } from "./installation-admin/telemetry-data-provider";
100101

101102
export const productionContainerModule = new ContainerModule((bind, unbind, isBound, rebind) => {
@@ -169,6 +170,7 @@ export const productionContainerModule = new ContainerModule((bind, unbind, isBo
169170
bind(SnapshotContextParser).toSelf().inSingletonScope();
170171
bind(IContextParser).to(SnapshotContextParser).inSingletonScope();
171172
bind(IPrefixContextParser).to(ReferrerPrefixParser).inSingletonScope();
173+
bind(IPrefixContextParser).to(EditorPrefixParser).inSingletonScope();
172174
bind(IPrefixContextParser).to(EnvvarPrefixParser).inSingletonScope();
173175
bind(IPrefixContextParser).to(ImageBuildPrefixContextParser).inSingletonScope();
174176
bind(IPrefixContextParser).to(AdditionalContentPrefixContextParser).inSingletonScope();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) 2020 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 { IPrefixContextParser } from "./context-parser";
8+
import { User, WorkspaceContext, WithEditorContext } from "@gitpod/gitpod-protocol";
9+
import { injectable } from "inversify";
10+
11+
@injectable()
12+
export class EditorPrefixParser implements IPrefixContextParser {
13+
private readonly prefix = /^\/?editor:([^\/:]*?)(?::([^\/:]*?))?(?::([^\/:]*?))?\//;
14+
15+
findPrefix(_: User, context: string): string | undefined {
16+
return this.prefix.exec(context)?.[0] || undefined;
17+
}
18+
19+
async handle(_: User, prefix: string, context: WorkspaceContext): Promise<WorkspaceContext | WithEditorContext> {
20+
const matches = this.prefix.exec(prefix);
21+
const ide = matches?.[1];
22+
const useLatest = matches?.[2] === "latest";
23+
const referrer = matches?.[3];
24+
return ide ? { ...context, ide, useLatest, referrer } : context;
25+
}
26+
}

components/server/src/workspace/workspace-starter.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ import * as path from "path";
103103
import * as grpc from "@grpc/grpc-js";
104104
import { IDEConfig, IDEConfigService } from "../ide-config";
105105
import { EnvVarWithValue } from "@gitpod/gitpod-protocol/src/protocol";
106-
import { WithReferrerContext } from "@gitpod/gitpod-protocol/lib/protocol";
106+
import { WithEditorContext, WithReferrerContext } from "@gitpod/gitpod-protocol/lib/protocol";
107107
import { IDEOption, IDEOptions } from "@gitpod/gitpod-protocol/lib/ide-protocol";
108108
import { Deferred } from "@gitpod/gitpod-protocol/lib/util/deferred";
109109
import { ExtendedUser } from "@gitpod/ws-manager/lib/constraints";
@@ -607,8 +607,12 @@ export class WorkspaceStarter {
607607
user.additionalData.ideSettings = migratted;
608608
}
609609

610-
const ideChoice = user.additionalData?.ideSettings?.defaultIde;
611-
const useLatest = !!user.additionalData?.ideSettings?.useLatestVersion;
610+
const preferenceIDE = user.additionalData?.ideSettings?.defaultIde;
611+
const perferenceUseLatest = !!user.additionalData?.ideSettings?.useLatestVersion;
612+
const ideChoice = WithEditorContext.is(workspace.context) ? workspace.context.ide : preferenceIDE;
613+
const useLatest = WithEditorContext.is(workspace.context)
614+
? workspace.context.useLatest ?? false
615+
: perferenceUseLatest;
612616

613617
// TODO(cw): once we allow changing the IDE in the workspace config (i.e. .gitpod.yml), we must
614618
// give that value precedence over the default choice.
@@ -618,7 +622,7 @@ export class WorkspaceStarter {
618622
ideConfig: {
619623
// We only check user setting because if code(insider) but desktopIde has no latestImage
620624
// it still need to notice user that this workspace is using latest IDE
621-
useLatest: user.additionalData?.ideSettings?.useLatestVersion,
625+
useLatest,
622626
},
623627
};
624628

@@ -633,11 +637,17 @@ export class WorkspaceStarter {
633637
configuration.desktopIdeImage = choose.desktopIdeImage;
634638
}
635639

640+
log.info(
641+
{ context: workspace.context, isEditorContext: WithEditorContext.is(workspace.context) },
642+
"check context",
643+
);
644+
636645
const referrerIde = this.resolveReferrerIDE(workspace, user, ideConfig);
637646
if (referrerIde) {
638-
configuration.desktopIdeImage = useLatest
647+
configuration.desktopIdeImage = perferenceUseLatest
639648
? referrerIde.option.latestImage ?? referrerIde.option.image
640649
: referrerIde.option.image;
650+
configuration.ideConfig!.useLatest = perferenceUseLatest;
641651
if (!user.additionalData?.ideSettings) {
642652
// A user does not have IDE settings configured yet configure it with a referrer ide as default.
643653
const additionalData = user?.additionalData || {};
@@ -691,17 +701,24 @@ export class WorkspaceStarter {
691701
},
692702
configuration,
693703
};
694-
if (WithReferrerContext.is(workspace.context)) {
704+
705+
const trackIDEPreferrer = (referrer?: string, referrerIde?: string, useLatest?: boolean) => {
695706
this.analytics.track({
696707
userId: user.id,
697708
event: "ide_referrer",
698709
properties: {
699710
workspaceId: workspace.id,
700711
instanceId: instance.id,
701-
referrer: workspace.context.referrer,
702-
referrerIde: workspace.context.referrerIde,
712+
referrer,
713+
useLatest,
714+
referrerIde,
703715
},
704716
});
717+
};
718+
if (WithReferrerContext.is(workspace.context)) {
719+
trackIDEPreferrer(workspace.context.referrer, workspace.context.referrerIde, perferenceUseLatest);
720+
} else if (WithEditorContext.is(workspace.context)) {
721+
trackIDEPreferrer(workspace.context.referrer, workspace.context.ide, workspace.context.useLatest);
705722
}
706723
return instance;
707724
}

0 commit comments

Comments
 (0)