This repository was archived by the owner on Mar 12, 2023. It is now read-only.
forked from velopert/nodejs-github-webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·68 lines (55 loc) · 2.29 KB
/
index.js
File metadata and controls
executable file
·68 lines (55 loc) · 2.29 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
67
68
const fs = require('fs');
const http = require('http');
const spawn = require('child_process').spawn;
const execSync = require('child_process').execSync
const crypto = require('crypto');
require('dotenv').config();
const webhookSecret = process.env.WEBHOOK_SECRET;
const port = 8081;
const contenType = {"Content-Type": "application/json"};
const LOGS = [];
function log(msg){
console.log(msg);
LOGS.push(msg);
}
http.createServer(function(req, res){
log("request received");
res.writeHead(400, contenType);
if(req.method != 'POST'){
const data = JSON.stringify({"error": "invalid request, expecting POST"});
return res.end(data);
}
let jsonString = '';
req.on('data', function(data){
jsonString += data;
});
req.on('end', function(){
const hash = "sha1=" + crypto.createHmac('sha1', webhookSecret).update(jsonString).digest('hex');
if(hash != req.headers['x-hub-signature']){
log('invalid key');
const data = JSON.stringify({success: false, msg: "invalid key", key: hash});
return res.end(data);
}
log('deleting old logs');
const keepXLatest = 10;
execSync(`find . -name "*.*.log" | head -n +${keepXLatest} | xargs rm -f`);
const nextFileNumber = Number(execSync('find . -name "*.*.log" | tail -n 1 | grep -oE "\d+" || echo "0"')) + 1;
log('archiving current logs at ' + nextFileNumber);
execSync(`mv out.log out.${nextFileNumber}.log || echo "creating logs"`);
execSync(`mv err.log err.${nextFileNumber}.log || echo "creating logs"`);
const out = fs.openSync('out.log', 'a');
const err = fs.openSync('err.log', 'a');
let jsonPayload = JSON.parse(jsonString)
let repoMatch = jsonPayload.repository.full_name.match(/signcollector\/signcollector-application-config-(\w+)/)
let envName = "staging"
if(repoMatch && repoMatch[1]){
envName = repoMatch[1];
}
log("running deploy_hook.sh " + envName);
const deploySh = spawn('bash', ['deploy_hook.sh', envName], {detached: true, stdio: ['ignore', out, err]});
deploySh.unref();
res.writeHead(200, contenType);
const data = JSON.stringify({success: true, env: envName, logs: LOGS.join('\n')});
return res.end(data);
});
}).listen(port);