Skip to content

Commit eac0ef9

Browse files
authored
Add a length limitation for auto-generated workspace names (#1045)
* fix: an error message typo Signed-off-by: Oleksii Orel <oorel@redhat.com> * fix: generateWorkspaceName method Signed-off-by: Oleksii Orel <oorel@redhat.com> --------- Signed-off-by: Oleksii Orel <oorel@redhat.com>
1 parent 8cabbdd commit eac0ef9

File tree

6 files changed

+55
-9
lines changed

6 files changed

+55
-9
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2018-2024 Red Hat, Inc.
3+
* This program and the accompanying materials are made
4+
* available under the terms of the Eclipse Public License 2.0
5+
* which is available at https://www.eclipse.org/legal/epl-2.0/
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* Red Hat, Inc. - initial API and implementation
11+
*/
12+
13+
import { generateWorkspaceName, WORKSPACE_NAME_MAX_LENGTH } from '@/services/helpers/generateName';
14+
15+
describe('Get a new workspace name', () => {
16+
it('returns a workspace name which start from the generateName', () => {
17+
const generateName = 'project-demo';
18+
19+
const workspaceName = generateWorkspaceName(generateName);
20+
21+
expect(workspaceName.startsWith(generateName)).toBeTruthy();
22+
});
23+
24+
it('returns a workspace name with 5 char suffix', () => {
25+
const generateName = 'project-demo';
26+
27+
const workspaceName = generateWorkspaceName(generateName);
28+
29+
expect(workspaceName.length).toBe(generateName.length + 5);
30+
});
31+
32+
it('returns a workspace name less then WORKSPACE_NAME_MAX_LENGTH symbols', () => {
33+
const generateName = 'a'.repeat(WORKSPACE_NAME_MAX_LENGTH + 100);
34+
35+
const workspaceName = generateWorkspaceName(generateName);
36+
37+
expect(workspaceName.length).toBeLessThan(WORKSPACE_NAME_MAX_LENGTH);
38+
});
39+
});

packages/dashboard-frontend/src/services/helpers/__tests__/getProjectName.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
* Red Hat, Inc. - initial API and implementation
1111
*/
1212

13-
import { getProjectName } from '@/services/helpers/getProjectName';
13+
import { getProjectName, PROJECT_NAME_MAX_LENGTH } from '@/services/helpers/getProjectName';
1414

1515
describe('Get a project name based on location', () => {
16-
it('should return a valid name less then 63 symbols', () => {
16+
it('should return a valid name less then PROJECT_NAME_MAX_LENGTH symbols', () => {
1717
let cloneUrl = 'http://dummy/test.com/project-demo';
1818

1919
cloneUrl += 'a'.repeat(100);
2020
const projectName = getProjectName(cloneUrl);
2121

22-
expect(projectName).toEqual('project-demo' + 'a'.repeat(50));
22+
expect(projectName.length).toBeLessThan(PROJECT_NAME_MAX_LENGTH);
2323
});
2424

2525
it('should return a valid name which has the first char [a-z0-9]', () => {

packages/dashboard-frontend/src/services/helpers/generateName.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@
1212

1313
import getRandomString from '@/services/helpers/random';
1414

15+
export const WORKSPACE_NAME_MAX_LENGTH = 63;
16+
1517
export function generateWorkspaceName(generateName: string): string {
16-
return generateName + generateSuffix();
18+
const suffix = generateSuffix();
19+
20+
if (generateName.length + suffix.length > WORKSPACE_NAME_MAX_LENGTH) {
21+
generateName = generateName.substring(0, WORKSPACE_NAME_MAX_LENGTH - suffix.length - 1);
22+
}
23+
return generateName + suffix;
1724
}
1825

1926
export function generateSuffix(): string {

packages/dashboard-frontend/src/services/helpers/getProjectName.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Red Hat, Inc. - initial API and implementation
1111
*/
1212

13-
const PROJECT_NAME_MAX_LENGTH = 63;
13+
export const PROJECT_NAME_MAX_LENGTH = 63;
1414

1515
export function getProjectName(cloneUrl: string): string {
1616
let name = cloneUrl
@@ -22,7 +22,7 @@ export function getProjectName(cloneUrl: string): string {
2222
name = name.replace(/(^[-]+)/, '');
2323
name = name.replace(/([-]+$)/, '');
2424
if (name.length > PROJECT_NAME_MAX_LENGTH) {
25-
name = name.substr(0, PROJECT_NAME_MAX_LENGTH - 1);
25+
name = name.substring(0, PROJECT_NAME_MAX_LENGTH - 1);
2626
}
2727

2828
return name;

packages/dashboard-frontend/src/store/Workspaces/devWorkspaces/__tests__/editorImage.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe('Update editor image', () => {
104104
}
105105

106106
expect(errorMessage).toEqual(
107-
'Failed to update editor image. Editor components is not defined.',
107+
'Failed to update editor image. Editor components are not defined.',
108108
);
109109
});
110110

@@ -146,7 +146,7 @@ describe('Update editor image', () => {
146146
}
147147

148148
expect(errorMessage).toEqual(
149-
'Failed to update editor image. Editor components is not defined.',
149+
'Failed to update editor image. Editor components are not defined.',
150150
);
151151
});
152152

packages/dashboard-frontend/src/store/Workspaces/devWorkspaces/editorImage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function updateComponents(
8787
editorImage: string,
8888
): boolean {
8989
if (!components) {
90-
throw new Error('Editor components is not defined.');
90+
throw new Error('Editor components are not defined.');
9191
}
9292
let isUpdated = false;
9393
for (let i = 0; i < components.length; i++) {

0 commit comments

Comments
 (0)