From 77fa552f87af7cb94a5a864c5b656129cd274a7f Mon Sep 17 00:00:00 2001 From: "James M. Greene" Date: Fri, 10 Sep 2021 15:48:30 -0500 Subject: [PATCH] Limit deployment environment cleanup per run (#21437) * Limit deployment environment cleanup per run to reduce the likelihood of exceeding the API rate limit * Remove the per_page size * Change wording --- script/remove-stale-staging-envs.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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 } }