Skip to content

Commit df0b217

Browse files
committed
fix: cleanup script should group errors
1 parent 7e80114 commit df0b217

File tree

1 file changed

+60
-23
lines changed

1 file changed

+60
-23
lines changed

scripts/cleanupAtlasTestLeftovers.test.ts

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,39 @@ async function findAllTestProjects(client: ApiClient, orgId: string): Promise<Gr
3535
return testProjects.filter((proj) => isOlderThanADay(proj.created));
3636
}
3737

38-
async function deleteAllClustersOnStaleProject(client: ApiClient, projectId: string): Promise<void> {
39-
const allClusters = await client
40-
.listClusters({
41-
params: {
42-
path: {
43-
groupId: projectId || "",
38+
async function deleteAllClustersOnStaleProject(client: ApiClient, projectId: string): Promise<string[]> {
39+
const errors: string[] = [];
40+
41+
try {
42+
const allClusters = await client
43+
.listClusters({
44+
params: {
45+
path: {
46+
groupId: projectId || "",
47+
},
4448
},
45-
},
46-
})
47-
.then((res) => res.results || []);
49+
})
50+
.then((res) => res.results || []);
4851

49-
await Promise.allSettled(
50-
allClusters.map((cluster) =>
51-
client.deleteCluster({ params: { path: { groupId: projectId || "", clusterName: cluster.name || "" } } })
52-
)
53-
);
52+
const results = await Promise.allSettled(
53+
allClusters.map((cluster) =>
54+
client.deleteCluster({
55+
params: { path: { groupId: projectId || "", clusterName: cluster.name || "" } },
56+
})
57+
)
58+
);
59+
60+
results.forEach((result, index) => {
61+
if (result.status === "rejected") {
62+
const clusterName = allClusters[index]?.name || "unknown";
63+
errors.push(`Failed to delete cluster ${clusterName} in project ${projectId}: ${result.reason}`);
64+
}
65+
});
66+
} catch (error) {
67+
errors.push(`Failed to list clusters for project ${projectId}: ${String(error)}`);
68+
}
69+
70+
return errors;
5471
}
5572

5673
async function main(): Promise<void> {
@@ -70,27 +87,47 @@ async function main(): Promise<void> {
7087

7188
if (testProjects.length === 0) {
7289
console.log("No stale test projects found for cleanup.");
90+
return;
7391
}
7492

93+
const allErrors: string[] = [];
94+
7595
for (const project of testProjects) {
7696
console.log(`Cleaning up project: ${project.name} (${project.id})`);
7797
if (!project.id) {
7898
console.warn(`Skipping project with missing ID: ${project.name}`);
7999
continue;
80100
}
81101

82-
await deleteAllClustersOnStaleProject(apiClient, project.id);
83-
await apiClient.deleteProject({
84-
params: {
85-
path: {
86-
groupId: project.id,
102+
// Try to delete all clusters first
103+
const clusterErrors = await deleteAllClustersOnStaleProject(apiClient, project.id);
104+
allErrors.push(...clusterErrors);
105+
106+
// Try to delete the project
107+
try {
108+
await apiClient.deleteProject({
109+
params: {
110+
path: {
111+
groupId: project.id,
112+
},
87113
},
88-
},
89-
});
90-
console.log(`Deleted project: ${project.name} (${project.id})`);
114+
});
115+
console.log(`Deleted project: ${project.name} (${project.id})`);
116+
} catch (error) {
117+
const errorStr = String(error);
118+
const errorMessage = `Failed to delete project ${project.name} (${project.id}): ${errorStr}`;
119+
console.error(errorMessage);
120+
allErrors.push(errorMessage);
121+
}
122+
}
123+
124+
// If there were any errors, throw with all accumulated errors
125+
if (allErrors.length > 0) {
126+
const errorSummary = `Cleanup completed with ${allErrors.length} error(s):\n${allErrors.map((err, i) => `${i + 1}. ${err}`).join("\n")}`;
127+
throw new Error(errorSummary);
91128
}
92129

93-
return;
130+
console.log("All stale test projects cleaned up successfully.");
94131
}
95132

96133
describe("Cleanup Atlas Test Leftovers", () => {

0 commit comments

Comments
 (0)