Skip to content

Commit

Permalink
move migrate to resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
akosyakov committed Dec 2, 2022
1 parent 1df9c47 commit cebaf02
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 44 deletions.
28 changes: 27 additions & 1 deletion components/server/src/ide-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See License-AGPL.txt in the project root for license information.
*/

import { TaskConfig, Workspace } from "@gitpod/gitpod-protocol";
import { IDESettings, TaskConfig, User, Workspace } from "@gitpod/gitpod-protocol";
import { IDEOptions, IDEClient } from "@gitpod/gitpod-protocol/lib/ide-protocol";
import {
IDEServiceClient,
Expand Down Expand Up @@ -42,6 +42,32 @@ export class IDEService {
}
}

migrateSettings(user: User): IDESettings | undefined {
if (!user?.additionalData?.ideSettings || user.additionalData.ideSettings.settingVersion === "2.0") {
return undefined;
}
const newIDESettings: IDESettings = {
settingVersion: "2.0",
};
const ideSettings = user.additionalData.ideSettings;
if (ideSettings.useDesktopIde) {
if (ideSettings.defaultDesktopIde === "code-desktop") {
newIDESettings.defaultIde = "code-desktop";
} else if (ideSettings.defaultDesktopIde === "code-desktop-insiders") {
newIDESettings.defaultIde = "code-desktop";
newIDESettings.useLatestVersion = true;
} else {
newIDESettings.defaultIde = ideSettings.defaultDesktopIde;
newIDESettings.useLatestVersion = ideSettings.useLatestVersion;
}
} else {
const useLatest = ideSettings.defaultIde === "code-latest";
newIDESettings.defaultIde = "code";
newIDESettings.useLatestVersion = useLatest;
}
return newIDESettings;
}

async resolveWorkspaceConfig(req: ResolveWorkspaceConfigRequest): Promise<ResolveWorkspaceConfigResponse> {
for (let attempt = 0; attempt < 15; attempt++) {
if (attempt != 0) {
Expand Down
23 changes: 12 additions & 11 deletions components/server/src/workspace/workspace-starter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@

import { User } from "@gitpod/gitpod-protocol";
import * as chai from "chai";
import { migrationIDESettings } from "./workspace-starter";
import { IDEService } from "../ide-service";
const expect = chai.expect;

describe("workspace-starter", function () {
describe("migrationIDESettings", function () {
describe("ide-service", function () {
describe("ideService.migrateSettings", function () {
const ideService = new IDEService();
it("with no ideSettings should be undefined", function () {
const user: User = {
id: "string",
creationDate: "string",
identities: [],
additionalData: {},
};
const result = migrationIDESettings(user);
const result = ideService.migrateSettings(user);
expect(result).to.undefined;
});

Expand All @@ -35,7 +36,7 @@ describe("workspace-starter", function () {
},
},
};
const result = migrationIDESettings(user);
const result = ideService.migrateSettings(user);
expect(result).to.undefined;
});

Expand All @@ -51,7 +52,7 @@ describe("workspace-starter", function () {
},
},
};
const result = migrationIDESettings(user);
const result = ideService.migrateSettings(user);
expect(result?.defaultIde).to.equal("code");
expect(result?.useLatestVersion ?? false).to.be.true;
});
Expand All @@ -69,7 +70,7 @@ describe("workspace-starter", function () {
},
},
};
const result = migrationIDESettings(user);
const result = ideService.migrateSettings(user);
expect(result?.defaultIde).to.equal("code-desktop");
expect(result?.useLatestVersion ?? false).to.be.true;
});
Expand All @@ -87,7 +88,7 @@ describe("workspace-starter", function () {
},
},
};
const result = migrationIDESettings(user);
const result = ideService.migrateSettings(user);
expect(result?.defaultIde).to.equal("code-desktop");
expect(result?.useLatestVersion ?? false).to.be.false;
});
Expand All @@ -106,7 +107,7 @@ describe("workspace-starter", function () {
},
},
};
const result = migrationIDESettings(user);
const result = ideService.migrateSettings(user);
expect(result?.defaultIde).to.equal("intellij");
expect(result?.useLatestVersion ?? false).to.be.false;
});
Expand All @@ -125,7 +126,7 @@ describe("workspace-starter", function () {
},
},
};
const result = migrationIDESettings(user);
const result = ideService.migrateSettings(user);
expect(result?.defaultIde).to.equal("intellij");
expect(result?.useLatestVersion ?? false).to.be.true;
});
Expand All @@ -144,7 +145,7 @@ describe("workspace-starter", function () {
},
},
};
const result = migrationIDESettings(user);
const result = ideService.migrateSettings(user);
expect(result?.defaultIde).to.equal("code");
expect(result?.useLatestVersion ?? false).to.be.true;
});
Expand Down
38 changes: 6 additions & 32 deletions components/server/src/workspace/workspace-starter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import {
ImageConfigFile,
ProjectEnvVar,
ImageBuildLogInfo,
IDESettings,
WithReferrerContext,
EnvVarWithValue,
BillingTier,
Expand Down Expand Up @@ -137,32 +136,6 @@ export interface StartWorkspaceOptions {
const MAX_INSTANCE_START_RETRIES = 2;
const INSTANCE_START_RETRY_INTERVAL_SECONDS = 2;

export const migrationIDESettings = (user: User) => {
if (!user?.additionalData?.ideSettings || user.additionalData.ideSettings.settingVersion === "2.0") {
return;
}
const newIDESettings: IDESettings = {
settingVersion: "2.0",
};
const ideSettings = user.additionalData.ideSettings;
if (ideSettings.useDesktopIde) {
if (ideSettings.defaultDesktopIde === "code-desktop") {
newIDESettings.defaultIde = "code-desktop";
} else if (ideSettings.defaultDesktopIde === "code-desktop-insiders") {
newIDESettings.defaultIde = "code-desktop";
newIDESettings.useLatestVersion = true;
} else {
newIDESettings.defaultIde = ideSettings.defaultDesktopIde;
newIDESettings.useLatestVersion = ideSettings.useLatestVersion;
}
} else {
const useLatest = ideSettings.defaultIde === "code-latest";
newIDESettings.defaultIde = "code";
newIDESettings.useLatestVersion = useLatest;
}
return newIDESettings;
};

export async function getWorkspaceClassForInstance(
ctx: TraceContext,
workspace: Workspace,
Expand Down Expand Up @@ -297,6 +270,7 @@ export class WorkspaceStarter {
}

const ideConfig = await this.resolveIDEConfiguration(ctx, workspace, user);

// create and store instance
let instance = await this.workspaceDb
.trace({ span })
Expand Down Expand Up @@ -379,6 +353,11 @@ export class WorkspaceStarter {
private async resolveIDEConfiguration(ctx: TraceContext, workspace: Workspace, user: User) {
const span = TraceContext.startSpan("resolveIDEConfiguration", ctx);
try {
const migrated = this.ideService.migrateSettings(user);
if (user.additionalData?.ideSettings && migrated) {
user.additionalData.ideSettings = migrated;
}

const workspaceType =
workspace.type === "prebuild"
? IdeServiceApi.WorkspaceType.PREBUILD
Expand Down Expand Up @@ -804,11 +783,6 @@ export class WorkspaceStarter {
): Promise<WorkspaceInstance> {
const span = TraceContext.startSpan("newInstance", ctx);
try {
const migrated = migrationIDESettings(user);
if (user.additionalData?.ideSettings && migrated) {
user.additionalData.ideSettings = migrated;
}

const configuration: WorkspaceInstanceConfiguration = {
ideImage: ideConfig.webImage,
supervisorImage: ideConfig.supervisorImage,
Expand Down

0 comments on commit cebaf02

Please sign in to comment.