Skip to content

Commit 6ee6353

Browse files
authored
fix: cleanup script should group errors (#649)
1 parent 7e80114 commit 6ee6353

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

scripts/cleanupAtlasTestLeftovers.test.ts

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ 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> {
38+
async function deleteAllClustersOnStaleProject(client: ApiClient, projectId: string): Promise<string[]> {
39+
const errors: string[] = [];
40+
3941
const allClusters = await client
4042
.listClusters({
4143
params: {
@@ -47,10 +49,18 @@ async function deleteAllClustersOnStaleProject(client: ApiClient, projectId: str
4749
.then((res) => res.results || []);
4850

4951
await Promise.allSettled(
50-
allClusters.map((cluster) =>
51-
client.deleteCluster({ params: { path: { groupId: projectId || "", clusterName: cluster.name || "" } } })
52-
)
52+
allClusters.map(async (cluster) => {
53+
try {
54+
await client.deleteCluster({
55+
params: { path: { groupId: projectId || "", clusterName: cluster.name || "" } },
56+
});
57+
} catch (error) {
58+
errors.push(`Failed to delete cluster ${cluster.name} in project ${projectId}: ${String(error)}`);
59+
}
60+
})
5361
);
62+
63+
return errors;
5464
}
5565

5666
async function main(): Promise<void> {
@@ -70,27 +80,47 @@ async function main(): Promise<void> {
7080

7181
if (testProjects.length === 0) {
7282
console.log("No stale test projects found for cleanup.");
83+
return;
7384
}
7485

86+
const allErrors: string[] = [];
87+
7588
for (const project of testProjects) {
7689
console.log(`Cleaning up project: ${project.name} (${project.id})`);
7790
if (!project.id) {
7891
console.warn(`Skipping project with missing ID: ${project.name}`);
7992
continue;
8093
}
8194

82-
await deleteAllClustersOnStaleProject(apiClient, project.id);
83-
await apiClient.deleteProject({
84-
params: {
85-
path: {
86-
groupId: project.id,
95+
// Try to delete all clusters first
96+
const clusterErrors = await deleteAllClustersOnStaleProject(apiClient, project.id);
97+
allErrors.push(...clusterErrors);
98+
99+
// Try to delete the project
100+
try {
101+
await apiClient.deleteProject({
102+
params: {
103+
path: {
104+
groupId: project.id,
105+
},
87106
},
88-
},
89-
});
90-
console.log(`Deleted project: ${project.name} (${project.id})`);
107+
});
108+
console.log(`Deleted project: ${project.name} (${project.id})`);
109+
} catch (error) {
110+
const errorStr = String(error);
111+
const errorMessage = `Failed to delete project ${project.name} (${project.id}): ${errorStr}`;
112+
console.error(errorMessage);
113+
allErrors.push(errorMessage);
114+
}
115+
}
116+
117+
if (allErrors.length > 0) {
118+
const errorList = allErrors.map((err, i) => `${i + 1}. ${err}`).join("\n");
119+
const errorSummary = `Cleanup completed with ${allErrors.length} error(s):\n${errorList}`;
120+
throw new Error(errorSummary);
91121
}
92122

93-
return;
123+
console.log("All stale test projects cleaned up successfully.");
94124
}
95125

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

0 commit comments

Comments
 (0)