Skip to content

Commit

Permalink
Move GitHub's post-receive script to the web UI [integrity#19 state:r…
Browse files Browse the repository at this point in the history
…esolved]
  • Loading branch information
foca committed Jul 26, 2008
1 parent d775d16 commit 12826e2
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 136 deletions.
1 change: 1 addition & 0 deletions lib/integrity.rb
Expand Up @@ -2,6 +2,7 @@
$:.unshift "#{__DIR__}/integrity", "#{__DIR__}integrity/scm", *Dir["#{__DIR__}/../vendor/**/lib"].to_a

require "rubygems"
require 'json'
require 'dm-core'
require 'dm-validations'
require 'dm-types'
Expand Down
34 changes: 0 additions & 34 deletions lib/integrity/github.rb

This file was deleted.

16 changes: 16 additions & 0 deletions lib/integrity/ui.rb
Expand Up @@ -81,6 +81,18 @@
show :new, :title => ["projects", current_project.permalink, "edit"]
end

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

begin
payload = JSON.parse(params[:payload] || "")
payload['commits'].each_key { |commit| current_project.build(commit) }
'Thanks, build started.'
rescue JSON::ParserError => exception
invalid_payload!(exception.to_s)
end
end

post "/:project/builds" do
login_required

Expand Down Expand Up @@ -121,6 +133,10 @@ def unauthorized!(realm=authorization_realm)
throw :halt, [401, show(:unauthorized, :title => "incorrect credentials")]
end

def invalid_payload!(msg=nil)
throw :halt, [422, msg || 'No payload given']
end

def current_project
@project ||= Project.first(:permalink => params[:project]) or raise Sinatra::NotFound
end
Expand Down
100 changes: 0 additions & 100 deletions spec/github_spec.rb

This file was deleted.

99 changes: 97 additions & 2 deletions spec/ui_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'

describe 'Web UI using Sinatra' do
describe 'Web UI' do
def mock_project(messages={})
messages = {
:name => "Integrity",
Expand Down Expand Up @@ -48,7 +48,7 @@ def provide_valid_credentials!

before(:each) do
Integrity.stub!(:new)
require File.dirname(__FILE__) + '/../lib/integrity/ui'
require Integrity.root / "lib" / "integrity" / "ui"
end

after(:each) { @project = nil }
Expand Down Expand Up @@ -428,6 +428,101 @@ def provide_valid_credentials!
end
end

describe "POST /:project/push" do
def payload
<<-EOS
{ "before": "5aef35982fb2d34e9d9d4502f6ede1072793222d",
"repository": {
"url": "http://github.com/defunkt/github",
"name": "github",
"owner": {
"email": "chris@ozmm.org",
"name": "defunkt"
}
},
"commits": {
"41a212ee83ca127e3c8cf465891ab7216a705f59": {
"url": "http://github.com/defunkt/github/commit/41a212ee83ca127e3c8cf465891ab7216a705f59",
"author": {
"email": "chris@ozmm.org",
"name": "Chris Wanstrath"
},
"message": "okay i give in",
"timestamp": "2008-02-15T14:57:17-08:00"
},
"de8251ff97ee194a289832576287d6f8ad74e3d0": {
"url": "http://github.com/defunkt/github/commit/de8251ff97ee194a289832576287d6f8ad74e3d0",
"author": {
"email": "chris@ozmm.org",
"name": "Chris Wanstrath"
},
"message": "update pricing a tad",
"timestamp": "2008-02-15T14:36:34-08:00"
}
},
"after": "de8251ff97ee194a289832576287d6f8ad74e3d0",
"ref": "refs/heads/master" }
EOS
end

before do
Project.stub!(:first).with(:permalink => "github").and_return mock_project
end

it 'should be successful' do
post_it '/github/push', :payload => payload
status.should == 200
end

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

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

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

it 'should be 404 if unknown project' do
Integrity::Project.stub!(:first).and_return(nil)
post_it '/github/push', :payload => payload
status.should == 404
end

it 'should make a new build for each commit' do
@project.should_receive(:build).with('41a212ee83ca127e3c8cf465891ab7216a705f59')
@project.should_receive(:build).with('de8251ff97ee194a289832576287d6f8ad74e3d0')
post_it '/github/push', :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 '/github/push'
@response.status.should == 422
end

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

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

describe "POST /:project/builds" do
it "should build the project" do
Project.stub!(:first).with(:permalink => "integrity").and_return mock_project
Expand Down

0 comments on commit 12826e2

Please sign in to comment.