-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
52 lines (42 loc) · 1.38 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
const { exec } = require("child_process");
const http = require('http')
const fs = require("fs")
var createHandler = require('github-webhook-handler')
function fatalError(string) {
console.log()
console.warn("\x1b[31m"+ string +"\x1b[0m")
console.log()
process.exit()
}
var config;
if (fs.existsSync("./config.json")) {
config = JSON.parse(fs.readFileSync('./config.json', 'utf8'))
}
else {
fatalError("No config file found, exiting!")
}
if (!config.secret || !config.repos) {
fatalError("Config file missing secret or repos!")
}
var handler = createHandler({ path: '/', secret: config.secret })
http.createServer(function (req, res) {
handler(req, res, function (err) {
res.statusCode = 404
res.end('no such location')
})
}).listen(3001, ()=>{
console.log("Listening for push events on: ", Object.keys(config.repos).join(", "))
})
handler.on('error', function (err) {
console.error('Rejected POST:', err.message)
})
handler.on('push', function (event) {
console.log(event.payload.repository.full_name, "PULL")
if (config.repos[event.payload.repository.full_name] && !config.devMode) {
exec(config.repos[event.payload.repository.full_name].command)
console.log(config.repos[event.payload.repository.full_name].command, "executed")
}
else {
console.log(`Error: Repo auth valid, but repo "${event.payload.repository.full_name}" not in config!`)
}
})