forked from CouchPotato/Issue-Bot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
86 lines (68 loc) · 1.96 KB
/
app.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
var express = require('express'),
compress = require('compression'),
bodyParser = require('body-parser'),
// HTTP server
http = require('http'),
xhub = require('express-x-hub'),
redis = require('redis'),
app = express(),
server = http.createServer(app),
GitHubApi = require("github"),
EventEmitter = require('events').EventEmitter;
global.winston = require('winston');
global.config = config = require(__dirname + '/config');
global.pubsub = new EventEmitter();
global.github = new GitHubApi({
// required
version: '3.0.0',
// optional
timeout: 5000,
headers: {
"user-agent": 'SickRageBot'
}
});
github.authenticate({
type: 'token',
token: config.token
});
// Development only
if(app.get('env') != 'development') {
winston.add(winston.transports.File, { filename: __dirname + '/logs/main.log'});
winston.remove(winston.transports.Console);
}
// all environments
app.set('port', config.port || 1234);
app.use(compress());
// Add xhub check
if(config.secret){
app.use(xhub({ algorithm: 'sha1', secret: config.secret }));
// Check if xhub is set
app.use(function(req, res, next){
if(req.isXHub && req.isXHubValid()){
next();
return;
}
res.status(401).send('Your shoe\'s untied');
});
}
app.use(bodyParser.urlencoded({'extended': true}))
app.use(bodyParser.json({'limit': '50mb'}));
app.post('/', function(req, res, next){
var hook = req.get('X-Github-Event'),
events_name = hook + '_' + req.body.action;
winston.info('Triggering event: ' + events_name);
pubsub.emit(events_name, req.body);
res.status(202).send('Thanks!');
});
app.get('/', function(req, res, next){
res.status(401).send('Your shoe\'s untied');
});
// Start server
httpServer = server.listen(app.get('port'), function() {
winston.info('Express server listening on port ' + app.get('port'));
});
// Load helpers
require('./helpers/checklist')({
'issues_opened': require(__dirname + '/configs/issue'),
'pull_request_opened': require(__dirname + '/configs/pullrequest')
});