-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
68 lines (54 loc) · 1.89 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
var express = require('express');
var app = express();
var https = require("https");
app.use(express.json());
app.use(express.urlencoded());
app.use(app.router);
var addLabel = function(app_name, issueNumber) {
console.log('Hooking '+ app_name + ', issue #'+issueNumber);
var username = process.env.GITHUB_NAME||'YOUR-GITHUB-USERNAME';
var password = process.env.GITHUB_PASSWORD||'YOUR-GITHUB-PASSWORD';
var appNameToMilestoneNumber = {
'copass-dev' : 11,
'copass' : 12
}
var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
var data = '{"milestone": '+(appNameToMilestoneNumber[app_name]||0)+'}';
var options = {
host: 'api.github.com',
path: '/repos/copass/copass/issues/'+issueNumber,
method: 'PATCH',
headers: {
'User-Agent': 'Copass-Hook',
'Authorization': auth,
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
}
};
var req = https.request(options, function(res) {
if (parseInt(res.headers.status) == 200) {
console.log('#'+issueNumber+' successfully hooked');
}
});
req.end(data);
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
}
app.get('/', function(req, res) {
res.send("<h1>Welcome to the Copass Hooks Apps</h1>");
res.send("<p>POST / with params app_name & git_log from heroku webhook will hook github repository to add labels</p>");
});
app.post('/', function(req, res) {
app_name = req.body.app;
matches = req.body.git_log.match(/(fixes?|closes?) #\d+/g);
for (var i = 0; i < matches.length; i++) {
issue_number = parseInt(matches[i].replace(/.* #/, ''));
addLabel(app_name, issue_number);
};
res.send('Ok, updated milestone to github for '+ matches.length + ' issues');
});
var port = process.env.PORT || 9000;
app.listen(port, function() {
console.log('Listening on port ' + port + ' in ' + app.get('env') + ' mode');
});