-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
113 lines (93 loc) · 2.73 KB
/
index.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
import ghauth from 'ghauth'
import { Octokit } from 'octokit'
import { retry } from '@octokit/plugin-retry'
import { throttling } from '@octokit/plugin-throttling'
import * as readline from 'node:readline/promises'
import { stdin as input, stdout as output } from 'node:process'
const rl = readline.createInterface({ input, output })
Octokit.plugin(throttling)
Octokit.plugin(retry)
const authOptions = {
configName: 'socket-app-installer',
scopes: ['repo', 'admin:org'],
noDeviceFlow: true
}
const authData = await ghauth(authOptions)
const octokit = new Octokit({
auth: authData.token,
throttle: {
onRateLimit: (retryAfter, options) => {
console.log(
`Request quota exhausted for request ${options.method} ${options.url}`
)
if (options.request.retryCount <= 20) {
console.log(`Retrying after ${retryAfter} seconds!`)
return true
}
},
onAbuseLimit: (retryAfter, options) => {
console.log(`Abuse detected for request ${options.method} ${options.url}`)
}
}
})
const org = await rl.question('Org Name?\n')
const installTopic = await rl.question('What repo topic should we activate Socket on?\n')
rl.close()
console.log(`Activating Socket for ${org} on all repos with the ${installTopic} topic`)
const appID = 156372 // socket-security
// const appID = 155833 // socket-security-dev
let hasAppInstalled = false
let appInstall = null
for await (const response of octokit.paginate.iterator(
octokit.rest.orgs.listAppInstallations,
{
org,
per_page: 100
}
)) {
for (const app of response.data) {
if (app.app_id === appID) {
hasAppInstalled = true
appInstall = app
break
}
}
}
if (!hasAppInstalled) {
console.error('App is not installed')
process.exit(1)
}
if (appInstall.suspended_at !== null) {
console.error('App install appears to be suspended')
process.exit(1)
}
if (appInstall.repository_selection !== 'selected') {
console.error('App is installed to all repos. Can\'t selectively enable repos')
process.exit(1)
}
console.log('App is installed')
const reposToInstall = []
for await (const response of octokit.paginate.iterator(
octokit.rest.repos.listForOrg,
{
org,
per_page: 100
}
)) {
for (const repo of response.data) {
if (repo?.topics.includes(installTopic)) {
reposToInstall.push(repo)
console.log(`Install Socket to ${repo.full_name}`)
}
}
}
for (const repo of reposToInstall) {
console.log(`Installing to ${repo.name}...`)
await octokit.rest.apps.addRepoToInstallationForAuthenticatedUser({
installation_id: appInstall.id,
repository_id: repo.id
})
console.log(`Installed to ${repo.name}`)
}
console.log(`Installed app to every repo with ${installTopic} topic`)
process.exit(0)