Skip to content

[server] use owner and repo name for workspace id #7390

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

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,19 @@ const expect = chai.expect
expect(longestName.length <= 36, `"${longestName}" is longer than 36 chars (${longestName.length})`).to.be.true;
}

@test public async testCustomName() {
const data = [
['foo','bar','foo-bar-'],
['f','bar','.{2,16}-bar-'],
['gitpod-io','gitpod','gitpodio-gitpod-'],
['this is rather long and has some "§$"% special chars','bar','thisisratherlong-bar-'],
]
for (const d of data) {
const id = await generateWorkspaceID(d[0], d[1]);
expect(id).match(new RegExp("^"+d[2]));
expect(new GitpodHostUrl().withWorkspacePrefix(id, "eu").workspaceId).to.equal(id);
}
}

}
module.exports = new TestGenerateWorkspaceId()
21 changes: 19 additions & 2 deletions components/gitpod-protocol/src/util/generate-workspace-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,25 @@
*/
import randomNumber = require("random-number-csprng");

export async function generateWorkspaceID(): Promise<string> {
return (await random(colors))+'-'+(await random(animals))+'-'+(await random(characters, 8));
export async function generateWorkspaceID(firstSegment?: string, secondSegment?: string): Promise<string> {
const firstSeg = clean(firstSegment) || await random(colors);
const secSeg = clean(secondSegment) || await random(animals);
return firstSeg+'-'+secSeg+'-'+(await random(characters, 8));
}

function clean(segment?: string) {
if (segment) {
let result = '';
for (let i =0; i < segment.length; i++) {
if (characters.indexOf(segment[i]) !== -1) {
result += segment[i];
}
}
if (result.length >= 2) {
return result.substring(0, 16);
}
}
return undefined;
}

async function random(array: string[], length: number = 1): Promise<string> {
Expand Down
6 changes: 3 additions & 3 deletions components/gitpod-protocol/src/util/gitpod-host-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export interface UrlChange {
}
export type UrlUpdate = UrlChange | Partial<URL>;

const basewoWkspaceIDRegex = "(([a-f][0-9a-f]{7}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})|([0-9a-z]{2,16}-[0-9a-z]{2,16}-[0-9a-z]{8}))";
const baseWorkspaceIDRegex = "(([a-f][0-9a-f]{7}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})|([0-9a-z]{2,16}-[0-9a-z]{2,16}-[0-9a-z]{8}))";

// this pattern matches v4 UUIDs as well as the new generated workspace ids (e.g. pink-panda-ns35kd21)
const workspaceIDRegex = RegExp(`^${basewoWkspaceIDRegex}$`);
const workspaceIDRegex = RegExp(`^${baseWorkspaceIDRegex}$`);

// this pattern matches URL prefixes of workspaces
const workspaceUrlPrefixRegex = RegExp(`^([0-9]{4,6}-)?${basewoWkspaceIDRegex}\\.`);
const workspaceUrlPrefixRegex = RegExp(`^([0-9]{4,6}-)?${baseWorkspaceIDRegex}\\.`);

export class GitpodHostUrl {
readonly url: URL;
Expand Down
3 changes: 1 addition & 2 deletions components/server/ee/src/workspace/workspace-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { LicenseEvaluator } from '@gitpod/licensor/lib';
import { Feature } from '@gitpod/licensor/lib/api';
import { ResponseError } from 'vscode-jsonrpc';
import { ErrorCodes } from '@gitpod/gitpod-protocol/lib/messaging/error';
import { generateWorkspaceID } from '@gitpod/gitpod-protocol/lib/util/generate-workspace-id';
import { HostContextProvider } from '../../../src/auth/host-context-provider';
import { RepoURL } from '../../../src/repohost';

Expand Down Expand Up @@ -220,7 +219,7 @@ export class WorkspaceFactoryEE extends WorkspaceFactory {
}
}

const id = await generateWorkspaceID();
const id = await this.generateWorkspaceID(context);
const newWs: Workspace = {
id,
type: "regular",
Expand Down
21 changes: 17 additions & 4 deletions components/server/src/workspace/workspace-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
*/

import { DBWithTracing, TracedWorkspaceDB, WorkspaceDB, ProjectDB, TeamDB } from '@gitpod/gitpod-db/lib';
import { AdditionalContentContext, CommitContext, IssueContext, PullRequestContext, Repository, SnapshotContext, User, Workspace, WorkspaceConfig, WorkspaceContext, WorkspaceProbeContext } from '@gitpod/gitpod-protocol';
import { AdditionalContentContext, CommitContext, IssueContext, PrebuiltWorkspaceContext, PullRequestContext, Repository, SnapshotContext, User, Workspace, WorkspaceConfig, WorkspaceContext, WorkspaceProbeContext } from '@gitpod/gitpod-protocol';
import { ErrorCodes } from '@gitpod/gitpod-protocol/lib/messaging/error';
import { generateWorkspaceID } from '@gitpod/gitpod-protocol/lib/util/generate-workspace-id';
import { log } from '@gitpod/gitpod-protocol/lib/util/logging';
import { TraceContext } from '@gitpod/gitpod-protocol/lib/util/tracing';
import { inject, injectable } from 'inversify';
import { ResponseError } from 'vscode-jsonrpc';
import { RepoURL } from '../repohost';
import { ConfigProvider } from './config-provider';
import { ImageSourceProvider } from './image-source-provider';

Expand Down Expand Up @@ -55,7 +56,7 @@ export class WorkspaceFactory {
// Basically we're using the raw alpine image bait-and-switch style without adding the GP layer.
const imageSource = await this.imageSourceProvider.getImageSource(ctx, user, null as any, config);

const id = await generateWorkspaceID();
const id = await this.generateWorkspaceID(context);
const date = new Date().toISOString();
const newWs: Workspace = {
id,
Expand Down Expand Up @@ -94,7 +95,7 @@ export class WorkspaceFactory {
throw new Error(`The original workspace has been deleted - cannot open this snapshot.`);
}

const id = await generateWorkspaceID();
const id = await this.generateWorkspaceID(context);
const date = new Date().toISOString();
const newWs = <Workspace>{
id,
Expand Down Expand Up @@ -166,7 +167,7 @@ export class WorkspaceFactory {
}
}

const id = await generateWorkspaceID();
const id = await this.generateWorkspaceID(context);
const newWs: Workspace = {
id,
type: "regular",
Expand Down Expand Up @@ -207,4 +208,16 @@ export class WorkspaceFactory {
return context.title;
}

protected async generateWorkspaceID(context: WorkspaceContext): Promise<string> {
let ctx = context;
if (PrebuiltWorkspaceContext.is(context)) {
ctx = context.originalContext;
}
if (CommitContext.is(ctx)) {
const parsed = RepoURL.parseRepoUrl(ctx.repository.cloneUrl);
return await generateWorkspaceID(parsed?.owner, parsed?.repo);
}
return await generateWorkspaceID();
}

}