This repository was archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (31 loc) · 1.29 KB
/
index.js
File metadata and controls
42 lines (31 loc) · 1.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
const http = require('http')
const crypto = require('crypto')
const exec = require('child_process').exec
const config = require('./config.json')
http.createServer((req, res) => {
function closeWith (code, message = '') {
res.statusCode = code
res.statusMessage = message
return res.end()
}
if (req.method !== 'POST') return closeWith(405, 'Method Not Allowed')
let data = ''
req.on('data', chunk => data += chunk)
req.on('end', () => {
const json = JSON.parse(data)
if (json.zen) {
console.log('Ping! ' + json.zen)
return closeWith(200, 'OK')
}
const target = config.repos.find(r => r.name === json.repository.name && json.ref === 'refs/heads/' + r.branch)
if (!target) return closeWith(404, 'Not Found')
const signature = 'sha1=' + crypto.createHmac('sha1', target.secret).update(data).digest('hex')
if (req.headers['x-hub-signature'] !== signature) return closeWith(403, 'Forbidden')
if (req.headers['x-github-event'] !== 'push') return closeWith(304, 'Not Modified')
const script = target.script || config.script
exec(script.split('{path}').join(target.path), err => {
if (err) console.error(err)
})
return closeWith(200, 'OK')
})
}).listen(config.port, () => console.log('github-repo-sync online at :' + config.port))