-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapp.js
More file actions
185 lines (167 loc) · 6.15 KB
/
Copy pathapp.js
File metadata and controls
185 lines (167 loc) · 6.15 KB
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
import config from './lib/config-loader.js';
import express from 'express';
import bodyParser from 'body-parser';
import expressSession from 'express-session';
import authManager from './lib/authentication.js';
import socketAuthenticator from './lib/socket-auth.js';
import refresh from './lib/refresh.js';
import pullManager from './lib/pull-manager.js';
import git from './lib/git-manager.js';
import dbManager from './lib/db-manager.js';
import pullQueue from './lib/pull-queue.js';
import mainController from './controllers/main.js';
import hooksController from './controllers/githubHooks.js';
import statsController from './controllers/stats.js';
import userNamesController from './controllers/user-names.js';
import apiController from './controllers/api.js';
import apiAuth from './lib/api-auth.js';
import Debug from './lib/debug.js';
import { createServer } from 'http';
import { Server } from 'socket.io';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const reqLogger = Debug('pulldasher:server:request');
const debug = Debug('pulldasher');
const app = express();
const httpServer = createServer(app);
const maxPostSize = 1024 * 1024;
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
app.set('view engine', 'html');
/**
* Middleware
*/
app.use('/public', express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({ limit: maxPostSize, extended: false }));
app.use(bodyParser.json({ limit: maxPostSize }));
app.use(
expressSession({
secret: config.session.secret,
resave: false,
saveUninitialized: false,
})
);
app.use(authManager.passport.initialize());
app.use(authManager.passport.session());
app.use(function (req, res, next) {
reqLogger('%s %s', req.method, req.url);
next();
});
/**
* Routes
*/
authManager.setupRoutes(app);
// v2 is the primary board at the root; the legacy v1 board runs side-by-side
// under /v1 (its assets are built with a matching /v1/ publicPath). Both share
// the same /token + socket.io API below. The /v1 mount is more specific, so it
// must precede the '/' catch-all static mount.
app.use('/v1', express.static(__dirname + '/frontend/dist'));
app.use('/', express.static(__dirname + '/frontend-v2/dist'));
app.get('/token', mainController.getToken);
app.get('/stats-history', statsController.getHistory);
app.get('/user-names', userNamesController.getNames);
app.post('/hooks/main', hooksController.main);
// /api/v1: machine-to-machine JSON for the review skills, Bearer-authed with
// the caller's own GitHub token (see lib/api-auth). Independent of the
// cookie-session gate -- setupRoutes never registers these paths, so the
// session `auth` middleware doesn't run for them.
app.get('/api/v1/me', apiAuth, apiController.getMe);
app.get('/api/v1/pulls', apiAuth, apiController.getPulls);
// Warm the bot-login cache (used to tell a pulldasher claim apart from a
// GitHub-UI self-request) before any webhook or socket traffic needs it.
// Memoized in git-manager, so this just avoids the first caller paying for
// the lookup.
git.getBotLogin();
debug('Loading all recent pulls from the DB');
dbManager
.getRecentPulls(pullManager.getOldestAllowedPullTimestamp())
.then(function (pulls) {
debug('Loaded %s pulls', pulls.length);
pullQueue.pause();
pulls.forEach(function (pull) {
pullManager.updatePull(pull);
});
pullQueue.resume();
})
.then(function () {
debug('Refreshing all open pulls from the API');
refresh.openPulls();
})
.done();
//====================================================
// Socket.IO
const io = new Server(httpServer);
io.on('connection', function (socket) {
var unauthenticated_timeout =
config.unauthenticated_timeout !== undefined ? config.unauthenticated_timeout : 10 * 1000;
var autoDisconnect = setTimeout(function () {
socket.disconnect();
}, unauthenticated_timeout);
socket.once('authenticate', function (token) {
// They did respond. No need to drop their connection for not responding.
clearTimeout(autoDisconnect);
var user = socketAuthenticator.retrieveUser(token);
if (user) {
socket.user = user;
socket.emit('authenticated');
pullManager.addSocket(socket);
} else {
socket.emit('unauthenticated');
socket.disconnect();
}
});
socket.on('refresh', function (repo, number) {
refresh.pull(repo, number).catch(function (err) {
console.error(
'Socket "refresh" failed for %s#%s: %s',
repo,
number,
(err && err.message) || err
);
});
});
// GitHub's requested_reviewers is now the only claim state (see
// review_requests on the pull payload). `ttlMs` may still arrive from
// older clients that used to size a claim's expiry; it's meaningless now
// and ignored. Requesting the reviewer on GitHub, then refreshing the pull
// from the API, is what makes the claim show up for every client -- there's
// no separate broadcast to do here.
socket.on('claimReview', function (repo, number) {
if (!socket.user) {
return;
}
git.requestReviewer(repo, number, socket.user.username)
.then(function () {
return refresh.pull(repo, number);
})
.catch(function (err) {
console.error(
'Socket "claimReview" failed for %s#%s (user %s): %s',
repo,
number,
socket.user.username,
(err && err.message) || err
);
});
});
socket.on('releaseReview', function (repo, number) {
if (!socket.user) {
return;
}
git.removeReviewer(repo, number, socket.user.username)
.then(function () {
return refresh.pull(repo, number);
})
.catch(function (err) {
console.error(
'Socket "releaseReview" failed for %s#%s (user %s): %s',
repo,
number,
socket.user.username,
(err && err.message) || err
);
});
});
});
debug('Listening on port %s', config.port);
httpServer.listen(config.port);