Skip to content

Commit 54583d4

Browse files
authored
Stale environment cleanup revisions (github#21508)
* Remove legacy environment name format * Ensure we handle multiple pages of Deployments * Track count of Deployments deleted per Environment
1 parent 3bf2b8c commit 54583d4

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

script/remove-stale-staging-envs.js

+41-39
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ async function main() {
4747
}
4848

4949
const prInfoMatch = /^(?:gha-|ghd-)?(?<repo>docs(?:-internal)?)-(?<pullNumber>\d+)--.*$/
50-
const legacyPrInfoMatch = /^help-docs-pr-(?<pullNumber>\d+)$/
5150

5251
let exceededLimit = false
5352
let matchingCount = 0
@@ -62,16 +61,8 @@ async function main() {
6261
const { data: environments } = response
6362

6463
const envsPlusPullIds = environments.map((env) => {
65-
let match = prInfoMatch.exec(env.name)
66-
let { repo: repoName, pullNumber } = (match || {}).groups || {}
67-
68-
if (!match) {
69-
match = legacyPrInfoMatch.exec(env.name)
70-
if (match) {
71-
repoName = repo
72-
pullNumber = ((match || {}).groups || {}).pullNumber
73-
}
74-
}
64+
const match = prInfoMatch.exec(env.name)
65+
const { repo: repoName, pullNumber } = (match || {}).groups || {}
7566

7667
return {
7768
env,
@@ -184,39 +175,45 @@ async function main() {
184175

185176
async function deleteEnvironment(envName) {
186177
try {
187-
// Get the latest deployment environment to signal its deactivation
188-
const { data: deployments } = await octokit.repos.listDeployments({
178+
let deploymentCount = 0
179+
180+
// Get all of the Deployments to signal this environment's complete deactivation
181+
for await (const response of octokit.paginate.iterator(octokit.repos.listDeployments, {
189182
owner,
190183
repo,
191184

192185
// In the GitHub API, there can only be one active deployment per environment.
193186
// For our many staging apps, we must use the unique appName as the environment.
194187
environment: envName,
195-
})
196-
197-
// Deactivate ALL of the deployments
198-
for (const deployment of deployments) {
199-
// Deactivate this Deployment with an 'inactive' DeploymentStatus
200-
await octokit.repos.createDeploymentStatus({
201-
owner,
202-
repo,
203-
deployment_id: deployment.id,
204-
state: 'inactive',
205-
description: 'The app was undeployed',
206-
// The 'ant-man' preview is required for `state` values of 'inactive', as well as
207-
// the use of the `log_url`, `environment_url`, and `auto_inactive` parameters.
208-
// The 'flash' preview is required for `state` values of 'in_progress' and 'queued'.
209-
mediaType: {
210-
previews: ['ant-man', 'flash'],
211-
},
212-
})
213-
214-
// Delete this Deployment
215-
await octokit.repos.deleteDeployment({
216-
owner,
217-
repo,
218-
deployment_id: deployment.id,
219-
})
188+
})) {
189+
const { data: deployments } = response
190+
191+
// Deactivate ALL of the deployments
192+
for (const deployment of deployments) {
193+
// Deactivate this Deployment with an 'inactive' DeploymentStatus
194+
await octokit.repos.createDeploymentStatus({
195+
owner,
196+
repo,
197+
deployment_id: deployment.id,
198+
state: 'inactive',
199+
description: 'The app was undeployed',
200+
// The 'ant-man' preview is required for `state` values of 'inactive', as well as
201+
// the use of the `log_url`, `environment_url`, and `auto_inactive` parameters.
202+
// The 'flash' preview is required for `state` values of 'in_progress' and 'queued'.
203+
mediaType: {
204+
previews: ['ant-man', 'flash'],
205+
},
206+
})
207+
208+
// Delete this Deployment
209+
await octokit.repos.deleteDeployment({
210+
owner,
211+
repo,
212+
deployment_id: deployment.id,
213+
})
214+
215+
deploymentCount++
216+
}
220217
}
221218

222219
// Delete this Environment
@@ -232,7 +229,12 @@ async function main() {
232229
}
233230
}
234231

235-
console.log('✅', chalk.green(`Removed stale deployment environment "${envName}"`))
232+
console.log(
233+
'✅',
234+
chalk.green(
235+
`Removed stale deployment environment "${envName}" (${deploymentCount} deployments)`
236+
)
237+
)
236238
} catch (error) {
237239
console.log(
238240
'❌',

0 commit comments

Comments
 (0)