Skip to content

Commit

Permalink
Use POST /:project for github post-receive [integrity#15 state:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
foca committed Jul 20, 2008
1 parent 9c00b71 commit fbea5eb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
30 changes: 19 additions & 11 deletions lib/integrity/github.rb
@@ -1,26 +1,34 @@
require 'rubygems'
require 'json'

require File.dirname(__FILE__) + '/../integrity'
require 'sinatra'
require 'json'

configure do
Integrity.new
end

include Integrity

post '/' do
post '/:project' do
content_type 'text/plain'

project = Project.first(:permalink => params[:project])
unknown_project! if project.nil?

begin
payload = JSON.parse(params[:payload] || '')
unless project = Project.first(:name => payload['repository']['name'])
throw :halt, [400, "Unknown project `#{payload['repository']['name']}'"]
else
payload['commits'].each_key { |commit| project.build(commit) }
end
payload = JSON.parse(params[:payload] || "")
payload['commits'].each_key { |commit| project.build(commit) }
'Thanks, build started.'
rescue JSON::ParserError => exception
throw :halt, [422, exception.to_s]
invalid_payload!(exception.to_s)
end
end

helpers do
def unknown_project!
throw :halt, [404, "Unknown project `#{params[:project]}'"]
end

def invalid_payload!(msg=nil)
throw :halt, [422, msg || 'No payload given']
end
end
40 changes: 18 additions & 22 deletions spec/github_spec.rb
Expand Up @@ -41,67 +41,63 @@ def payload
EOS
end

def do_post
post_it '/', :payload => payload
end

before(:each) do
require File.dirname(__FILE__) + '/../lib/integrity/github'
require Integrity.root / "lib" / "integrity" / "github"
Integrity.stub!(:new)
@project = mock('project', :build => true)
Integrity::Project.stub!(:first).and_return(@project)
end

it 'should be successful' do
do_post
@response.should be_ok
post_it '/github', :payload => payload
status.should == 200
end

it 'should return a confirmation message' do
do_post
@response.body.should == 'Thanks, build started.'
post_it '/github', :payload => payload
body.should == 'Thanks, build started.'
end

it 'should be 422 without payload' do
post_it '/'
@response.status.should == 422
post_it '/github'
status.should == 422
end

it 'should find the Project by its name' do
Integrity::Project.should_receive(:first).with(:name => 'github').and_return(@project)
do_post
Integrity::Project.should_receive(:first).with(:permalink => 'github').and_return(@project)
post_it '/github', :payload => payload
end

it 'should be 400 if unknown project' do
it 'should be 404 if unknown project' do
Integrity::Project.stub!(:first).and_return(nil)
do_post
@response.status.should == 400
@response.body.should == "Unknown project `github'"
post_it '/github', :payload => payload
status.should == 404
body.should == "Unknown project `github'"
end

it 'should make a new build for each commit' do
@project.should_receive(:build).with('41a212ee83ca127e3c8cf465891ab7216a705f59')
@project.should_receive(:build).with('de8251ff97ee194a289832576287d6f8ad74e3d0')
do_post
post_it '/github', :payload => payload
end

describe 'With invalid payload' do
before(:each) do
JSON.stub!(:parse).and_raise(JSON::ParserError.new('error message'))
end

it 'should rescue any JSON parse error and return a 422 status code' do
post_it '/'
post_it '/github'
@response.status.should == 422
end

it 'should rescue any JSON parse error and return the error' do
post_it '/'
post_it '/github'
@response.body.should == 'error message'
end

it 'should return error in plain/text' do
post_it '/'
post_it '/github'
@response['Content-Type'].should == 'text/plain'
end
end
Expand Down

0 comments on commit fbea5eb

Please sign in to comment.