Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
334 lines (308 loc) · 13.2 KB
/
Copy pathapp.js
File metadata and controls
334 lines (308 loc) · 13.2 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
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, { observeRateLimit } from './lib/git-manager.js';
import dbManager from './lib/db-manager.js';
import pullQueue from './lib/pull-queue.js';
import { createRefreshAll } from './lib/refresh-all.js';
import { createPacer } from './lib/pacer.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 projectsController from './controllers/projects.js';
import roadmapController, { canWrite, forgetProjectCaches } from './controllers/roadmap.js';
import { API_ROUTES, apiIndex } from './controllers/api-routes.js';
import settingsController from './controllers/settings.js';
import { syncRepoIssues } from './lib/project-issue-sync.js';
import { loadSettingsWithRetry } from './lib/settings.js';
import { issueRepos, projectSettings } from './lib/projects.js';
import { setOnWorkSynced, syncWork } from './lib/work.js';
import { missingMigrations } from './lib/schema-check.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');
// "Refresh all" compares GitHub with the board, so it waits for the board to
// load and for the startup refresh, which re-reads every open pull a press
// would. Its refetches wait on their own pacer, which watches the quota every
// response reports, webhook refreshes included, so a press yields to them.
let boardLoaded;
const refreshAllPacer = createPacer();
observeRateLimit(refreshAllPacer);
const refreshAll = createRefreshAll({
board: pullManager.getPulls,
repos: config.repos,
closedSince: pullManager.getOldestAllowedPullTimestamp,
pacer: refreshAllPacer,
ready: new Promise(resolve => (boardLoaded = resolve)),
onProgress: progress => pullManager.broadcast('refreshAllProgress', progress),
});
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.get('/projects-data', projectsController.getBoardData);
app.get('/retro-data', projectsController.getRetro);
app.get('/work-data', projectsController.getWork);
app.get('/issue-search', projectsController.searchIssues);
app.get('/project-work', projectsController.getProjectWork);
// the review board's view of projects: which projects its PRs link, and
// each project's plan, so it orders and names PRs by the Projects tab's rule
app.get('/project-standing', roadmapController.standing);
// Something open boards show of the projects changed: what's kept of them is
// read again, and every open board fetches again, so one person's call shows
// on another's screen without a reload.
function projectsChanged() {
forgetProjectCaches();
pullManager.projectsChanged();
}
// every write the Projects tab can see
const PROJECT_WRITES = ['/project-issues', '/roadmap', '/settings', '/api/v1/project-issues', '/api/v1/roadmap', '/api/v1/settings'];
app.use(PROJECT_WRITES, function (req, res, next) {
if (req.method !== 'GET') {
res.on('finish', function () {
if (res.statusCode < 300) projectsChanged();
});
}
next();
});
setOnWorkSynced(projectsChanged);
app.post('/project-issues', canWrite, projectsController.attachIssue);
app.delete('/project-issues', canWrite, projectsController.detachIssue);
// the roadmap is the one part of the Projects tab people edit here: reads are
// gated like the other board data (lib/authentication.js), writes by canWrite
app.get('/roadmap', roadmapController.list);
app.post('/roadmap', canWrite, roadmapController.create);
app.put('/roadmap/order', canWrite, roadmapController.reorder);
app.patch('/roadmap/:id', canWrite, roadmapController.update);
app.delete('/roadmap/:id', canWrite, roadmapController.remove);
app.get('/roadmap/:id/updates', roadmapController.updates);
app.post('/roadmap/:id/updates', canWrite, roadmapController.postUpdate);
app.delete('/roadmap/:id/updates/:update', canWrite, roadmapController.removeUpdate);
app.get('/settings', settingsController.get);
app.patch('/settings', canWrite, settingsController.update);
app.post('/hooks/main', hooksController.main);
// /api/v1: machine-to-machine JSON for the review skills and scripts,
// Bearer-authed with the caller's own GitHub token (see lib/api-auth). Every
// route is in controllers/api-routes.js, and GET /api/v1 lists them.
// 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', apiAuth, apiIndex);
for (const { method, path, handlers } of API_ROUTES) app[method](path, apiAuth, ...handlers);
// 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();
// Saved settings (the developer teams) replace config.js's once loaded;
// until then, and if the table can't be read, config.js's stand.
loadSettingsWithRetry();
// Migrations run by hand, before a deploy (migrations/*.sql never run on
// one). Say loudly at startup which are missing, since the board then
// fails in pieces: pull refreshes, the issue syncs, roadmap reads.
missingMigrations()
.then(missing => {
if (missing.length) {
console.error(
'The database is missing migrations, so parts of the board will fail. Run these by hand, in order: %s',
missing.join(', ')
);
}
})
.catch(err => console.error('Checking the schema failed: %s', (err && err.message) || err));
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().finally(boardLoaded);
syncProjectIssues();
syncAttachedIssues();
})
.done();
// Webhooks get lost, and a lost `closed` left a PR open on the board until the
// next restart (pulldasher#501 repairs it only at startup), which inflates every
// backlog number. Once an hour, list each repo's open pulls and refresh just the
// ones the DB has wrong, and pick up project issues that changed.
const RECONCILE_MS = 60 * 60 * 1000;
setInterval(function () {
refresh.reconcileOpenPulls().catch(function (err) {
console.error('Hourly open-pull repair failed: %s', (err && err.message) || err);
});
syncProjectIssues();
syncAttachedIssues();
}, RECONCILE_MS);
// The issues added to projects by hand, and which PRs link each issue a
// project has (lib/work.js), read off GitHub once an hour; a webhook on one
// of those issues reads them sooner.
function syncAttachedIssues() {
syncWork(projectSettings()).catch(function (err) {
console.error('Work sync failed: %s', (err && err.message) || err);
});
}
// Project issues can live in any tracked repo (lib/projects.js issueRepos).
// The projects repo is synced whole at startup; a tracked repo only from
// TRACKED_ISSUES_DAYS back, since listing every issue of a big repo costs
// thousands of calls, and webhooks already keep its issues current. So an
// issue labeled while webhooks were down for longer than that waits for its
// next change (or bin/refresh-open-issues). After that, only what changed.
// `since` backs off a few minutes so GitHub's clock can't skip an update.
const TRACKED_ISSUES_DAYS = 7;
function syncProjectIssues() {
const projects = projectSettings();
if (!projects) return;
const startedAt = new Date(Date.now() - 5 * 60 * 1000).toISOString();
const lookback = new Date(Date.now() - TRACKED_ISSUES_DAYS * 86400 * 1000).toISOString();
const synced = issueRepos(projects).map(function (repo) {
const firstSince = repo === projects.repo ? null : lookback;
return syncRepoIssues(refresh, repo, firstSince, startedAt).catch(function (err) {
console.error('Project issue sync failed in %s: %s', repo, (err && err.message) || err);
return false;
});
});
// a project's name, target or state may have changed: open boards hear it
// once, after every repo is read, and only when an issue changed (a word
// per repo had every open Projects tab fetch it all again once per repo)
Promise.all(synced).then(function (changed) {
if (changed.some(Boolean)) projectsChanged();
});
}
//====================================================
// 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);
// a board that connects mid-press shows how far it got
const progress = refreshAll.progress();
if (progress && progress.state !== 'done') {
socket.emit('refreshAllProgress', progress);
}
} else {
socket.emit('unauthenticated');
socket.disconnect();
}
});
socket.on('refreshAll', function () {
if (socket.user) {
refreshAll.start();
}
});
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);