-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (56 loc) · 1.78 KB
/
index.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
const winston = require('winston');
const request = require('request');
const express = require('express')
const config = require('config');
const bodyParser = require('body-parser')
const fs = require('fs-extra');
const path = require('path');
const app = express();
const logger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.prettyPrint()
),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'logs/combined.log' })
]
});
const sendCommand = (resource, action, eventBody) => request({
uri: `${config.get('antonUrl')}/c/${resource}/${action}`,
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(eventBody)
});
app.use(bodyParser.json())
app.post('/', (req, res) => {
const event = `${req.headers['x-github-event']}${req.body.action ? '_' + req.body.action : ''}`;
const outputPath = path.resolve(
__dirname,
'events',
req.headers['x-github-event'],
req.body.action || '',
`${new Date().getTime()}.json`
);
logger.log('info', `event, ${event}, ${outputPath.split('/').reverse()[0]}`);
if (process.env.NODE_ENV === 'development') {
fs.outputFileSync(outputPath, JSON.stringify(req.body), 'utf8');
}
switch (event) {
case 'pull_request_opened':
return sendCommand('pull-request', 'open', req.body);
case 'pull_request_assigned':
if (req.body)
return sendCommand('pull-request', 'assign', req.body);
case 'pull_request_closed':
if (req.body)
return sendCommand('pull-request', 'close', req.body);
default:
break;
}
res.status(200).send('ok');
})
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Isla listening on port ${port}!`))