Express middleware for validating Github webhooks
Clone or download
gmaclennan Merge pull request #1 from mvines/master
Add option to override the default webhook payload body size of 100kb
Latest commit 66dd4ba Mar 17, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis.yml Expand tests for other node versions May 21, 2016
README.md Merge pull request #1 from mvines/master Mar 18, 2017
index.js Add option to override the default webhook payload body size of 100kb Feb 4, 2016
package.json Add tests to README Jan 7, 2015
test.js Add tests Jan 7, 2015

README.md

Build Status

Github Webhook Middleware

This middleware parses a Github webhook and validates the signature as documented https://developer.github.com/webhooks/#payloads

You will need to set a secret when you create the webhook and pass the same secret as options.secret for validation.

The Github webhook payload will be accessible via req.body

Borrows ideas and code from https://github.com/developmentseed/jekyll-hook/ and https://github.com/rvagg/github-webhook-handler

Installation

npm install github-webhook-middleware --save

Run tests

npm test

Usage

var express = require('express');
var app     = express();
var githubMiddleware = require('github-webhook-middleware')({
  secret: process.env.GITHUB_SECRET,
  limit: '1mb', // <-- optionally include the webhook json payload size limit, useful if you have large merge commits.  Default is '100kb'
});

app.post('/hooks/github/', githubMiddleware, function(req, res) {
  // Only respond to github push events
  if (req.headers['x-github-event'] != 'push') return res.status(200).end();

  var payload = req.body
    , repo    = payload.repository.full_name
    , branch  = payload.ref.split('/').pop();
  
  var textFiles = getChangedFiles(payload.commits, /.*\.txt$/);
});


// The Github push event returns an array of commits.
// Each commit object has an array of added, modified and deleted files.
// getChangedFiles() returns a list of all the added and modified files
// excluding any files which are subsequently removed.
function getChangedFiles(commits, matchRegex) {
  return commits
    .reduce(function(previousCommit, currentCommit) {
      return previousCommit
        .concat(currentCommit.modified)
        .concat(currentCommit.added)
        .filter(function(value) {
          return currentCommit.removed.indexOf(value) === -1;
        });
    }, [])
    .filter(function(value, i, arr) {
      return arr.indexOf(value) >= i && matchRegex.test(value);
    });
}

Contributing

Pull requests welcome.

Release History

  • 0.0.1 Initial release