Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for application/x-www-form-urlencoded content type webhooks #68

Merged
merged 2 commits into from
Aug 27, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions files/webhook
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# Jeff Malnick

require 'rubygems'

require 'sinatra/base'
require 'webrick'
require 'webrick/https'
Expand All @@ -17,6 +16,7 @@ require 'mcollective'
require 'resolv'
require 'json'
require 'yaml'
require 'cgi'
include MCollective::RPC


Expand Down Expand Up @@ -60,15 +60,26 @@ class Server < Sinatra::Base
# Simulate a github post:
# curl -d '{ "ref": "refs/heads/production" }' -H "Accept: application/json" 'https://puppet:puppet@localhost:8088/payload' -k -q
#
# Somulate a stash post:
# Simulate a stash post:
# curl -d '{ "refChanges":[ { "refId":"refs/heads/production" } ] }' -H "Accept: application/json" 'https://puppet:puppet@localhost:8088/payload' -k -q
#
# Simulate a Gitorious post:
# curl -X POST -d '%7b%22ref%22%3a%22master%22%7d' 'http://puppet:puppet@localhost:8088/payload' -q
# Yes, Gitorious does not support https...

post '/payload' do
protected! if $config['protected']
$logger.info("authenticated: #{$config['user']}")
request.body.rewind # in case someone already read it
data = JSON.parse(request.body.read, :quirks_mode => true)

# Check if content type is x-www-form-urlencoded
if request.content_type.to_s.downcase.eql?('application/x-www-form-urlencoded')
decoded = CGI::unescape(request.body.read).gsub(/^payload\=/,'')
else
decoded = request.body.read
end
data = JSON.parse(decoded, :quirks_mode => true)

# github sends a 'ref', stash sends an array in 'refChanges'
branch = ( data['ref'] || data['refChanges'][0]['refId'] ).split("/").last

Expand Down