-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGWServer.js
126 lines (111 loc) · 3.47 KB
/
GWServer.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
/**
* GitLab CE Webhook Server v0.1.0
* GitLab CE local Webhook Server based on NodeJS to
* trigger some tasks when repo is updated.
* start this server by running `node GWServer.js`
*
*
* Author: Kushal Pandya <kushalspandya@gmail.com> (https://doublslash.com)
* Date: 23 May, 2016
* License: MIT
*
* Main Server Script.
*/
var config = require("./config.json"),
os = require("os"),
http = require("http"),
proc = require("child_process"),
port = config.listenerPort,
supportedHooks = Object.keys(config.hooks),
fnProcessRequest,
fnVerifyMatches,
server;
/**
* This method verifies conditions given in config[<hook_type>].matches against requestBody
*/
fnVerifyMatches = function(requestBody, matchesCollection) {
var matchItem;
for (matchItem in matchesCollection)
{
if (!requestBody.hasOwnProperty(matchItem))
return false;
else
{
if (requestBody[matchItem] === matchesCollection[matchItem])
continue;
else
return false;
}
}
return true;
};
/**
* This method does all the processing and command execution on requestBody.
*/
fnProcessRequest = function(requestBody) {
var object_kind = requestBody.object_kind,
satisfiesMatches = false,
pipedOutput = [],
errors = [],
commandBatch,
hookConfig,
i;
hookConfig = config.hooks[object_kind];
if (typeof hookConfig.matches === "object") // Check if 'matches' map is provided with this hook type.
satisfiesMatches = fnVerifyMatches(requestBody, hookConfig.matches); // Verify matches.
else
satisfiesMatches = true;
// Run commandBatch only if matches are satisfied.
if (satisfiesMatches)
{
// Beware, this is DANGEROUS.
commandBatch = proc.spawn(hookConfig.commandBatch);
// Collect Bash output.
commandBatch.stdout.on('data', function(data) {
pipedOutput.push(data);
});
// Collect Bash errors.
commandBatch.stderr.on('data', function(data) {
errors.push(data);
});
// Listen for end of commandBatch execution.
commandBatch.on('exit', function(status) {
if (status === 0) // Check if execution failed with non-Zero status
console.log(Buffer.concat(pipedOutput).toString()); // All good.
else
console.error('Hook Execution Terminated with status : %s \n', status, Buffer.concat(errors).toString());
});
}
};
server = http.createServer(function(request, response) {
var reqHeaders = request.headers,
reqBody = [];
request
.on('data', function(chunk) {
reqBody.push(chunk);
})
.on('end', function() {
reqBody = JSON.parse(Buffer.concat(reqBody).toString());
// Check if
// x-gitlab-event header is present in headers AND
// object_kind is one of the supported hooks in config
// then respond accordingly.
if (reqHeaders.hasOwnProperty('x-gitlab-event') &&
supportedHooks.indexOf(reqBody.object_kind) > -1)
{
response.statusCode = 200;
fnProcessRequest(reqBody);
}
else
response.statusCode = 400;
response.end();
});
});
server.listen(port, function() {
console.info("%s started on %s:%d at %s",
config.serverTitle,
os.hostname(),
port,
new Date()
);
});