@eclipse-che/che-e2e 7.121.0-next-288e5c5 → 7.121.0-next-0ae5289

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /** *******************************************************************
2
- * copyright (c) 2019-2023 Red Hat, Inc.
2
+ * copyright (c) 2019-2026 Red Hat, Inc.
3
3
  *
4
4
  * This program and the accompanying materials are made
5
5
  * available under the terms of the Eclipse Public License 2.0
@@ -26,7 +26,8 @@ export class ApiUrlResolver {
26
26
  ) {}
27
27
 
28
28
  async getWorkspaceApiUrl(workspaceName: string): Promise<string> {
29
- return `${await this.getWorkspacesApiUrl()}/${workspaceName}`;
29
+ const actualWorkspaceName: string = await this.resolveWorkspaceName(workspaceName);
30
+ return `${await this.getWorkspacesApiUrl()}/${actualWorkspaceName}`;
30
31
  }
31
32
 
32
33
  async getWorkspacesApiUrl(): Promise<string> {
@@ -34,6 +35,66 @@ export class ApiUrlResolver {
34
35
  return `${ApiUrlResolver.DASHBOARD_API_URL}/${namespace}/devworkspaces`;
35
36
  }
36
37
 
38
+ /**
39
+ * resolves the actual DevWorkspace name from the API.
40
+ * If the exact workspace name exists, returns it as is.
41
+ * If not found, searches for a workspace whose name matches the pattern: workspaceName + '-' + random suffix
42
+ * (to handle cases where DevWorkspace has a random suffix like '-4fnq' after backup/restore).
43
+ * @param workspaceName - The workspace name to search for
44
+ * @returns The actual DevWorkspace name from the API
45
+ * @throws Error if no matching workspace is found
46
+ */
47
+ private async resolveWorkspaceName(workspaceName: string): Promise<string> {
48
+ Logger.debug(`Resolving workspace name: ${workspaceName}`);
49
+
50
+ try {
51
+ // first, try to get the workspace directly by the provided name
52
+ const directUrl: string = `${await this.getWorkspacesApiUrl()}/${workspaceName}`;
53
+ const directResponse: AxiosResponse = await this.processRequestHandler.get(directUrl);
54
+ if (directResponse.status === 200) {
55
+ Logger.debug(`Found exact match: ${workspaceName}`);
56
+ return workspaceName;
57
+ }
58
+ } catch (error) {
59
+ // workspace not found by exact name, will search by prefix with suffix pattern
60
+ Logger.debug(`Exact match not found for ${workspaceName}, searching by prefix with suffix`);
61
+ }
62
+
63
+ // if exact match not found, get all workspaces and search by prefix + dash + suffix pattern
64
+ const allWorkspacesResponse: AxiosResponse = await this.processRequestHandler.get(await this.getWorkspacesApiUrl());
65
+ if (allWorkspacesResponse.status !== 200) {
66
+ throw new Error(`Cannot get workspaces list. Code: ${allWorkspacesResponse.status} Data: ${allWorkspacesResponse.data}`);
67
+ }
68
+
69
+ const workspaces: Array<{ metadata: { name: string } }> = allWorkspacesResponse.data.items || [];
70
+ // look for workspace with pattern: workspaceName + '-' + suffix (e.g., 'test-workspace-2-4fnq')
71
+ // this ensures we don't match 'test-workspace-20' when looking for 'test-workspace-2'
72
+ const matchingWorkspaces: Array<{ metadata: { name: string } }> = workspaces.filter((ws): boolean => {
73
+ const dwName: string = ws.metadata.name;
74
+ // check if name starts with workspaceName followed by a dash
75
+ if (dwName.startsWith(workspaceName + '-')) {
76
+ // verify that what follows the dash looks like a random suffix (lowercase letters/numbers)
77
+ const suffix: string = dwName.substring(workspaceName.length + 1);
78
+ return suffix.length > 0 && /^[a-z0-9]+$/.test(suffix);
79
+ }
80
+ return false;
81
+ });
82
+
83
+ if (matchingWorkspaces.length === 1) {
84
+ Logger.debug(`Found workspace by prefix: ${matchingWorkspaces[0].metadata.name} (requested: ${workspaceName})`);
85
+ return matchingWorkspaces[0].metadata.name;
86
+ }
87
+
88
+ if (matchingWorkspaces.length > 1) {
89
+ const names: string = matchingWorkspaces.map((ws): string => ws.metadata.name).join(', ');
90
+ throw new Error(
91
+ `Multiple workspaces found matching '${workspaceName}': ${names}. Please use exact DevWorkspace name or delete duplicates.`
92
+ );
93
+ }
94
+
95
+ throw new Error(`Workspace not found: ${workspaceName} (tried exact match and prefix search)`);
96
+ }
97
+
37
98
  private async obtainUserNamespace(): Promise<string> {
38
99
  Logger.debug(`${this.userNamespace}`);
39
100
  if (this.userNamespace.length === 0) {