-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(repo): Cleanup leftover users from the e2e instances (#2181)
* chore(repo): Cleanup leftover users from the e2e instances Playwright will not run the cleanup tasks on test failure, so it's possible that some temp users will not be deleted. * Create moody-radios-begin.md
- Loading branch information
1 parent
e2d67fc
commit 5f8b40e
Showing
5 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,2 @@ | ||
--- | ||
--- |
This file contains 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,42 @@ | ||
name: Nightly upstream tests | ||
on: | ||
workflow_dispatch: | ||
schedule: | ||
- cron: '0 5,17 * * *' | ||
|
||
jobs: | ||
integration-tests: | ||
name: Cleanup e2e instances | ||
runs-on: ${{ vars.RUNNER_NORMAL }} | ||
timeout-minutes: ${{ fromJSON(vars.TIMEOUT_MINUTES_NORMAL) }} | ||
|
||
steps: | ||
- name: Checkout Repo | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
show-progress: false | ||
|
||
- name: Setup | ||
id: config | ||
uses: ./.github/actions/init | ||
with: | ||
turbo-signature: ${{ secrets.TURBO_REMOTE_CACHE_SIGNATURE_KEY }} | ||
turbo-team: ${{ vars.TURBO_TEAM }} | ||
turbo-token: ${{ secrets.TURBO_TOKEN }} | ||
playwright-enabled: true | ||
|
||
- name: Verdaccio | ||
uses: ./.github/actions/verdaccio | ||
with: | ||
publish-cmd: | | ||
if [ "$(npm config get registry)" = "https://registry.npmjs.org/" ]; then echo 'Error: Using default registry' && exit 1; else npx turbo build $TURBO_ARGS && npx changeset publish --no-git-tag; fi | ||
- name: Install @clerk/backend in /integration | ||
working-directory: ./integration | ||
run: npm init -y && npm install @clerk/backend | ||
|
||
- name: Run cleanup | ||
run: npm run test:integration:cleanup | ||
env: | ||
INTEGRATION_INSTANCE_KEYS: ${{ secrets.INTEGRATION_INSTANCE_KEYS }} |
This file contains 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,58 @@ | ||
import type { User } from '@clerk/backend'; | ||
import { Clerk } from '@clerk/backend'; | ||
import { test as setup } from '@playwright/test'; | ||
|
||
import { appConfigs } from '../presets/'; | ||
|
||
setup('cleanup instances ', async () => { | ||
const secretKeys = Object.values(appConfigs.envs) | ||
.map(e => e.toJson()) | ||
.map(json => json.private) | ||
.map(keys => keys['CLERK_SECRET_KEY']) | ||
.filter(Boolean); | ||
|
||
for (const secretKey of secretKeys) { | ||
console.log(`Cleanup for ${secretKey.replace(/(sk_test_)(.+)(...)/, '$1***$3')}`); | ||
const clerkClient = Clerk({ secretKey }); | ||
const { data: users, errors } = await clerkClient.users.getUserList({ | ||
orderBy: '-created_at', | ||
query: 'clerkcookie', | ||
limit: 100, | ||
}); | ||
|
||
if (errors) { | ||
console.log(errors); | ||
return; | ||
} | ||
|
||
const batches = batchElements(skipUsersThatWereCreatedToday(users), 5); | ||
for (const batch of batches) { | ||
console.log(`Starting batch...`); | ||
await Promise.all( | ||
batch.map(user => { | ||
console.log( | ||
`Cleaning up user ${user.id} (${user.emailAddresses[0]?.emailAddress}) (${new Date( | ||
user.createdAt, | ||
).toISOString()})`, | ||
); | ||
return clerkClient.users.deleteUser(user.id); | ||
}), | ||
); | ||
await new Promise(r => setTimeout(r, 1000)); | ||
} | ||
} | ||
}); | ||
|
||
const skipUsersThatWereCreatedToday = (users: User[]): User[] => { | ||
const today = new Date(); | ||
const todayString = today.toISOString().slice(0, 10); | ||
return users.filter(user => new Date(user.createdAt).toISOString().slice(0, 10) !== todayString); | ||
}; | ||
|
||
function batchElements<T>(users: T[], batchSize = 5): T[][] { | ||
const batches = []; | ||
for (let i = 0; i < users.length; i += batchSize) { | ||
batches.push(users.slice(i, i + batchSize)); | ||
} | ||
return batches; | ||
} |
This file contains 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,18 @@ | ||
import { defineConfig } from '@playwright/test'; | ||
import { config } from 'dotenv'; | ||
import * as path from 'path'; | ||
|
||
import { common } from './playwright.config'; | ||
|
||
config({ path: path.resolve(__dirname, '.env.local') }); | ||
|
||
export default defineConfig({ | ||
...common, | ||
testDir: './cleanup', | ||
projects: [ | ||
{ | ||
name: 'setup', | ||
testMatch: /cleanup\.setup/, | ||
}, | ||
], | ||
}); |
This file contains 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