From df0b217e68ea6e223a08e8cd307db5d96ee189fd Mon Sep 17 00:00:00 2001 From: Bianca Lisle Date: Tue, 14 Oct 2025 11:50:40 +0100 Subject: [PATCH 1/3] fix: cleanup script should group errors --- scripts/cleanupAtlasTestLeftovers.test.ts | 83 ++++++++++++++++------- 1 file changed, 60 insertions(+), 23 deletions(-) diff --git a/scripts/cleanupAtlasTestLeftovers.test.ts b/scripts/cleanupAtlasTestLeftovers.test.ts index 24351c8b6..58cda0f74 100644 --- a/scripts/cleanupAtlasTestLeftovers.test.ts +++ b/scripts/cleanupAtlasTestLeftovers.test.ts @@ -35,22 +35,39 @@ async function findAllTestProjects(client: ApiClient, orgId: string): Promise isOlderThanADay(proj.created)); } -async function deleteAllClustersOnStaleProject(client: ApiClient, projectId: string): Promise { - const allClusters = await client - .listClusters({ - params: { - path: { - groupId: projectId || "", +async function deleteAllClustersOnStaleProject(client: ApiClient, projectId: string): Promise { + const errors: string[] = []; + + try { + const allClusters = await client + .listClusters({ + params: { + path: { + groupId: projectId || "", + }, }, - }, - }) - .then((res) => res.results || []); + }) + .then((res) => res.results || []); - await Promise.allSettled( - allClusters.map((cluster) => - client.deleteCluster({ params: { path: { groupId: projectId || "", clusterName: cluster.name || "" } } }) - ) - ); + const results = await Promise.allSettled( + allClusters.map((cluster) => + client.deleteCluster({ + params: { path: { groupId: projectId || "", clusterName: cluster.name || "" } }, + }) + ) + ); + + results.forEach((result, index) => { + if (result.status === "rejected") { + const clusterName = allClusters[index]?.name || "unknown"; + errors.push(`Failed to delete cluster ${clusterName} in project ${projectId}: ${result.reason}`); + } + }); + } catch (error) { + errors.push(`Failed to list clusters for project ${projectId}: ${String(error)}`); + } + + return errors; } async function main(): Promise { @@ -70,8 +87,11 @@ async function main(): Promise { if (testProjects.length === 0) { console.log("No stale test projects found for cleanup."); + return; } + const allErrors: string[] = []; + for (const project of testProjects) { console.log(`Cleaning up project: ${project.name} (${project.id})`); if (!project.id) { @@ -79,18 +99,35 @@ async function main(): Promise { continue; } - await deleteAllClustersOnStaleProject(apiClient, project.id); - await apiClient.deleteProject({ - params: { - path: { - groupId: project.id, + // Try to delete all clusters first + const clusterErrors = await deleteAllClustersOnStaleProject(apiClient, project.id); + allErrors.push(...clusterErrors); + + // Try to delete the project + try { + await apiClient.deleteProject({ + params: { + path: { + groupId: project.id, + }, }, - }, - }); - console.log(`Deleted project: ${project.name} (${project.id})`); + }); + console.log(`Deleted project: ${project.name} (${project.id})`); + } catch (error) { + const errorStr = String(error); + const errorMessage = `Failed to delete project ${project.name} (${project.id}): ${errorStr}`; + console.error(errorMessage); + allErrors.push(errorMessage); + } + } + + // If there were any errors, throw with all accumulated errors + if (allErrors.length > 0) { + const errorSummary = `Cleanup completed with ${allErrors.length} error(s):\n${allErrors.map((err, i) => `${i + 1}. ${err}`).join("\n")}`; + throw new Error(errorSummary); } - return; + console.log("All stale test projects cleaned up successfully."); } describe("Cleanup Atlas Test Leftovers", () => { From 72bd48a84cb8b0370a2a4bafbdb41c0e43c47fc6 Mon Sep 17 00:00:00 2001 From: Bianca Lisle Date: Tue, 14 Oct 2025 11:56:02 +0100 Subject: [PATCH 2/3] fix: update cleanup --- scripts/cleanupAtlasTestLeftovers.test.ts | 41 ++++++++++------------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/scripts/cleanupAtlasTestLeftovers.test.ts b/scripts/cleanupAtlasTestLeftovers.test.ts index 58cda0f74..d3a4613e0 100644 --- a/scripts/cleanupAtlasTestLeftovers.test.ts +++ b/scripts/cleanupAtlasTestLeftovers.test.ts @@ -38,34 +38,27 @@ async function findAllTestProjects(client: ApiClient, orgId: string): Promise { const errors: string[] = []; - try { - const allClusters = await client - .listClusters({ - params: { - path: { - groupId: projectId || "", - }, + const allClusters = await client + .listClusters({ + params: { + path: { + groupId: projectId || "", }, - }) - .then((res) => res.results || []); + }, + }) + .then((res) => res.results || []); - const results = await Promise.allSettled( - allClusters.map((cluster) => - client.deleteCluster({ + await Promise.allSettled( + allClusters.map(async (cluster) => { + try { + await client.deleteCluster({ params: { path: { groupId: projectId || "", clusterName: cluster.name || "" } }, - }) - ) - ); - - results.forEach((result, index) => { - if (result.status === "rejected") { - const clusterName = allClusters[index]?.name || "unknown"; - errors.push(`Failed to delete cluster ${clusterName} in project ${projectId}: ${result.reason}`); + }); + } catch (error) { + errors.push(`Failed to delete cluster ${cluster.name} in project ${projectId}: ${String(error)}`); } - }); - } catch (error) { - errors.push(`Failed to list clusters for project ${projectId}: ${String(error)}`); - } + }) + ); return errors; } From 9028cbed3ede0d1f41a82a62cd8297a5a320b816 Mon Sep 17 00:00:00 2001 From: Bianca Lisle Date: Tue, 14 Oct 2025 11:58:06 +0100 Subject: [PATCH 3/3] update --- scripts/cleanupAtlasTestLeftovers.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/cleanupAtlasTestLeftovers.test.ts b/scripts/cleanupAtlasTestLeftovers.test.ts index d3a4613e0..e7e4ffefb 100644 --- a/scripts/cleanupAtlasTestLeftovers.test.ts +++ b/scripts/cleanupAtlasTestLeftovers.test.ts @@ -114,9 +114,9 @@ async function main(): Promise { } } - // If there were any errors, throw with all accumulated errors if (allErrors.length > 0) { - const errorSummary = `Cleanup completed with ${allErrors.length} error(s):\n${allErrors.map((err, i) => `${i + 1}. ${err}`).join("\n")}`; + const errorList = allErrors.map((err, i) => `${i + 1}. ${err}`).join("\n"); + const errorSummary = `Cleanup completed with ${allErrors.length} error(s):\n${errorList}`; throw new Error(errorSummary); }