forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-stale-staging-envs.js
executable file
·247 lines (208 loc) · 6.79 KB
/
remove-stale-staging-envs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env node
// [start-readme]
//
// This script removes all stale GitHub deployment environments that outlasted
// the closure of their corresponding pull requests, or correspond to spammy
// pull requests.
//
// [end-readme]
import dotenv from 'dotenv'
import chalk from 'chalk'
import getOctokit from './helpers/github.js'
dotenv.config()
// Check for required GitHub PAT
if (!process.env.GITHUB_TOKEN) {
console.error('Error! You must have a GITHUB_TOKEN environment variable for repo access.')
process.exit(1)
}
if (!process.env.REPO) {
console.error('Error! You must have a REPO environment variable.')
process.exit(1)
}
// This helper uses the `GITHUB_TOKEN` implicitly
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
const prClosureStaleTime = 2 * ONE_HOUR
main()
async function main() {
const owner = 'github'
const [repoOwner, repo] = (process.env.REPO || '').split('/')
if (repoOwner !== owner) {
console.error(`Error! The repository owner must be "${owner}" but was "${repoOwner}".`)
process.exit(1)
}
const prInfoMatch = /^(?:gha-|ghd-)?(?<repo>docs(?:-internal)?)-(?<pullNumber>\d+)--.*$/
let exceededLimit = false
let matchingCount = 0
let staleCount = 0
let spammyCount = 0
const nonMatchingEnvNames = []
for await (const response of octokit.paginate.iterator(octokit.repos.getAllEnvironments, {
owner,
repo,
})) {
const { data: environments } = response
const envsPlusPullIds = environments.map((env) => {
const match = prInfoMatch.exec(env.name)
const { repo: repoName, pullNumber } = (match || {}).groups || {}
return {
env,
repo: repoName,
pullNumber: parseInt(pullNumber, 10) || null,
}
})
const envsWithPullIds = envsPlusPullIds.filter(
(eppi) => eppi.repo === repo && eppi.pullNumber > 0
)
matchingCount += envsWithPullIds.length
nonMatchingEnvNames.push(
...envsPlusPullIds
.filter((eppi) => !(eppi.repo && eppi.pullNumber > 0))
.map((eppi) => eppi.env.name)
.filter((name) => !protectedEnvNames.includes(name))
)
for (const ewpi of envsWithPullIds) {
const { isStale, isSpammy } = await assessPullRequest(ewpi.pullNumber)
if (isSpammy) spammyCount++
if (isStale) staleCount++
if (isSpammy || isStale) {
await deleteEnvironment(ewpi.env.name)
}
if (spammyCount + staleCount >= maxEnvironmentsToProcess) {
exceededLimit = true
break
}
}
if (exceededLimit) {
console.log(
'🛑',
chalk.bgRed(`STOP! Exceeded limit, halting after ${maxEnvironmentsToProcess}.`)
)
break
}
}
const counts = {
total: matchingCount,
alive: matchingCount - staleCount,
stale: {
total: staleCount,
spammy: spammyCount,
closed: staleCount - spammyCount,
},
}
console.log(`🧮 COUNTS!\n${JSON.stringify(counts, null, 2)}`)
const nonMatchingCount = nonMatchingEnvNames.length
if (nonMatchingCount > 0) {
console.log(
'⚠️ 👀',
chalk.yellow(
`Non-matching env names (${nonMatchingCount}):\n - ${nonMatchingEnvNames.join('\n - ')}`
)
)
}
function displayParams(params) {
const { owner, repo, pull_number: pullNumber } = params
return `${owner}/${repo}#${pullNumber}`
}
async function assessPullRequest(pullNumber) {
const params = {
owner,
repo,
pull_number: pullNumber,
}
let isStale = false
let isSpammy = false
try {
const { data: pullRequest } = await octokit.pulls.get(params)
if (pullRequest && pullRequest.state === 'closed') {
const isMerged = pullRequest.merged === true
const closureAge = Date.now() - Date.parse(pullRequest.closed_at)
isStale = isMerged || closureAge >= prClosureStaleTime
if (isStale) {
console.debug(chalk.green(`STALE: ${displayParams(params)} is closed`))
} else {
console.debug(
chalk.blue(`NOT STALE: ${displayParams(params)} is closed but not yet stale`)
)
}
}
} catch (error) {
// Using a standard GitHub PAT, PRs from spammy users will respond as 404
if (error.status === 404) {
isStale = true
isSpammy = true
console.debug(chalk.yellow(`STALE: ${displayParams(params)} is spammy or deleted`))
} else {
console.debug(chalk.red(`ERROR: ${displayParams(params)} - ${error.message}`))
}
}
return { isStale, isSpammy }
}
async function deleteEnvironment(envName) {
try {
let deploymentCount = 0
// Get all of the Deployments to signal this environment's complete deactivation
for await (const response of octokit.paginate.iterator(octokit.repos.listDeployments, {
owner,
repo,
// In the GitHub API, there can only be one active deployment per environment.
// For our many staging apps, we must use the unique appName as the environment.
environment: envName,
})) {
const { data: deployments } = response
// Deactivate ALL of the deployments
for (const deployment of deployments) {
// Deactivate this Deployment with an 'inactive' DeploymentStatus
await octokit.repos.createDeploymentStatus({
owner,
repo,
deployment_id: deployment.id,
state: 'inactive',
description: 'The app was undeployed',
// The 'ant-man' preview is required for `state` values of 'inactive', as well as
// the use of the `log_url`, `environment_url`, and `auto_inactive` parameters.
// The 'flash' preview is required for `state` values of 'in_progress' and 'queued'.
mediaType: {
previews: ['ant-man', 'flash'],
},
})
// Delete this Deployment
await octokit.repos.deleteDeployment({
owner,
repo,
deployment_id: deployment.id,
})
deploymentCount++
}
}
// Delete this Environment
try {
await octokit.repos.deleteAnEnvironment({
owner,
repo,
environment_name: envName,
})
} catch (error) {
if (error.status !== 404) {
throw error
}
}
console.log(
'✅',
chalk.green(
`Removed stale deployment environment "${envName}" (${deploymentCount} deployments)`
)
)
} catch (error) {
console.log(
'❌',
chalk.red(
`ERROR: Failed to remove stale deployment environment "${envName}" - ${error.message}`
)
)
}
}
}