Skip to content

Instantly share code, notes, and snippets.

@eugeneius
Created October 19, 2014 02:36
Show Gist options
  • Save eugeneius/66a94644dfe9c1b07cca to your computer and use it in GitHub Desktop.
Save eugeneius/66a94644dfe9c1b07cca to your computer and use it in GitHub Desktop.
Git post-commit hook that sends an approximation of GitHub's 'push' webhook after every commit. Helpful for testing code that consumes those webhooks.
#!/usr/bin/env ruby
# Git post-commit hook that sends an approximation of GitHub's 'push' webhook
# after every commit. Helpful for testing code that consumes those webhooks.
#
# To set this up:
# 1. Create a local repo where you'll make test commits
# 2. Copy this script to .git/hooks/post-commit
# 3. Make it executable: `chmod +x .git/hooks/post-commit`
# 4. Change the `uri` variable below to where webhooks should be sent
# 5. Make some commits and your app will receive webhook-like requests
#
# Note: this sends the webhook as form-encoded data, not JSON.
require 'json'
require 'net/http'
require 'uri'
uri = 'http://my-app.dev/github/post-receive' # Add your webhook URL here
ref = `git rev-parse --symbolic-full-name HEAD`.strip
before = `git rev-list --max-count=1 HEAD~1`.strip
after = `git rev-list --max-count=1 HEAD`.strip
repository_url = `pwd`.strip
commits = [{
message: `git log --max-count=1 --format='%B' HEAD`.strip,
author: {
name: `git show --no-patch --format='%an' HEAD`.strip
}
}]
payload = {
ref: ref,
before: before,
after: after,
commits: commits,
repository: {
url: repository_url
}
}
Net::HTTP.post_form(URI.parse(uri), { payload: payload.to_json })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment