Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 60 additions & 23 deletions scripts/cleanupAtlasTestLeftovers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,39 @@ async function findAllTestProjects(client: ApiClient, orgId: string): Promise<Gr
return testProjects.filter((proj) => isOlderThanADay(proj.created));
}

async function deleteAllClustersOnStaleProject(client: ApiClient, projectId: string): Promise<void> {
const allClusters = await client
.listClusters({
params: {
path: {
groupId: projectId || "",
async function deleteAllClustersOnStaleProject(client: ApiClient, projectId: string): Promise<string[]> {
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<void> {
Expand All @@ -70,27 +87,47 @@ async function main(): Promise<void> {

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) {
console.warn(`Skipping project with missing ID: ${project.name}`);
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}`;
Comment on lines +110 to +111
Copy link

Copilot AI Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider extracting the error message formatting into a helper function since similar patterns are used in lines 63 and 67. This would reduce code duplication and improve consistency.

Copilot uses AI. Check for mistakes.
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", () => {
Expand Down
Loading