-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
119 lines (116 loc) · 3.29 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
var http = require('http');
var https = require('https');
var port = process.env.PORT || 3000;
var apiKey = process.env.GITHUB_API_KEY;
if (!apiKey) {
throw new Error("GITHUB_API_KEY not set");
}
var makeComment = function(issueNumberOrSha, commentType, bodyMsg, cb) {
var postData = JSON.stringify({ body: bodyMsg });
var options = {
hostname: 'api.github.com',
port: 443,
method: 'POST',
headers: {
'Authorization': 'token ' + apiKey,
'Content-Type': 'application/json',
'Content-Length': postData.length,
'User-Agent': 'CodeCracker Bot'
}
};
if (commentType === 'issue') {
options.path = '/repos/code-cracker/code-cracker/issues/' + issueNumberOrSha + '/comments';
} else if (commentType === 'commit') {
options.path = '/repos/code-cracker/code-cracker/commits/' + issueNumberOrSha + '/comments';
}
var error = "";
var callbackCalled = false;
var req = https.request(options, function(res) {
if (res.statusCode != 201) {
error = "Response was not 201, but " + res.statusCode + ". Headers: " + JSON.stringify(res.headers) + "\nData:";
res.setEncoding('utf8');
res.on('data', function(data) {
error += data;
});
res.on('end', function() {
if (!callbackCalled) {
callbackCalled = true;
cb(new Error(error));
}
});
} else {
if (!callbackCalled) {
callbackCalled = true;
cb();
}
}
});
req.write(postData);
req.end();
req.on('error', function(err) {
if (!callbackCalled) {
callbackCalled = true;
cb(err);
}
});
}
http.createServer(function (req, res) {
if (req.method === 'POST' & req.url.toUpperCase() === '/MESSAGE') {
try {
var bodyMsg = "";
req.on('data', function (chunk) {
bodyMsg += chunk;
});
req.on('end', function () {
if (!bodyMsg) {
res.writeHead(500);
res.end('No body specified.');
return;
}
try {
var parsedBodyMsg = JSON.parse(bodyMsg);
} catch (e) {
res.writeHead(500);
res.end('Only Json allowed.');
return;
}
if (!parsedBodyMsg.body) {
res.writeHead(500);
res.end('Body missing in json.');
return;
}
if (!parsedBodyMsg.issueNumber & !parsedBodyMsg.sha) {
res.writeHead(500);
res.end('Issue number and sha missing in json.');
return;
}
if (parsedBodyMsg.issueNumber) {
var commentType = 'issue';
var issueNumberOrSha = parsedBodyMsg.issueNumber;
} else {
var commentType = 'commit';
var issueNumberOrSha = parsedBodyMsg.sha;
}
makeComment(issueNumberOrSha, commentType, parsedBodyMsg.body, function(err){
if (err) {
console.log(err);
res.writeHead(500);
res.end('Error sending request to Github.');
}
else {
res.writeHead(201);
res.end();
}
});
});
} catch (err) {
console.log("Unexpected error: " + err);
res.writeHead(500);
res.end();
}
return;
}
res.writeHead(404);
res.end();
}).listen(port);
console.log('Server running at http://127.0.0.1:' + port + '/');