Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
SixQuant committed Jan 8, 2016
1 parent 4df1254 commit 0037dd6
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

Modify rvagg/github-webhook-handler to fit Gitlab.

GitLab allows you to register **[Webhooks](https://developer.gitlab.com/webhooks/)** for your repositories. Each time an event occurs on your repository, whether it be pushing code, filling issues or creating pull requests, the webhook address you register can be configured to be pinged with details.
GitLab allows you to register **[Webhooks](https://gitlab.com/help/web_hooks/web_hooks)** for your repositories. Each time an event occurs on your repository, whether it be pushing code, filling issues or creating pull requests, the webhook address you register can be configured to be pinged with details.

This library is a small handler (or "middleware" if you must) for Node.js web servers that handles all the logic of receiving and verifying webhook requests from GitHub.


## Example

$ npm install gitlab-webhook-handler

```js
var http = require('http')
var createHandler = require('gitlab-webhook-handler')
Expand Down
78 changes: 78 additions & 0 deletions gitlab-webhook-handler.js
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
26 changes: 26 additions & 0 deletions package.json
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"
}
}

0 comments on commit 0037dd6

Please sign in to comment.