Skip to content
This repository was archived by the owner on Jan 6, 2021. It is now read-only.

Commit 58c68c7

Browse files
committed
All works
1 parent 156b284 commit 58c68c7

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
.c9revisions/
2+
test/
3+
config/test.json
4+
response.txt
5+
16
lib-cov
27
*.seed
38
*.log

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Config parameters:
2424
* user - System user, from which will ne performed commands
2525
* path - root path for project
2626
* commands - Shell commands, which will be performed after receive hook
27+
* refs - Non-required, if ref not match, any operation will not be performed. String or Array of Strings which be substituted to ref.match()
2728

2829

2930
TODO

app.js

+23-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ process.env.PORT = process.env.PORT || 6666;
33
//Rewrite for logging to files
44
var dbg = true;
55
function log(msg, file){
6-
if(dbg && file){
6+
if(file){
77
//Write to file
88
} else {
99
console.log(msg);
10+
if(dbg && typeof msg == 'object'){
11+
console.dir(msg);
12+
}
1013
}
1114

1215
return false;
@@ -37,7 +40,7 @@ var processFile = function(fileName){
3740
throw new Error('Bad config file "' + filePath + '". It need to be json object with path, user and commands keys.');
3841
}
3942
} catch(e) {
40-
return log(e);
43+
return log('Error while processing file "' + filePath + '": ' + e);
4144
}
4245
//Populate good cfg object to objects map by filename without extension
4346
return cfg_map[path.basename(fileName, fileExt)] = cfg;
@@ -82,6 +85,9 @@ http.createServer(function(request, response) {
8285
});
8386

8487
request.on('end', function () {
88+
var bodyObj = JSON.parse(body.trim())
89+
log(body);
90+
8591
var cfg = cfg_map[request.url];
8692
var spawn_options = {
8793
encoding: "utf-8",
@@ -92,14 +98,28 @@ http.createServer(function(request, response) {
9298
if(!fs.readdirSync(cfg.path)){
9399
return log('Invalid path "' + cfg.path + '" in config "' + request.url + '"');
94100
}
95-
spawn_options.path = cfg.path;
101+
spawn_options.cwd = cfg.path;
102+
103+
var refsType = typeof cfg.refs;
104+
if(['string', 'object'].indexOf(refsType = typeof cfg.refs)){
105+
if(refsType == 'string') cfg.refs = [cfg.refs];
106+
var cont = false;
107+
for(var key in cfg.refs){
108+
if(bodyObj.ref.match(cfg.refs[key])) {
109+
cont = true;
110+
break;
111+
}
112+
}
113+
if(!cont) return log('No refs match. Aborting.');
114+
}
96115

97116
if(cfg.commands.length){
98117
var handleExec = function(err, stdout, stderr) {
99118
if(err){log(err);}
100119

101120

102121
};
122+
103123
for(var i in cfg.commands){
104124
var commandArray = cfg.commands[i];
105125
var commandString = commandArray.join(' ');
@@ -115,7 +135,6 @@ http.createServer(function(request, response) {
115135
}
116136
}
117137
//Do work according to hook data
118-
log(body);
119138
});
120139
}
121140
// on every request, we'll output 'Hello world'

config/sample.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
{
22
"path": "/path/to/project",
3+
"refs": [
4+
"refs/heads/*"
5+
],
36
"user": "shell-user",
47
"commands": [
58
["git", "fetch", "--all"],
6-
["git", "reset", "--hard", "origin/master"],
9+
["git", "reset", "--hard", "origin/master"]
710
]
8-
}
11+
}

0 commit comments

Comments
 (0)