-
Notifications
You must be signed in to change notification settings - Fork 128
chore(ci): add cleanup script on CI for atlas envs #608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f6ab263
chore: add cleanup script on CI for atlas envs
kmruiz 0b59b3d
Merge branch 'main' into chore/atlas-cleanup
kmruiz e42ffd6
Update scripts/cleanupAtlasTestLeftovers.test.ts
kmruiz e3005b9
Update .github/workflows/cleanup-atlas-env.yml
kmruiz 1dd48a4
chore: make sure vitest does not run scripts unexpectedly
kmruiz 9f4eab1
chore: apply PR fixes
kmruiz 6bb0921
chore: fix env var ref
kmruiz 5ca4358
chore: formatting
kmruiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} | ||
kmruiz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
run: npm test -- scripts/cleanupAtlasTestLeftovers.test.ts |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
kmruiz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
credentials: { | ||
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(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.