diff --git a/script/remove-stale-staging-envs.js b/script/remove-stale-staging-envs.js index 42ed2e7ba7..4ffe96eafd 100755 --- a/script/remove-stale-staging-envs.js +++ b/script/remove-stale-staging-envs.js @@ -29,6 +29,7 @@ if (!process.env.REPO) { const octokit = getOctokit() const protectedEnvNames = ['production'] +const maxEnvironmentsToProcess = 50 // How long must a PR be closed without being merged to be considered stale? const ONE_HOUR = 60 * 60 * 1000 @@ -48,6 +49,7 @@ async function main() { const prInfoMatch = /^(?:gha-|ghd-)?(?docs(?:-internal)?)-(?\d+)--.*$/ const legacyPrInfoMatch = /^help-docs-pr-(?\d+)$/ + let exceededLimit = false let matchingCount = 0 let staleCount = 0 let spammyCount = 0 @@ -81,7 +83,6 @@ async function main() { const envsWithPullIds = envsPlusPullIds.filter( (eppi) => eppi.repo === repo && eppi.pullNumber > 0 ) - matchingCount += envsWithPullIds.length nonMatchingEnvNames.push( ...envsPlusPullIds @@ -99,6 +100,20 @@ async function main() { if (isSpammy || isStale) { await deleteEnvironment(ewpi.env.name) } + + matchingCount += 1 + if (matchingCount >= maxEnvironmentsToProcess) { + exceededLimit = true + break + } + } + + if (exceededLimit) { + console.log( + '🛑', + chalk.bgRed(`STOP! Exceeded limit, halting after ${maxEnvironmentsToProcess}.`) + ) + break } }