-
Notifications
You must be signed in to change notification settings - Fork 24
/
server.js
840 lines (778 loc) · 33.8 KB
/
server.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
// this is where the express app goes
const doAsync = require("doasync");
// 3043
var express = require("express")
, exwin = require("express-winston")
, session = require("express-session")
, FileStore = require("session-file-store")(session)
, serveStatic = require("serve-static")
, bp = require("body-parser")
, async = require("async")
, fs = require("fs")
, assign = require("object-assign")
, passport = require("passport")
, GitHubStrategy = require("passport-github2").Strategy
, bl = require("bl")
, w3c = require("node-w3capi")
, jn = require("path").join
, dataDir = jn(__dirname, "data")
, GH = require("./gh")
, app = express()
, version = require("./package.json").version
, nodemailer = require('nodemailer')
, PRChecker = require('./pr-check')
;
var config, log, Store, store, mailer;
// HELPERS
// OK
function ok (res) {
res.json({ ok: true });
}
// all errors
function error (res, err) {
log.error(err);
res.status(500).json({ error: err });
}
// the handler for everything that might error, or might send data
function makeRes (res) {
return function (err, data) {
if (err) return error(res, err);
res.json(data);
};
}
// a handler for when the response is just ok
function makeOK (res) {
return function (err) {
if (err) return error(res, err);
ok(res);
};
}
// use this as middleware on any call that requires authentication
// this is for API use, not in the human URL space
// it passes if authentication has happened, otherwise it will return a 401
function ensureAPIAuth (req, res, next) {
if (req.isAuthenticated()) return next();
res.status(401).json({ error: "Authentication required." });
}
// same as above, but user has to be admin too
function ensureAdmin (req, res, next) {
ensureAPIAuth(req, res, function () {
if (!req.user.admin) return res.status(403).json({ error: "Forbidden" });
next();
});
}
// load up the GH object
function loadGH (req, res, next) {
req.gh = new GH(req.user);
next();
}
// passport uses this when a user is create to decide what we want to write into the session
// in our case we need nothing more than the username with which to retrieve it later
passport.serializeUser(function (profile, done) {
done(null, profile.username);
});
// this is the reverse operation, our session contains the key we provided above and passport gives
// us that in order to have a user. We get it from the store of course.
passport.deserializeUser(function (id, done) {
store.getUser(id, done);
});
// Express configuration
// static resources
app.use(serveStatic("public"));
var router = express.Router();
// GET this (not as an API), it will redirect the user to GitHub to authenticate
// use ?back=http://... for the URL to which to return later
router.get(
"/auth/github"
, function (req, res, next) {
var redir = config.url + "auth/github/callback";
if (req.query.back) redir += "?back=" + req.query.back;
log.info("auth github, with redir=" + redir);
passport.authenticate(
"github"
, {
// these are the permissions we request
scope: [
"user:email"
, "read:org"
]
, callbackURL: redir
}
)(req, res, next);
}
);
// Login page for admin (requires more permission to add webhooks)
router.get(
"/admin/auth/github"
, function (req, res, next) {
var redir = config.url + "auth/github/callback";
if (req.query.back) redir += "?back=" + req.query.back;
log.info("auth github, with redir=" + redir);
passport.authenticate(
"github"
, {
// these are the permissions we request
scope: [
"user:email"
, "public_repo"
, "write:repo_hook"
, "read:org"
]
, callbackURL: redir
}
)(req, res, next);
}
);
// this is the callback that we get from GH
// if all worked according to plan, it has a ?back=http://... with the location we wish to redirect
// to. Given judicious usage of the History API this should return the client to a valid state
router.get(
"/auth/github/callback"
, function (req, res, next) {
var redir = req.query.back;
passport.authenticate("github", { failureRedirect: redir + "?failure" })(req, res, next);
}
, function (req, res) {
log.info("GitHub auth success, redirecting to " + (req.query.back || "/"));
res.redirect(req.query.back || "/");
}
);
// This is the call to log the user out. Note that it is an *API* call.
router.get("/api/logout", function (req, res) {
log.info("User logging out.");
req.logout();
ok(res);
});
// check if the user is logged in
router.get("/api/logged-in", function (req, res) {
// Format of github OAuth access tokens per https://github.blog/changelog/2021-03-31-authentication-token-format-updates-are-generally-available/
if (req.user && req.user.accessToken && !req.user.accessToken.startsWith("gho_")) {
log.info(`Force disconnect of ${req.user.username}`);
req.logout();
}
res.json({ ok: req.isAuthenticated(), login: req.user ? req.user.username : null, admin: req.user ? req.user.admin : false });
});
// list all the users known to the system
router.get("/api/users", ensureAPIAuth, function (req, res) {
store.users(makeRes(res));
});
// make user an admin
router.put("/api/user/:username/admin", ensureAdmin, function (req, res) {
store.makeUserAdmin(req.params.username, makeOK(res));
});
// give user blanket okay for contributions
router.put("/api/user/:username/blanket", ensureAdmin, function (req, res) {
store.giveUserBlanket(req.params.username, makeOK(res));
});
// get user data
router.get("/api/user/:username", ensureAPIAuth, function (req, res) {
store.getUser(req.params.username, makeRes(res));
});
// set affiliation on user
router.post("/api/user/:username/affiliate", ensureAdmin, bp.json(), function (req, res) {
store.mergeOnUser(req.params.username, {
affiliation: req.body.affiliation
, affiliationName: req.body.affiliationName
, w3cid: req.body.w3cid
, w3capi: req.body.w3capi
, groups: req.body.groups
}
, makeOK(res));
});
// add a user to the system without the user logging in
router.post("/api/user/:username/add", ensureAdmin, bp.json(), loadGH, function (req, res) {
var username = req.params.username;
store.getUser(username, function (err, user) {
if (err && err.error !== "not_found") return error(res, err);
if (user) return error(res, "User " + username + " is already in the system");
req.gh.getUser(username, function (err, user) {
store.addUser(user, makeRes(res));
});
});
});
// log the last repo a user added to the system (to simplify UI)
router.get("/api/my/last-added-repo", ensureAPIAuth, function (req, res) {
store.getUser(req.user.username, (err, user) => {
if (err) return error(res, err);
return makeRes(res)(null, user.lastAddedRepo || {});
});
});
router.post("/api/my/last-added-repo", ensureAPIAuth, bp.json(), function (req, res) {
store.mergeOnUser(req.user.username, {
lastAddedRepo: req.body
}
, makeOK(res));
});
// GROUPS
// list all the groups known to the system
router.get("/api/groups", function (req, res) {
res.setHeader("Access-Control-Allow-Origin", "*");
store.groups(makeRes(res));
});
// add a group to the list of those that the system knows about
router.post("/api/groups", ensureAPIAuth, bp.json(), function (req, res) {
// group must specifiy: name, w3cid, groupType{CG, WG, IG}
store.addGroup(req.body, makeOK(res));
});
// GITHUB APIs
router.get("/api/orgs", ensureAPIAuth, loadGH, function (req, res) {
req.gh.userOrgs(makeRes(res));
});
// GITHUB APIs
router.get("/api/org-repos", ensureAPIAuth, loadGH, function (req, res) {
req.gh.userOrgRepos(makeRes(res));
});
function makeCreateOrImportRepo (mode) {
var ghFunc = mode === "create" ? "createRepo" : "importRepo";
return function (req, res) {
// need to user the req.body.group to fetch the group from the list we manage
// so that we can fill it out for createRepo
var data = req.body;
async.map(
data.groups
, function (g, cb) {
store.getGroup(g, function (err, doc) {
if (err) return cb(err);
cb(null, doc);
});
}
, function (err, allDocs) {
if (err) return error(res, err);
data.groups = allDocs;
req.gh[ghFunc](data, config, function (err, data) {
if (err) return error(res, err);
var repo = data.repo;
async.parallel(
[
function (cb) {
store.addSecret({ repo: repo.fullName, secret: repo.secret }, cb);
}
, function (cb) {
// We only set the token if the user is admin of the org
req.gh.isAdmin(req.user.username, repo.owner, function(err, isAdmin) {
if (err) return cb(err);
if (isAdmin) {
return store.createOrUpdateToken({ owner: repo.owner, token: req.user.accessToken, from: req.user.username }, cb);
} else {
// If there is no token for this org
// we send back an error
store.getToken(repo.owner, (err, token) => {
if ((err && err.error === "not_found") || !token) return cb({message: "This is the first repo imported in the " + repo.owner + " github account, it needs to be done by an owner of the organization"});
if (err) return cb(err);
cb(null);
});
}
});
}
, function (cb) {
var secretLess = assign({}, repo);
delete secretLess.secret;
store.addRepo(secretLess, cb);
}
]
, function (err) {
if (err) return error(res, err);
res.json({ actions: data.actions, repo: repo.fullName });
}
);
});
}
);
};
}
// Endpoint of W3C Webhook
router.post("/api/revalidate", bp.json(), async (req, res) => {
// Find pull requests with matching account
log.info("Received hook payload on revalidate endpoint");
log.info(JSON.stringify(req.body, null, 2));
if (!req.body || !req.body.event) {
return error(res, "Invalid request sent to revalidate endpoint");
}
let getPRs;
if (req.body.event === "connected_account.created" && req.body.account && req.body.account.nickname) { // new connected account
if (!req.body.account.service === "github") {
return res.json({msg: "Ignoring updates to accounts other than github"});
}
getPRs = new Promise((res, rej) => store.getUnaffiliatedUserPRs(req.body.account.nickname, (err, prs) => {
if (err) return rej(err);
log.info("Found unaffiliated contributors for PRs " + JSON.stringify(prs, null, 2));
return res(prs);
}));
} else if (req.body.event === "group.participant_joined" && req.body.group && req.body.group.id) { // new group participant
let githubAccount = req.body.user && req.body.user["connected-accounts"] ? req.body.user["connected-accounts"].find(a => a.service === "github") : undefined;
if (!githubAccount || !githubAccount.nickname) {
return res.json({msg: "Ignoring participations when there's no connected account"});
}
getPRs = new Promise((res, rej) => store.getOutsideUserPRs(req.body.user["connected-accounts"][0]["nickname"], (err, prs) => {
if (err) return rej(err);
const groupPRs = prs.filter(pr => pr.groups ? pr.groups.includes(req.body.group.id.toString()) : false);
log.info("Found contributors for group PRs " + JSON.stringify(groupPRs, null, 2));
return res(groupPRs);
}));
} else {
return res.json({msg: "Skipped revalidation for this event."});
}
try {
const prs = await getPRs;
// revalidate them
const delta = parseMessage(""); // this gets us a valid delta object, even if it has nothing
for (let pr of prs) {
if (pr.acceptable === "no" && pr.status === "open") {
log.info("Revalidating " + pr._id);
await new Promise((res, rej) => prChecker.validate(pr, delta, (err) => {
if (err) return rej(err);
return res();
}));
}
}
res.json({msg: "OK"});
} catch (e) {
return error(res, e);
}
});
// GITHUB APIs
router.post("/api/create-repo", ensureAPIAuth, bp.json(), loadGH, makeCreateOrImportRepo("create"));
router.post("/api/import-repo", ensureAPIAuth, bp.json(), loadGH, makeCreateOrImportRepo("import"));
router.post("/api/repos/:owner/:shortname/edit", ensureAdmin, bp.json(), function(req, res) {
store.updateRepo(req.params.owner + "/" + req.params.shortname, {groups: req.body.groups}, function(err, data) {
if (err) return makeRes(res)(err);
data.actions = ["Moved repository to groups with id " + req.body.groups.join(", ")];
makeRes(res)(null, data);
});
});
// get the permissions granted by the user
router.get("/api/scope-granted", function (req, res) {
if (req.user) {
const gh = new GH(req.user);
gh.getUser(req.user.username, function(err, user) {
res.json(user ? { scopes: user.scopes } : {});
});
} else {
res.json({ scopes: "" });
}
});
// GITHUB HOOKS
function parseMessage (msg) {
var ret = {
add: []
, remove: []
, total: 0
};
(msg || "").split(/[\n\r]+/)
.forEach(function (line) {
if (/^\+@[\w-_]+$/.test(line)) {
ret.add.push(line.replace(/^\+@/, ""));
ret.total++;
}
else if (/^-@[\w-_]+$/.test(line)) {
ret.remove.push(line.replace(/^-@/, ""));
ret.total++;
}
})
;
return ret;
}
function addGHHook(app, path) {
app.post("/" + path, function (req, res) {
// we can't use bp.json(), that'll wreck secret checking by consuming the buffer
// first, req.pipe(bl(function (err, data))) to save the buffer
req.pipe(bl(async function (err, buffer) {
if (err) return error(res, err);
// get us some JSON
var event;
try { event = JSON.parse(buffer.toString()); }
catch (e) { return error(res, e); }
// run checks before getting the secret to check the crypto
var eventType = req.headers["x-github-event"];
log.info("Hook got GH event " + eventType);
// we only care about PRs, issue comments on PR and repository
if (!["pull_request", "issue_comment", "repository"].includes(eventType)
|| (eventType === "issue_comment" && !event.issue.pull_request)) {
return ok(res);
}
const owner = event.repository.owner.login
, repo = event.repository.full_name
, repoShortname = event.repository.name
, repoId = event.repository.id
;
// for repository, action is needed only for renames and transfers
if (eventType === "repository") {
let previousRepo;
if (event.action === "renamed") {
previousRepo = `${owner}/${event.changes.repository.name.from}`;
} else if (event.action === "transferred") {
previousRepo = `${event.changes.owner.from.user.login}/${repoShortname}`;
} else {
return ok(res);
}
log.info(`Repository ${event.action} from ${previousRepo} to ${repo}`);
// update secret, repository and all associated PRs
await doAsync(store).updateSecret(previousRepo, {repo: repo});
await doAsync(store).updateRepo(previousRepo, {name: repoShortname, fullName: repo, owner});
store.getPRsByRepo(previousRepo, async function(err, prs) {
if (err) {
return error(res, `Error fetching PRs from the repository: ${previousRepo}`);
}
for (const pr of prs) {
await doAsync(store).updatePR(pr.fullName, pr.num, {fullName: repo, shortName: repoShortname, owner});
}
return ok(res);
});
} else {
const statusData = {
owner,
shortName: repoShortname,
payload: {
state: "failure",
target_url: `${config.url}pr/id/${owner}/${repoShortname}/${event.number}`,
context: "ipr"
}
}
;
if (event.pull_request && event.pull_request.head && event.pull_request.head.sha) {
statusData.sha = event.pull_request.head.sha;
}
store.getSecret(repo, async function (err, data) {
const { token } = await doAsync(store).getToken(owner)
, gh = new GH({ accessToken: token });
if (err || !data) {
try {
statusData.payload.description = `The repository manager doesn't know the following repository: ${repo}`;
gh.status(statusData, err => {
if (err) {
console.log(err);
log.error(err);
}
});
} catch (e) {
log.error(`Token not found for ${owner}`);
}
return error(res, "Secret for " + repo + " not found: " + (err || "simply not there."));
}
// we have the secret, crypto check becomes possible
if (!GH.checkPayloadSignature("sha256", data.secret, buffer, req.headers["x-hub-signature-256"])) {
statusData.payload.description = `GitHub signature does not match known secret for ${repo}.`;
gh.status(statusData, err => {
if (err) {
console.log(err);
log.error(err);
}
});
return error(res, `GitHub signature does not match known secret for ${repo}.`);
}
// for status we need: owner, repoShort, and sha
var repoShort = event.repository.name
, prNum = (eventType === "pull_request") ? event.number : event.issue.number
;
// pull request events
if (eventType === "pull_request") {
var sha = event.pull_request.head.sha;
// if event.action === "closed" we have to store the new head SHA in
// the DB, but other than that we can ignore it
if (event.action === "closed") {
return store.updatePR(
repo
, prNum
, {
status: "closed"
, sha: sha
}
, makeOK(res)
);
// if event.action === "synchronize" we have to store the new head SHA in
// the DB, and re-send the pr status to github based on stored data
} else if (event.action === "synchronize") {
return store.getPR(repo, prNum, function (err, storedpr) {
if (err || !storedpr) return error(res, (err || "PR not found: " + repo + "-" + prNum));
storedpr.sha = sha;
prChecker.validate(storedpr, parseMessage(""), makeOK(res));
});
} else if (event.action === "opened" || event.action === "reopened") {
var pr = {
fullName: repo
, repoId: repoId
, shortName: repoShort
, owner: owner
, num: prNum
, sha: sha
, status: "open"
, acceptable: "pending"
, unknownUsers: []
, outsideUsers: []
, contributors: [event.pull_request.user.login]
, contribStatus: {}
}
, delta = parseMessage(event.pull_request.body)
;
store.addPR(pr, function (err) {
if (err) return error(res, err);
prChecker.validate(pr, delta, makeOK(res));
});
} else {
// we ignore other pull request events
return ok(res);
}
}
// issue comment events
else if (eventType === "issue_comment") {
var delta = parseMessage(event.comment.body);
if (!delta.total) return ok(res);
store.getPR(repo, prNum, function (err, pr) {
if (err || !pr) return error(res, (err || "PR not found: " + repo + "-" + prNum));
prChecker.validate(pr, delta, makeOK(res));
});
}
});
}
}));
});
}
// get a given PR
router.get("/api/pr/:owner/:shortName/:num", bp.json(), function (req, res) {
var prms = req.params;
store.getPR(prms.owner + "/" + prms.shortName, prms.num, function(err, pr) {
if (err || !pr) return makeRes(res)(err || "PR not found: " + prms.owner + "/" + prms.shortName + "-" + prms.num);
async.map(pr.groups,
function(groupid, cb) {
store.getGroup(groupid, cb)
},
function (err, results) {
if (err) return makeRes(res)(err);
pr.groupDetails = results;
makeRes(res)(null, pr);
});
});
});
// revalidate a PR
router.post("/api/pr/:owner/:shortName/:num/revalidate", ensureAPIAuth, function (req, res) {
var prms = req.params
, delta = parseMessage("") // this gets us a valid delta object, even if it has nothing
;
log.info("Revalidating " + prms.owner + "/" + prms.shortName + "/pulls/" + prms.num);
store.getPR(prms.owner + "/" + prms.shortName, prms.num, function (err, pr) {
if (err || !pr) return error(res, (err || "PR not found: " + prms.owner + "/" + prms.shortName + "/pulls/" + prms.num));
prChecker.validate(pr, delta, makeOK(res));
});
});
// Mark a PR as non substantive
router.post("/api/pr/:owner/:shortName/:num/markAs(|Non)Substantive", ensureAPIAuth, loadGH, function (req, res) {
var prms = req.params
, delta = parseMessage("") // this gets us a valid delta object, even if it has nothing
, qualifier = prms[0].toLowerCase() // "" or "non" depending on the path
;
log.info("Marking " + prms.owner + "/" + prms.shortName + "/pulls/" + prms.num + " as " + qualifier + " substantive");
store.getPR(prms.owner + "/" + prms.shortName, prms.num, function (err, pr) {
if (err || !pr) return error(res, (err || "PR not found: " + prms.owner + "/" + prms.shortName + "/pulls/" + prms.num));
store.updatePR(pr.fullName, pr.num, {markedAsNonSubstantiveBy: qualifier == "non" ? req.user.username : null}, function (err) {
if (err) return error(res, err);
store.getPR(pr.fullName, pr.num, function(err, updatedPR) {
if (err || !updatedPR) return error(res, (err || "PR not found: " + pr.fullName + "/pulls/" + pr.num));
prChecker.validate(updatedPR, delta, function(err, pr) {
if (err) return error(res, err);
pr.comment = `[${req.user.username}](https://github.com/${req.user.username}) marked as ${qualifier} substantive for IPR from ash-nazg.`;
req.gh.commentOnPR(pr, function(err, comment) {
makeRes(res)(err, pr);
});
});
});
});
});
});
// list open PRs
router.get("/api/pr/open", function (req, res) {
store.getOpenPRs(makeRes(res));
});
// list PRs from last week
router.get("/api/pr/last-week", function (req, res) {
store.getLastWeekPRs(makeRes(res));
});
// list repos
router.get("/api/repos", function (req, res) {
res.setHeader("Access-Control-Allow-Origin", "*");
var groups = {};
store.repos(function (err, docs) {
if (err) return error(res, err);
docs.forEach(function (doc) {
doc.groups.forEach(function (g) {
groups[g] = true;
});
});
async.each(
Object.keys(groups)
, function (g, cb) {
store.getGroup(g, function (err, doc) {
if (err) return cb(err);
groups[g] = doc;
cb();
});
}
, function (err) {
if (err) return error(res, err);
docs.forEach(function (doc) {
doc.groups.forEach(function (g, idx) {
doc.groups[idx] = groups[g];
});
});
res.json(docs);
}
);
});
});
// list contributors to a repo
router.get("/api/repos/:owner/:shortName/contributors", function (req, res) {
res.setHeader("Access-Control-Allow-Origin", "*");
const prms = req.params;
store.getPRsByRepo(prms.owner + "/" + prms.shortName, function (err, docs) {
if (err) return error(res, err);
const substantiveContributors = {};
const nonSubstantiveContributors = {};
docs.forEach(function (doc) {
if (doc.markedAsNonSubstantiveBy) {
for (const contributor of doc.contributors) {
if (!nonSubstantiveContributors[contributor]) {
nonSubstantiveContributors[contributor] = { name: contributor, prs: []};
}
nonSubstantiveContributors[contributor].prs.push(doc.num);
}
} else {
for (const affiliationId in doc.affiliations) {
if (!substantiveContributors[affiliationId]) {
substantiveContributors[affiliationId] = { name: doc.affiliations[affiliationId], prs: []};
}
substantiveContributors[affiliationId].prs.push(doc.num);
}
}
});
res.json({substantiveContributors, nonSubstantiveContributors});
});
});
// router.del("/api/repo/:owner/:name", ensureAdmin, function (req, res) {
// // delete repo
// // remove hook
// });
// list the group the current user is team contact of
router.get("/api/team-contact-of", ensureAPIAuth, function (req, res) {
let username = req.user.username;
store.getUser(username, function (err, user) {
if (err) {
return makeRes(res)(err);
}
if (!user.w3capi) {
return error(res, err);
}
w3c.user(user.w3capi).teamcontactofgroups().fetch(function(err, teamcontactof) {
if (err || teamcontactof[0] === undefined) {
return error(res, err);
}
return makeRes(res)(null, teamcontactof);
});
});
});
// W3C APIs
// given the issues with paging and irregularities in the W3C API, it has been wrapped up in
// an easy to use library that is meant to be used on the server side
// therefore, the client never contacts the W3C API directly, but instead hits these endpoints
router.get("/api/w3c/groups", function (req, res) {
// these require the embedded data
w3c.groups().fetch({ embed: true }, makeRes(res));
});
router.get("/api/w3c/group/:group", function (req, res) {
w3c.group(req.params.group).fetch(makeRes(res));
});
router.get("/api/w3c/group/:group/users", function (req, res) {
w3c.group(req.params.group).users().fetch(makeRes(res));
});
router.get("/api/w3c/user/:user", function (req, res) {
w3c.user(req.params.user).fetch(makeRes(res));
});
router.get("/api/w3c/user/:user/affiliations", function (req, res) {
w3c.user(req.params.user).affiliations().fetch(makeRes(res));
});
function addClientSideRoutes(app) {
// handler for client-side routing
var indexHTML = fs.readFileSync(jn(__dirname, "templates/app.html"), "utf8");
function showIndex (req, res) {
res.send(indexHTML);
}
app.get("/", showIndex);
app.get("/login", showIndex);
app.get("/repo/*", showIndex);
app.get("/repos", showIndex);
app.get("/repos/*", showIndex);
app.get("/admin/*", ensureAdmin, showIndex);
app.get("/pr/*", showIndex);
}
// run!
function run(configuration, configuredmailer) {
config = configuration;
log = require("./log")(config);
Store = require("./store");
store = new Store(config);
mailer = configuredmailer;
// logging
app.use(exwin.logger({
winstonInstance: log
, expressFormat: true
}));
// sessions
app.use(session({
store: new FileStore({
path: jn(dataDir, "sessions")
, ttl: 60 * 60 * 24 * 7
})
, cookie: { maxAge: 1000 * 60 * 60 * 24 * 365 , sameSite: "lax"}
, name: "ash-nazg"
, resave: false
, rolling: true
, saveUninitialized: false
, secret: config.sessionSecret
}));
prChecker = new PRChecker(config, log, store, GH, mailer);
passport.use(
new GitHubStrategy({
clientID: config.ghClientID
, clientSecret: config.ghClientSecret
, callbackURL: config.url + "auth/github/callback"
}
, function (accessToken, refreshToken, profile, done) {
log.info("Login attempt for user " + profile.username);
// we have a user logging in, could be new, could be old, we add it to the store
// but first we need to massage it a bit
profile.ghID = profile.id;
delete profile.id;
delete profile._raw;
var pics = profile.photos || []
, json = profile._json
;
if (json.avatar_url) pics.push({ value: json.avatar_url });
profile.photos = pics;
if (json.blog) profile.blog = json.blog;
if (json.company) profile.company = json.company;
delete profile._json;
profile.accessToken = accessToken;
profile.refreshToken = refreshToken;
store.findOrCreateUser(profile, function (err) {
if (err) return done(err);
log.info("Login successful: " + profile.username);
done(null, profile);
});
}
));
// GH auth init
app.use(passport.initialize());
app.use(passport.session());
app.use(router);
addGHHook(app, config.hookPath);
addClientSideRoutes(app);
return app.listen(config.serverPort, function (err) {
if (err) return log.error(err);
log.info("Ash-Nazg/" + version + " up and running.");
});
}
module.exports.run = run;
module.exports.app = app;
if (require.main === module) {
// FIXME abstract into configuration the mailer parameters
var transporter = nodemailer.createTransport({sendmail: true, path: '/usr/sbin/sendmail', tls: {rejectUnauthorized: false}});
run(require("./config.json"), transporter);
}