-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcherryhook.js
executable file
·87 lines (80 loc) · 2.39 KB
/
cherryhook.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#! /usr/bin/env node
var quere = require('queue-async');
var express = require('express');
var bodyParser = require('body-parser');
var task = require('queue-async')();
var path = require('path');
var cp = require('child_process');
var app = express();
app.use(bodyParser.json());
var config, listener;
var _reload_config = function(){
if (typeof config !== 'undefined'){
var pwd = path.resolve() + 'config.js';
delete require.cache[pwd];
}
config = require('./config.json');
listener = {};
for (var itemid in config.items){
var item = config.items[itemid];
for (var resid in item){
var res = item[resid];
var name = res.name,
branch = res.branch,
actions = res.actions;
if (typeof listener[name] === 'undefined'){
listener[name] = {};
}
for (var actionType in res.actions){
var action = res.actions[actionType];
var arrTask = config.scripts[res.actions[actionType]];
if (typeof arrTask === 'undefined'){
console.log('WARNING: task [' + res.actions[actionType] + '] in ' +
name + 'not found');
continue;
}
if (typeof listener[name][actionType] === 'undefined'){
listener[name][actionType] = {};
}
if (typeof listener[name][actionType][branch] === 'undefined'){
listener[name][actionType][branch] = [];
}
for (var i in arrTask){
listener[name][actionType][branch].push(arrTask[i]);
}
}
}
}
};
_reload_config();
var port = config.port;
app.listen(port);
console.log('Listening on port ' + port);
var _runCMDcb = function(error, stdout, stderr){
if (error){
console.log(error.toString());
return false;
}else{
console.log(stdout);
return true;
}
};
app.post('*', function(req, res){
res.send(202);
task.defer(function(req, res){
var eType = req.headers["x-github-event"];
var body = req.body;
var branch = body.ref.split('/')[2];
var name = body.repository.name;
var actions = (listener[name] && listener[name][eType] && listener[name][eType][branch]);
if (typeof actions === 'undefined'){
console.log('INFO: ' + name + ':' + branch + ' got a ' + eType + ' trigger but no action fount.');
return;
}
for (var i in actions){
console.log('INFO: ' + name + ':' + branch + ' triggered script ' + actions[i]);
var dirname = path.dirname(path.resolve(actions[i]));
cp.execFile(actions[i],[dirname],{}, _runCMDcb);
}
}, req, res).await();
});