Skip to content

Commit 7ba0e6c

Browse files
svenefftingeroboquat
authored andcommitted
[protocol] fix generate id for bad repo names
1 parent 6bc0571 commit 7ba0e6c

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

components/gitpod-protocol/src/util/generate-workspace-id.spec.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const expect = chai.expect;
1414
@suite
1515
class TestGenerateWorkspaceId {
1616
@test public async testGenerateWorkspaceId() {
17-
for (let i = 0; i < 100; i++) {
17+
for (let i = 0; i < 10; i++) {
1818
const id = await generateWorkspaceID();
1919
expect(new GitpodHostUrl().withWorkspacePrefix(id, "eu").workspaceId).to.equal(id);
2020
}
@@ -32,13 +32,20 @@ class TestGenerateWorkspaceId {
3232
["foo", "bar", "foo-bar-"],
3333
["f", "bar", ".{2,16}-bar-"],
3434
["gitpod-io", "gitpod", "gitpodio-gitpod-"],
35+
["breatheco-de", "python-flask-api-tutorial", "breathecode-pythonflask-"],
36+
["short", "muchlongerthaneleven", "short-muchlongerthanel-"],
37+
["muchlongerthaneleven", "short", "muchlongerthanel-short-"],
3538
[
3639
'this is rather long and has some "§$"% special chars',
3740
"also here pretty long and needs abbreviation",
38-
"thisisratherlon-alsohere-",
41+
"thisisrathe-alsoherepre-",
3942
],
40-
["breatheco-de", "python-flask-api-tutorial", "breathecode-pythonflaska-"],
4143
["UPPER", "CaSe", "upper-case-"],
44+
[
45+
"superlongfirstsegment",
46+
"---------",
47+
"superlong" /* we don't mantch for the whole first segment, because it has different length depending on the animal that is used to replace the -------*/,
48+
],
4249
];
4350
for (const d of data) {
4451
const id = await generateWorkspaceID(d[0], d[1]);

components/gitpod-protocol/src/util/generate-workspace-id.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
* Licensed under the GNU Affero General Public License (AGPL).
44
* See License-AGPL.txt in the project root for license information.
55
*/
6+
67
import randomNumber = require("random-number-csprng");
78

89
export async function generateWorkspaceID(firstSegment?: string, secondSegment?: string): Promise<string> {
910
const firstSeg = clean(firstSegment) || (await random(colors));
10-
const secSeg = clean(secondSegment, Math.min(15, 23 - firstSeg.length)) || (await random(animals));
11-
return firstSeg + "-" + secSeg + "-" + (await random(characters, 11));
11+
const secSeg = clean(secondSegment) || (await random(animals));
12+
function fit(makeFit: string, otherSeg: string) {
13+
return makeFit.substring(0, Math.max(segLength, 2 * segLength - otherSeg.length));
14+
}
15+
return fit(firstSeg, secSeg) + "-" + fit(secSeg, firstSeg) + "-" + (await random(characters, segLength));
1216
}
1317

14-
function clean(segment: string | undefined, maxChars: number = 15) {
18+
function clean(segment: string | undefined, maxChars: number = 16) {
1519
if (!segment) {
1620
return undefined;
1721
}
@@ -35,6 +39,8 @@ async function random(array: string[], length: number = 1): Promise<string> {
3539
return result;
3640
}
3741

42+
const segLength = 11;
43+
3844
const characters = "abcdefghijklmnopqrstuvwxyz0123456789".split("");
3945

4046
export const colors = [

0 commit comments

Comments
 (0)