diff --git a/.github/scripts/cleanup-test-indexes.js b/.github/scripts/cleanup-test-indexes.js new file mode 100644 index 0000000..7bc0fe8 --- /dev/null +++ b/.github/scripts/cleanup-test-indexes.js @@ -0,0 +1,45 @@ +const { algoliasearch } = require('algoliasearch'); + +const BATCH_SIZE = 20; + +async function main() { + const client = algoliasearch(process.env.APP_ID, process.env.API_KEY); + + const { items } = await client.listIndices(); + const names = items.map((i) => i.name).filter((n) => n.includes('sf_phpunit_')); + + if (names.length === 0) { + console.log('Nothing to delete.'); + return; + } + + console.log(`Deleting ${names.length} indexes in batches of ${BATCH_SIZE}...`); + + const tasks = []; + for (let i = 0; i < names.length; i += BATCH_SIZE) { + const batch = names.slice(i, i + BATCH_SIZE); + const batchTasks = await Promise.all( + batch.map(async (name) => { + const { taskID } = await client.deleteIndex({ indexName: name }); + return { name, taskID }; + }) + ); + tasks.push(...batchTasks); + console.log(` requested ${Math.min(i + BATCH_SIZE, names.length)}/${names.length}`); + } + + console.log('Waiting for all tasks to complete...'); + await Promise.all( + tasks.map(async ({ name, taskID }) => { + await client.waitForTask({ indexName: name, taskID }); + console.log(` confirmed ${name}`); + }) + ); + + console.log('All deletes confirmed.'); +} + +main().catch((err) => { + console.error(err); + process.exit(1); +}); diff --git a/.github/scripts/package.json b/.github/scripts/package.json new file mode 100644 index 0000000..3f7574b --- /dev/null +++ b/.github/scripts/package.json @@ -0,0 +1,10 @@ +{ + "name": "search-bundle-ci-scripts", + "private": true, + "dependencies": { + "algoliasearch": "^5" + }, + "devDependencies": { + "@types/node": "^24" + } +} diff --git a/.github/workflows/cleanup-test-indexes.yml b/.github/workflows/cleanup-test-indexes.yml new file mode 100644 index 0000000..83ce09d --- /dev/null +++ b/.github/workflows/cleanup-test-indexes.yml @@ -0,0 +1,21 @@ +name: Cleanup test indexes + +on: + schedule: + - cron: '0 3 * * 0' + workflow_dispatch: + +jobs: + cleanup: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: '24' + - run: npm install algoliasearch@^5 --no-save + - name: Delete sf_phpunit_* indexes + env: + APP_ID: ${{ secrets.ALGOLIA_APP_ID }} + API_KEY: ${{ secrets.ALGOLIA_API_KEY }} + run: node .github/scripts/cleanup-test-indexes.js diff --git a/.gitignore b/.gitignore index b8e453c..88c1b7a 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ clover.xml /tests/QualityTools/composer.lock .phpunit.result.cache phpunit.xml.dist.bak +/.github/scripts/node_modules/ +/.github/scripts/package-lock.json