-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·66 lines (64 loc) · 1.99 KB
/
app.js
File metadata and controls
executable file
·66 lines (64 loc) · 1.99 KB
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
const { createServer } = require('http');
const port = process.env.GITHUB_WEBHOOK_PORT || '3000';
const secret = process.env.GITHUB_WEBHOOK_SECRET || '';
const server = createServer((req, res) => {
if('POST' === req.method){
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
if('' !== secret){
const { createHmac } = require('crypto');
let signature = createHmac('sha1', secret).update(body).digest('hex');
if(req.headers['x-hub-signature'] !== `sha1=${signature}`){
console.log('Signature Error');
res.statusCode = 403;
res.end();
return;
}
}
try{
body = JSON.parse(decodeURIComponent(body).replace(/^payload=/, ''));
}catch(e){
console.log(e)
}
if('object' === typeof body){
if('refs/heads/master' === body.ref){
console.log(`${body.repository.name} master`);
const { exec } = require('child_process');
const command = `cd ../${body.repository.name} && git pull origin master`;
exec(command, (error, stdout, stderr) => {
if(error){
console.log('updated unsuccessfully');
console.log(error);
res.statusCode = 500;
res.end('Error');
}else{
console.log(stdout);
console.log('updated successfully');
res.statusCode = 200;
res.end('Success');
}
});
}else{
console.log(`${body.repository.name} ${body.ref} was pushed`);
res.statusCode = 200;
res.end('Nothing');
}
}else{
console.log('Body cannot be parsed to an object');
console.log(body);
res.statusCode = 400;
res.end();
}
});
}else{
console.log('Unexpected get request');
res.statusCode = 403;
res.end();
}
})
server.listen(port, () => {
console.log(`Listening on ${port}`);
});