-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
256 lines (213 loc) · 6.63 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
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
248
249
250
251
252
253
254
255
256
const express = require('express')
const session = require('express-session')
const partials = require('express-partials')
const oauth = require('./lib/oauth')
const bodyParser = require('body-parser')
const { Octokit } = require('@octokit/rest')
const { parsePackageContext } = require('./lib/process')
const path = require('path')
const apicache = require('apicache')
/**
* This is the main entrypoint to your Probot app
* @param {import('probot').Application} app
*/
module.exports = async (app, { getRouter }) => {
const router = getRouter("/")
// Your code here
app.log('Yay, the app was loaded!')
const db = require('./models')
try {
await db.sequelize.authenticate()
app.log('Connection has been established successfully.')
} catch (error) {
app.log('Unable to connect to the database:', error)
console.log(error)
}
app.log('authenticated')
await db.sequelize.sync({ alter: true })
app.log('synced')
if (process.env.REPROCESS_ALL === 'True') {
app.log('Reporecessing all packages')
db.Package.update({
processing: true
}, {
where: {}
}).then(function (rows) {
console.log('modified', rows)
}).catch(function (err) {
console.log('caught an error', err)
})
}
const expressApp = express()
expressApp.use(express.static('public'))
expressApp.db = db
expressApp.set('views', path.join(__dirname, 'views'))
expressApp.set('view engine', 'ejs')
expressApp.use(session({ secret: 'keyboard cat', resave: false, saveUninitialized: true }))
expressApp.use(partials())
expressApp.use(require('connect-flash')())
expressApp.use(bodyParser.urlencoded({ extended: true }))
expressApp.use(bodyParser.json())
expressApp.apicache = apicache.middleware
expressApp.use(async (req, res, next) => {
console.log('setting robot', req.url)
req.robot = app
const personalAccess = new Octokit({
auth: process.env.GITHUB_PERSONAL_ACCESS_TOKEN
})
req.octokitWithPersonalAccessToken = personalAccess
const octokit = await app.auth()
req.octokit = octokit
const { data } = await octokit.apps.getAuthenticated()
req.whoami = data
res.locals.whoami = data
res.locals.req = req
res.locals.flash = req.flash()
res.locals.packageCount = await db.Package.count({ where: { processing: false, error: null } })
next()
})
oauth(expressApp)
require('./lib/index')(expressApp)
require('./lib/about')(expressApp)
require('./lib/add')(expressApp)
require('./lib/all')(expressApp)
require('./lib/api/collection')(expressApp)
require('./lib/package')(expressApp)
require('./lib/search')(expressApp)
require('./lib/mod')(expressApp)
require('./lib/delete')(expressApp)
require('./lib/api/package')(expressApp)
require('./lib/api/all')(expressApp)
require('./lib/api/username')(expressApp)
expressApp.get('/whoami', async function (req, res) {
const octokit = await app.auth()
const { data } = await octokit.apps.getAuthenticated()
res.json(data)
})
expressApp.get('/mod/cache/performance', async function (req, res) {
if (!req.user || !req.user.isMod) {
res.redirect('/')
return
}
res.json(apicache.getPerformance())
})
expressApp.get('/mod/cache/index', async function (req, res) {
if (!req.user || !req.user.isMod) {
res.redirect('/')
return
}
res.json(res.json(apicache.getIndex()))
})
require('./lib/userAllPackage')(expressApp)
router.use(expressApp)
var removeIsInstall = function (repo) {
db.Package.findOne({
where: {
github_id: repo.id
}
}).then(function (_package) {
_package.is_installed = false
_package.save()
}).catch(() => {})
}
var addRepo = async function (context, repo) {
if (repo.private) {
return
}
const owner = repo.full_name.split('/')[0]
const name = repo.full_name.split('/')[1]
var latestRelease
try {
var { data: lr } = await context.github.repos.getLatestRelease({ owner, repo: name })
latestRelease = lr
} catch (err) {}
var ref = 'master'
if (latestRelease) {
ref = latestRelease.tag_name
}
var pkg
try {
var { data } = await context.github.repos.getContents({ owner, repo: name, path: 'Package.swift', ref })
pkg = data
} catch (err) { return }
if (pkg) {
const { data: info } = await context.github.repos.get({ owner, repo: name })
if (!info) { return }
const [_package] = await db.Package.findOrCreate({
where: { github_id: repo.id },
defaults: {
github_id: repo.id,
info: info
}
})
_package.info = info
_package.is_installed = true
_package.save()
parsePackageContext(context, _package)
}
}
app.on('installation', async context => {
const payload = context.payload
app.log('An installation was ', payload.action)
// console.log(context)
const jwt = app.app.getSignedJsonWebToken()
context.log('jwt', jwt)
if (payload.action === 'created') {
payload.repositories.forEach(async function (repo) {
addRepo(context, repo)
})
} else if (payload.action === 'deleted') {
payload.repositories.forEach(async function (repo) {
removeIsInstall(repo)
})
}
})
app.on('installation_repositories', async context => {
const payload = context.payload
if (!payload) { return }
if (payload.repositories_added) {
payload.repositories_added.forEach(function (repo) {
addRepo(context, repo)
})
}
if (payload.repositories_removed) {
payload.repositories_removed.forEach(function (repo) {
removeIsInstall(repo)
})
}
})
app.on(['release', 'public', 'created'], async context => {
const payload = context.payload
console.log(context.payload)
if (!payload) {
return
}
if (!payload.repository) {
return
}
await addRepo(context, payload.repository)
})
var updateRepoInfo = async function (context, repo) {
const owner = repo.full_name.split('/')[0]
const name = repo.full_name.split('/')[1]
const { data: info } = await context.github.repos.get({ owner, repo: name })
if (!info) { return }
const _package = await db.Package.findOne({
where: { github_id: repo.id }
})
if (_package) {
_package.info = info
_package.save()
}
}
app.on(['star', 'repository'], async context => {
const payload = context.payload
if (!payload) { return }
if (!payload.repository) { return }
if (payload.repository.private) {
return
}
const repo = payload.repository
updateRepoInfo(context, repo)
})
}