├── LICENSE ├── README.md ├── gitlab-webhook-handler.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 caviler 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gitlab-webhook-handler 2 | 3 | [![NPM](https://nodei.co/npm/gitlab-webhook-handler.png?downloads=true&downloadRank=true)](https://nodei.co/npm/gitlab-webhook-handler/) 4 | [![NPM](https://nodei.co/npm-dl/gitlab-webhook-handler.png?months=6&height=3)](https://nodei.co/npm/gitlab-webhook-handler/) 5 | 6 | Modify **[rvagg/github-webhook-handler](https://github.com/rvagg/github-webhook-handler)** to fit Gitlab. 7 | 8 | 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. 9 | 10 | 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. 11 | 12 | 13 | ## Example 14 | 15 | $ npm install gitlab-webhook-handler 16 | 17 | ```js 18 | var http = require('http') 19 | var createHandler = require('gitlab-webhook-handler') 20 | var handler = createHandler({ path: '/webhook' }) 21 | 22 | http.createServer(function (req, res) { 23 | handler(req, res, function (err) { 24 | res.statusCode = 404 25 | res.end('no such location') 26 | }) 27 | }).listen(7777) 28 | 29 | console.log("Gitlab Hook Server running at http://0.0.0.0:7777/webhook"); 30 | 31 | handler.on('error', function (err) { 32 | console.error('Error:', err.message) 33 | }) 34 | 35 | handler.on('push', function (event) { 36 | console.log('Received a push event for %s to %s', 37 | event.payload.repository.name, 38 | event.payload.ref) 39 | }) 40 | 41 | handler.on('issues', function (event) { 42 | console.log('Received an issue event for %s action=%s: #%d %s', 43 | event.payload.repository.name, 44 | event.payload.action, 45 | event.payload.issue.number, 46 | event.payload.issue.title) 47 | }) 48 | ``` 49 | 50 | ## 中文使用说明 51 | **[使用 GitHub / GitLab 的 Webhooks 进行网站自动化部署](http://www.lovelucy.info/auto-deploy-website-by-webhooks-of-github-and-gitlab.html)** -------------------------------------------------------------------------------- /gitlab-webhook-handler.js: -------------------------------------------------------------------------------- 1 | const EventEmitter = require('events').EventEmitter 2 | , inherits = require('util').inherits 3 | , bl = require('bl') 4 | 5 | function create (options) { 6 | if (typeof options != 'object') 7 | throw new TypeError('must provide an options object') 8 | 9 | if (typeof options.path != 'string') 10 | throw new TypeError('must provide a \'path\' option') 11 | 12 | // make it an EventEmitter, sort of 13 | handler.__proto__ = EventEmitter.prototype 14 | EventEmitter.call(handler) 15 | 16 | return handler 17 | 18 | function handler (req, res, callback) { 19 | if (req.url.split('?').shift() !== options.path) 20 | return callback() 21 | 22 | function hasError (msg) { 23 | res.writeHead(400, { 'content-type': 'application/json' }) 24 | res.end(JSON.stringify({ error: msg })) 25 | 26 | var err = new Error(msg) 27 | 28 | handler.emit('error', err, req) 29 | callback(err) 30 | } 31 | 32 | var event = req.headers['x-gitlab-event']; 33 | 34 | if (!event) 35 | return hasError('No X-Gitlab-Event found on request') 36 | 37 | req.pipe(bl(function (err, data) { 38 | if (err) { 39 | return hasError(err.message) 40 | } 41 | 42 | var obj 43 | 44 | try { 45 | obj = JSON.parse(data.toString()) 46 | } catch (e) { 47 | return hasError(e) 48 | } 49 | 50 | // console.log(obj); 51 | var event = obj.object_kind; 52 | 53 | // invalid json 54 | if (!obj || !obj.repository || !obj.repository.name) { 55 | return hasError('received invalid data from ' + req.headers['host'] + ', returning 400'); 56 | } 57 | 58 | var repo = obj.repository.name; 59 | 60 | res.writeHead(200, { 'content-type': 'application/json' }) 61 | res.end('{"ok":true}') 62 | 63 | var emitData = { 64 | event : event 65 | , payload : obj 66 | , protocol: req.protocol 67 | , host : req.headers['host'] 68 | , url : req.url 69 | } 70 | 71 | handler.emit(event, emitData) 72 | handler.emit('*', emitData) 73 | })) 74 | } 75 | } 76 | 77 | 78 | module.exports = create 79 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gitlab-webhook-handler", 3 | "version": "1.0.1", 4 | "description": "Web handler / middleware for processing GitLab Webhooks", 5 | "main": "gitlab-webhook-handler.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/caviler/gitlab-webhook-handler.git" 12 | }, 13 | "keywords": [ 14 | "gitlab", 15 | "webhooks" 16 | ], 17 | "author": "Eric Xu ", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/caviler/gitlab-webhook-handler/issues" 21 | }, 22 | "homepage": "https://github.com/caviler/gitlab-webhook-handler#readme", 23 | "dependencies": { 24 | "bl": "~0.9.4" 25 | } 26 | } 27 | --------------------------------------------------------------------------------