forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete-unused-staging-apps.js
executable file
·57 lines (49 loc) · 1.51 KB
/
delete-unused-staging-apps.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
#!/usr/bin/env node
// [start-readme]
//
// This script finds and lists all the Heroku staging apps and deletes any leftover apps that have closed PRs
//
// [end-readme]
require('dotenv').config()
const assert = require('assert')
assert(process.env.HEROKU_API_TOKEN)
const { chain } = require('lodash')
const chalk = require('chalk')
const Heroku = require('heroku-client')
const github = require('../lib/github')()
const heroku = new Heroku({ token: process.env.HEROKU_API_TOKEN })
const owner = 'github'
const repo = 'docs-internal'
const stagingAppNamePrefix = 'docs-internal-pr-'
main()
async function main () {
const stagingApps = chain(await heroku.get('/apps'))
.filter(app => app.name.startsWith(stagingAppNamePrefix))
.map(app => {
app.pullRequestNumber = Number(app.name.match(/\d+$/)[0])
return app
})
.orderBy('name')
.value()
console.log('staging apps:', stagingApps.length)
for (const app of stagingApps) {
try {
const { data: pr } = await github.pulls.get({
owner,
repo,
pull_number: app.pullRequestNumber
})
if (pr.state === 'open') {
console.log(chalk.green(app.name))
} else if (pr.state === 'closed') {
console.log(chalk.red(app.name), '(PR was closed; deleting app now)')
await heroku.delete(`/apps/${app.name}`)
} else {
console.log(chalk.red(app.name), `(${pr.state})`)
}
} catch (err) {
console.log('no PR found', chalk.red(app.name))
console.log(err)
}
}
}