This repository has been archived by the owner on Dec 4, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
110 lines (93 loc) · 3.08 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
/*eslint new-cap:0 */
'use strict';
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var path = require('path');
var linter = require('./linter');
var log = require('./log');
var options = {
accessToken: process.env.GITHUB_TOKEN,
port: process.env.VCAP_APP_PORT || 3000,
codeMargin: 7,
url: process.env.GHRR_URI || 'http://ghrr.gq:80/events'
};
if(!options.accessToken) {
throw 'No Github Access token specified.';
}
var github = require('./github')(options.accessToken);
var code = require('./code')(options.codeMargin);
var ioClient = require('socket.io-client')(options.url);
var lastError;
var lastSuccess;
server.listen(options.port);
app.use(express.static(path.join(__dirname, '/public')));
io.on('connection', function(socket) {
if(lastError) {
socket.emit('linterror', lastError);
}
if(lastSuccess) {
socket.emit('lintsuccess', lastSuccess);
}
});
function handlePushEvent(event) {
function checkRepo(callback) {
event.payload.commits.forEach(function (commit) {
github.patch(event.repo, commit, function (patch) {
callback(commit, linter.checkPatch(patch));
});
});
};
function handleError(commit, filename, error) {
github.file(event.repo.name, commit.sha, filename, function(file) {
lastError = {
actor: event.actor,
repo: event.repo,
commit: commit,
error: error,
filename: filename,
code: code.fragment(error.linenumber, file)
};
io.sockets.emit('linterror', lastError);
});
};
function handleCheck(commit, lintResults) {
if (lintResults.length === 0) {
log.skip(event.repo.name, commit.sha);
}
lintResults.forEach(function processLintResults(lintResult) {
if (lintResult.errors.length > 0) {
lintResult.errors.forEach(function(error) {
handleError(commit, lintResult.filename, error);
log.lintError(event.repo.name, commit.sha,
lintResult.filename, error);
});
} else {
lastSuccess = {
actor: event.actor,
repo: event.repo,
commit: commit,
};
io.sockets.emit('lintsuccess', lastSuccess);
log.success(event.repo.name, commit.sha, lintResult.filename);
}
});
};
github.languages(event.repo, function handleLanguage(languages) {
if('C++' in languages) {
checkRepo(handleCheck);
} else {
log.ignore(event.repo.name);
}
}, function() {
checkRepo(handleCheck);
});
};
ioClient.on('pushevent', handlePushEvent);
ioClient.on('connect', function() {
console.log('Connected to ' + options.url);
});
ioClient.on('connect_error', function(err) {
console.error(err);
});