# HG changeset patch # User Jakob Skjerning # Date 1249238037 -7200 # Node ID f22b5d28158e315d3cfb8bb154925eacee951348 # Parent a3752fbaea05c55a91548bd561d5d3d03b4b23bf When they say 'JSON' payload, that's actually what we'll get - who'd have thunk? diff -r a3752fbaea05c55a91548bd561d5d3d03b4b23bf -r f22b5d28158e315d3cfb8bb154925eacee951348 app/controllers/github_hook_controller.rb --- a/app/controllers/github_hook_controller.rb Sun Aug 02 17:48:12 2009 +0200 +++ b/app/controllers/github_hook_controller.rb Sun Aug 02 20:33:57 2009 +0200 @@ -1,16 +1,26 @@ +require 'json' + class GithubHookController < ApplicationController def index + logger.debug { "---------------------------------" } + payload = JSON.parse(params[:payload]) + logger.debug { "Received from Github: #{payload.inspect}" } + + logger.debug { "Finding project" } # For now, we assume that the repository name is the same as the project identifier - project = Project.find_by_identifier(params[:repository][:name]) + project = Project.find_by_identifier(payload['repository']['name']) raise ActiveRecord::RecordNotFound if project.nil? || project.repository.nil? + logger.debug { "Finding repo" } repository = project.repository raise TypeError unless repository.is_a?(Repository::Git) # Get updates from the Github repository command = "cd '#{repository.url}' && git pull" exec(command) + + render(:text => 'OK') end private diff -r a3752fbaea05c55a91548bd561d5d3d03b4b23bf -r f22b5d28158e315d3cfb8bb154925eacee951348 init.rb --- a/init.rb Sun Aug 02 17:48:12 2009 +0200 +++ b/init.rb Sun Aug 02 20:33:57 2009 +0200 @@ -4,5 +4,5 @@ name 'Redmine Github Hook plugin' author 'Jakob Skjerning' description 'This plugin allows your Redmine installation to receive Github post-receive notifications' - version '0.0.2' + version '0.1.0' end diff -r a3752fbaea05c55a91548bd561d5d3d03b4b23bf -r f22b5d28158e315d3cfb8bb154925eacee951348 test/functional/github_hook_controller_test.rb --- a/test/functional/github_hook_controller_test.rb Sun Aug 02 17:48:12 2009 +0200 +++ b/test/functional/github_hook_controller_test.rb Sun Aug 02 20:33:57 2009 +0200 @@ -6,54 +6,57 @@ def setup # Sample JSON post from http://github.com/guides/post-receive-hooks - @json = { - "before" => "5aef35982fb2d34e9d9d4502f6ede1072793222d", - "repository" => { - "url" => "http://github.com/defunkt/github", - "name" => "github", - "description" => "You're lookin' at it.", - "watchers" => 5, - "forks" => 2, - "private" => 1, - "owner" => { - "email" => "chris@ozmm.org", - "name" => "defunkt" + @json = '{ + "before": "5aef35982fb2d34e9d9d4502f6ede1072793222d", + "repository": { + "url": "http://github.com/defunkt/github", + "name": "github", + "description": "You\'re lookin\' at it.", + "watchers": 5, + "forks": 2, + "private": 1, + "owner": { + "email": "chris@ozmm.org", + "name": "defunkt" } }, - "commits" => [ + "commits": [ { - "id" => "41a212ee83ca127e3c8cf465891ab7216a705f59", - "url" => "http://github.com/defunkt/github/commit/41a212ee83ca127e3c8cf465891ab7216a705f59", - "author" => { - "email" => "chris@ozmm.org", - "name" => "Chris Wanstrath" + "id": "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", - "added" => ["filepath.rb"] + "message": "okay i give in", + "timestamp": "2008-02-15T14:57:17-08:00", + "added": ["filepath.rb"] }, { - "id" => "de8251ff97ee194a289832576287d6f8ad74e3d0", - "url" => "http://github.com/defunkt/github/commit/de8251ff97ee194a289832576287d6f8ad74e3d0", - "author" => { - "email" => "chris@ozmm.org", - "name" => "Chris Wanstrath" + "id": "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" + "message": "update pricing a tad", + "timestamp": "2008-02-15T14:36:34-08:00" } ], - "after" => "de8251ff97ee194a289832576287d6f8ad74e3d0", - "ref" => "refs/heads/master" - } + "after": "de8251ff97ee194a289832576287d6f8ad74e3d0", + "ref": "refs/heads/master" + }' @project = Project.first @repository = Repository::Git.new @project.stubs(:repository).returns(@repository) @controller.stubs(:exec) + Project.stubs(:find_by_identifier).with('github').returns(@project) end - def do_post - post :index, @json + def do_post(payload = nil) + payload = @json if payload.nil? + payload = payload.to_json if payload.is_a?(Hash) + post :index, :payload => payload end def test_should_use_the_repository_name_as_project_identifier @@ -67,9 +70,16 @@ do_post end + def test_should_render_ok_when_done + do_post + assert_response :success + assert_equal 'OK', @response.body + end + def test_should_return_404_if_project_not_found assert_raises ActiveRecord::RecordNotFound do - post :index, :repository => {:name => 'foobar'} + Project.expects(:find_by_identifier).with('foobar').returns(nil) + do_post :repository => {:name => 'foobar'} end end @@ -78,7 +88,7 @@ project = mock('project') project.expects(:repository).returns(nil) Project.expects(:find_by_identifier).with('github').returns(project) - post :index, :repository => {:name => 'github'} + do_post :repository => {:name => 'github'} end end @@ -88,7 +98,7 @@ repository = Repository::Subversion.new project.expects(:repository).at_least(1).returns(repository) Project.expects(:find_by_identifier).with('github').returns(project) - post :index, :repository => {:name => 'github'} + do_post :repository => {:name => 'github'} end end