Skip to content
Merged
Changes from all commits
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
56 changes: 43 additions & 13 deletions scripts/cleanupAtlasTestLeftovers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ 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> {
async function deleteAllClustersOnStaleProject(client: ApiClient, projectId: string): Promise<string[]> {
const errors: string[] = [];

const allClusters = await client
.listClusters({
params: {
Expand All @@ -47,10 +49,18 @@ async function deleteAllClustersOnStaleProject(client: ApiClient, projectId: str
.then((res) => res.results || []);

await Promise.allSettled(
allClusters.map((cluster) =>
client.deleteCluster({ params: { path: { groupId: projectId || "", clusterName: cluster.name || "" } } })
)
allClusters.map(async (cluster) => {
try {
await client.deleteCluster({
params: { path: { groupId: projectId || "", clusterName: cluster.name || "" } },
});
} catch (error) {
errors.push(`Failed to delete cluster ${cluster.name} in project ${projectId}: ${String(error)}`);
}
})
);

return errors;
}

async function main(): Promise<void> {
Expand All @@ -70,27 +80,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 (allErrors.length > 0) {
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);
}

return;
console.log("All stale test projects cleaned up successfully.");
}

describe("Cleanup Atlas Test Leftovers", () => {
Expand Down
Loading