Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 28 additions & 0 deletions .github/workflows/cleanup-atlas-env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: "Cleanup stale Atlas test environments"
on:
workflow_dispatch:
schedule:
- cron: "0 0 * * *"

permissions: {}

jobs:
cleanup-envs:
runs-on: ubuntu-latest
steps:
- uses: GitHubSecurityLab/actions-permissions/monitor@v1
if: matrix.os == 'ubuntu-latest'
- uses: actions/checkout@v5
- uses: actions/setup-node@v5
with:
node-version-file: package.json
cache: "npm"
- name: Install dependencies
run: npm ci
- name: Run cleanup script
env:
MDB_MCP_API_CLIENT_ID: ${{ secrets.TEST_ATLAS_CLIENT_ID }}
MDB_MCP_API_CLIENT_SECRET: ${{ secrets.TEST_ATLAS_CLIENT_SECRET }}
MDB_MCP_API_BASE_URL: ${{ vars.TEST_ATLAS_BASE_URL }}
run: npm test -- scripts/cleanupAtlasTestLeftovers.test.ts
File renamed without changes.
File renamed without changes.
100 changes: 100 additions & 0 deletions scripts/cleanupAtlasTestLeftovers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import type { Group, AtlasOrganization } from "../src/common/atlas/openapi.js";
import { ApiClient } from "../src/common/atlas/apiClient.js";
import { ConsoleLogger } from "../src/common/logger.js";
import { Keychain } from "../src/lib.js";
import { describe, it } from "vitest";

function isOlderThanADay(date: string): boolean {
const oneDayInMs = 24 * 60 * 60 * 1000;
const projectDate = new Date(date);
const currentDate = new Date();
return currentDate.getTime() - projectDate.getTime() > oneDayInMs;
}

async function findTestOrganization(client: ApiClient): Promise<AtlasOrganization> {
const orgs = await client.listOrganizations();
const testOrg = orgs?.results?.find((org) => org.name === "MongoDB MCP Test");

if (!testOrg) {
throw new Error('Test organization "MongoDB MCP Test" not found.');
}

return testOrg;
}

async function findAllTestProjects(client: ApiClient, orgId: string): Promise<Group[]> {
const projects = await client.listOrganizationProjects({
params: {
path: {
orgId,
},
},
});

const testProjects = projects?.results?.filter((proj) => proj.name.startsWith("testProj-")) || [];
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 || "",
},
},
})
.then((res) => res.results || []);

await Promise.allSettled(
allClusters.map((cluster) =>
client.deleteCluster({ params: { path: { groupId: projectId || "", clusterName: cluster.name || "" } } })
)
);
}

async function main(): Promise<void> {
const apiClient = new ApiClient(
{
baseUrl: process.env.TEST_ATLAS_BASE_URL || "https://cloud-dev.mongodb.com",

Check failure on line 59 in scripts/cleanupAtlasTestLeftovers.test.ts

View workflow job for this annotation

GitHub Actions / check-style

Delete `·`
credentials: {

Check failure on line 60 in scripts/cleanupAtlasTestLeftovers.test.ts

View workflow job for this annotation

GitHub Actions / check-style

Delete `·`
clientId: process.env.MDB_MCP_API_CLIENT_ID || "",
clientSecret: process.env.MDB_MCP_API_CLIENT_SECRET || "",
},
},
new ConsoleLogger(Keychain.root)
);

const testOrg = await findTestOrganization(apiClient);
const testProjects = await findAllTestProjects(apiClient, testOrg.id || "");

if (testProjects.length === 0) {
console.log("No stale test projects found for cleanup.");
}

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,
},
},
});
console.log(`Deleted project: ${project.name} (${project.id})`);
}

return;
}

describe("Cleanup Atlas Test Leftovers", () => {
it("should clean up stale test projects", async () => {
await main();
});
});
Loading