-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgitlabhook.js
265 lines (227 loc) · 7.04 KB
/
gitlabhook.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
Rolf Niepraschk ([email protected])
Inspired by https://github.com/nlf/node-github-hook
*/
var Http = require('http');
var Url = require('url');
var Fs = require('fs');
var execFile = require('child_process').execFile;
var Path = require('path');
var Os = require('os');
var Tmp = require('temp'); Tmp.track();
var Util = require('util');
var inspect = Util.inspect;
var isArray = Util.isArray;
var GitLabHook = function(_options, _callback) {
if (!(this instanceof GitLabHook)) return new GitLabHook(_options, _callback);
var callback = null, options = null;
if (typeof _options === 'function') {
callback = _options;
} else {
callback = _callback;
options = _options;
}
options = options || {};
this.configFile = options.configFile || 'gitlabhook.conf';
this.configPathes = options.configPathes ||
['/etc/gitlabhook/', '/usr/local/etc/gitlabhook/', '.'];
this.port = options.port || 3420;
this.host = options.host || '0.0.0.0';
this.cmdshell = options.cmdshell || '/bin/sh';
this.keep = (typeof options.keep === 'undefined') ? false : options.keep;
this.logger = options.logger;
this.callback = callback;
var active = false, tasks;
if (typeof callback == 'function') {
active = true;
} else {
cfg = readConfigFile(this.configPathes, this.configFile);
if (cfg) {
this.logger.info('loading config file: ' + this.configFile);
this.logger.info('config file:\n' + Util.inspect(cfg));
for (var i in cfg) {
if (i == 'tasks' && typeof cfg.tasks == 'object' &&
Object.keys(cfg.tasks).length) {
this.tasks = cfg.tasks;
active = true;
} else {
this[i] = cfg[i];
}
}
} else {
this.logger.error("can't read config file: ", this.configFile);
}
}
this.logger = this.logger || { info: function(){}, error: function(){} };
this.logger.info('self: ' + inspect(this) + '\n');
if (active) this.server = Http.createServer(serverHandler.bind(this));
};
GitLabHook.prototype.listen = function(callback) {
var self = this;
if (typeof self.server !== 'undefined') {
self.server.listen(self.port, self.host, function () {
self.logger.info(Util.format(
'listening for github events on %s:%d', self.host, self.port));
if (typeof callback === 'function') callback();
});
} else {
self.logger.info('server disabled');
}
};
function readConfigFile(pathes, file) {
var fname, ret = false;
for (var i=0;i<pathes.length;i++) {
fname = Path.join(pathes[i], file);
try {
var data = Fs.readFileSync(fname, 'utf-8');
ret = parse(data);
break;
} catch(err) {
}
}
return ret;
}
function parse(data) {
var result;
try {
result = JSON.parse(data);
} catch (e) {
result = false;
}
return result;
}
function reply(statusCode, res) {
var headers = {
'Content-Length': 0
};
res.writeHead(statusCode, headers);
res.end();
}
function executeShellCmds(self, address, data) {
var repo = data.repository.name.replace(/[&|;$`]/gi, "");
var lastCommit = data.commits ? data.commits[data.commits.length-1] : null;
var map = {
'%r': repo,
'%k': data.object_kind,
'%g': data.repository ? data.repository.git_ssh_url : '',
'%h': data.repository ? data.repository.git_http_url : '',
'%u': data.user_name,
'%b': data.ref,
'%i': lastCommit ? lastCommit.id : '',
'%t': lastCommit ? lastCommit.timestamp : '',
'%m': lastCommit ? lastCommit.message : '',
'%s': address
};
function execute(path, idx) {
if (idx == cmds.length) {
if (!self.keep) {
self.logger.info('Remove working directory: ' + self.path);
Tmp.cleanup();
} else {
self.logger.info('Keep working directory: ' + self.path);
}
return;
}
var fname = Path.join(path, 'task-' + pad(idx, 3));
Fs.writeFile(fname, cmds[idx], function (err) {
if (err) {
self.logger.error('File creation error: ' + err);
return;
}
self.logger.info('File created: ' + fname);
execFile(self.cmdshell, [ fname ], { cwd:path, env:process.env },
function (err, stdout, stderr) {
if (err) {
self.logger.error('Exec error: ' + err);
} else {
self.logger.info('Executed: ' + self.cmdshell + ' ' + fname);
process.stdout.write(stdout);
}
process.stderr.write(stderr);
execute(path, ++idx);
});
});
}
var cmds = getCmds(self.tasks, map, repo);
if (cmds.length > 0) {
self.logger.info('cmds: ' + inspect(cmds) + '\n');
Tmp.mkdir({dir:Os.tmpDir(), prefix:'gitlabhook.'}, function(err, path) {
if (err) {
self.logger.error(err);
return;
}
self.path = path;
self.logger.info('Tempdir: ' + path);
var i = 0;
execute(path, i);
});
} else {
self.logger.info('No related commands for repository "' + repo + '"');
}
}
function serverHandler(req, res) {
var self = this;
var url = Url.parse(req.url, true);
var buffer = [];
var bufferLength = 0;
var failed = false;
var remoteAddress = req.ip || req.socket.remoteAddress ||
req.socket.socket.remoteAddress;
req.on('data', function (chunk) {
if (failed) return;
buffer.push(chunk);
bufferLength += chunk.length;
});
req.on('end', function (chunk) {
if (failed) return;
var data;
if (chunk) {
buffer.push(chunk);
bufferLength += chunk.length;
}
self.logger.info(Util.format('received %d bytes from %s\n\n', bufferLength,
remoteAddress));
data = Buffer.concat(buffer, bufferLength).toString();
data = parse(data);
// invalid json
if (!data || !data.repository || !data.repository.name) {
self.logger.error(Util.format('received invalid data from %s, returning 400\n\n',
remoteAddress));
return reply(400, res);
}
var repo = data.repository.name.replace(/[&|;$`]/gi, "");
reply(200, res);
self.logger.info(Util.format('got event on %s:%s from %s\n\n', repo, data.ref,
remoteAddress));
self.logger.info(Util.inspect(data, { showHidden: true, depth: 10 }) + '\n\n');
if (typeof self.callback == 'function') {
self.callback(data);
} else {
executeShellCmds(self, remoteAddress, data);
}
});
// 405 if the method is wrong
if (req.method !== 'POST') {
self.logger.error(Util.format('got invalid method from %s, returning 405',
remoteAddress));
failed = true;
return reply(405, res);
}
}
function getCmds(tasks, map, repo) {
var ret = [], x = [];
if (tasks.hasOwnProperty('*')) x.push(tasks['*']);
if (tasks.hasOwnProperty(repo)) x.push(tasks[repo]);
for (var i=0; i<x.length; i++) {
var cmdStr = (isArray(x[i])) ? x[i].join('\n') : x[i];
for (var j in map) cmdStr = cmdStr.replace(new RegExp(j, 'g'), map[j]);
ret.push(cmdStr + '\n');
}
return ret;
}
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
module.exports = GitLabHook;