-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const EventEmitter = require('events').EventEmitter | ||
, inherits = require('util').inherits | ||
, bl = require('bl') | ||
|
||
function create (options) { | ||
if (typeof options != 'object') | ||
throw new TypeError('must provide an options object') | ||
|
||
if (typeof options.path != 'string') | ||
throw new TypeError('must provide a \'path\' option') | ||
|
||
// make it an EventEmitter, sort of | ||
handler.__proto__ = EventEmitter.prototype | ||
EventEmitter.call(handler) | ||
|
||
return handler | ||
|
||
function handler (req, res, callback) { | ||
if (req.url.split('?').shift() !== options.path) | ||
return callback() | ||
|
||
function hasError (msg) { | ||
res.writeHead(400, { 'content-type': 'application/json' }) | ||
res.end(JSON.stringify({ error: msg })) | ||
|
||
var err = new Error(msg) | ||
|
||
handler.emit('error', err, req) | ||
callback(err) | ||
} | ||
|
||
var event = req.headers['x-gitlab-event']; | ||
|
||
if (!event) | ||
return hasError('No X-Gitlab-Event found on request') | ||
|
||
req.pipe(bl(function (err, data) { | ||
if (err) { | ||
return hasError(err.message) | ||
} | ||
|
||
var obj | ||
|
||
try { | ||
obj = JSON.parse(data.toString()) | ||
} catch (e) { | ||
return hasError(e) | ||
} | ||
|
||
// console.log(obj); | ||
var event = obj.object_kind; | ||
|
||
// invalid json | ||
if (!obj || !obj.repository || !obj.repository.name) { | ||
return hasError('received invalid data from ' + req.headers['host'] + ', returning 400'); | ||
} | ||
|
||
var repo = obj.repository.name; | ||
|
||
res.writeHead(200, { 'content-type': 'application/json' }) | ||
res.end('{"ok":true}') | ||
|
||
var emitData = { | ||
event : event | ||
, payload : obj | ||
, protocol: req.protocol | ||
, host : req.headers['host'] | ||
, url : req.url | ||
} | ||
|
||
handler.emit(event, emitData) | ||
handler.emit('*', emitData) | ||
})) | ||
} | ||
} | ||
|
||
|
||
module.exports = create |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "gitlab-webhook-handler", | ||
"version": "1.0.1", | ||
"description": "Web handler / middleware for processing GitLab Webhooks", | ||
"main": "gitlab-webhook-handler.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/caviler/gitlab-webhook-handler.git" | ||
}, | ||
"keywords": [ | ||
"gitlab", | ||
"webhooks" | ||
], | ||
"author": "Eric Xu <[email protected]>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/caviler/gitlab-webhook-handler/issues" | ||
}, | ||
"homepage": "https://github.com/caviler/gitlab-webhook-handler#readme", | ||
"dependencies": { | ||
"bl": "~0.9.4" | ||
} | ||
} |