# HG changeset patch # User Jakob Skjerning # Date 1249228092 -7200 # Node ID a3752fbaea05c55a91548bd561d5d3d03b4b23bf # Parent abc18cf65c79da5ee7e04c0c59ff1241070a7101 Renaming to Github Hook which is probably more accurate than Git Hook diff -r abc18cf65c79da5ee7e04c0c59ff1241070a7101 -r a3752fbaea05c55a91548bd561d5d3d03b4b23bf app/controllers/git_hook_controller.rb --- a/app/controllers/git_hook_controller.rb Sun Aug 02 17:44:40 2009 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,23 +0,0 @@ -class GitHookController < ApplicationController - - def index - # For now, we assume that the repository name is the same as the project identifier - project = Project.find_by_identifier(params[:repository][:name]) - raise ActiveRecord::RecordNotFound if project.nil? || project.repository.nil? - - 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) - end - - private - - def exec(command) - logger.debug { "GitHook: Executing command: '#{command}'" } - `#{command}` - end - -end diff -r abc18cf65c79da5ee7e04c0c59ff1241070a7101 -r a3752fbaea05c55a91548bd561d5d3d03b4b23bf app/controllers/github_hook_controller.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/controllers/github_hook_controller.rb Sun Aug 02 17:48:12 2009 +0200 @@ -0,0 +1,23 @@ +class GithubHookController < ApplicationController + + def index + # For now, we assume that the repository name is the same as the project identifier + project = Project.find_by_identifier(params[:repository][:name]) + raise ActiveRecord::RecordNotFound if project.nil? || project.repository.nil? + + 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) + end + + private + + def exec(command) + logger.debug { "GitHook: Executing command: '#{command}'" } + `#{command}` + end + +end diff -r abc18cf65c79da5ee7e04c0c59ff1241070a7101 -r a3752fbaea05c55a91548bd561d5d3d03b4b23bf init.rb --- a/init.rb Sun Aug 02 17:44:40 2009 +0200 +++ b/init.rb Sun Aug 02 17:48:12 2009 +0200 @@ -1,7 +1,7 @@ require 'redmine' -Redmine::Plugin.register :redmine_git_hook do - name 'Redmine Git Hook plugin' +Redmine::Plugin.register :redmine_github_hook do + 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' diff -r abc18cf65c79da5ee7e04c0c59ff1241070a7101 -r a3752fbaea05c55a91548bd561d5d3d03b4b23bf test/functional/git_hook_controller_test.rb --- a/test/functional/git_hook_controller_test.rb Sun Aug 02 17:44:40 2009 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,95 +0,0 @@ -require File.dirname(__FILE__) + '/../test_helper' - -require 'mocha' - -class GitHookControllerTest < ActionController::TestCase - - 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" - } - }, - "commits" => [ - { - "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"] - }, - { - "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" - } - ], - "after" => "de8251ff97ee194a289832576287d6f8ad74e3d0", - "ref" => "refs/heads/master" - } - @project = Project.first - @repository = Repository::Git.new - @project.stubs(:repository).returns(@repository) - @controller.stubs(:exec) - end - - def do_post - post :index, @json - end - - def test_should_use_the_repository_name_as_project_identifier - Project.expects(:find_by_identifier).with('github').returns(@project) - do_post - end - - def test_should_update_the_repository_using_git_on_the_commandline - Project.expects(:find_by_identifier).with('github').returns(@project) - @controller.expects(:exec).returns(true) - do_post - end - - def test_should_return_404_if_project_not_found - assert_raises ActiveRecord::RecordNotFound do - post :index, :repository => {:name => 'foobar'} - end - end - - def test_should_return_404_if_project_has_no_repository - assert_raises ActiveRecord::RecordNotFound do - project = mock('project') - project.expects(:repository).returns(nil) - Project.expects(:find_by_identifier).with('github').returns(project) - post :index, :repository => {:name => 'github'} - end - end - - def test_should_return_500_if_repository_is_not_git - assert_raises TypeError do - project = mock('project') - 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'} - end - end - -end diff -r abc18cf65c79da5ee7e04c0c59ff1241070a7101 -r a3752fbaea05c55a91548bd561d5d3d03b4b23bf test/functional/github_hook_controller_test.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/functional/github_hook_controller_test.rb Sun Aug 02 17:48:12 2009 +0200 @@ -0,0 +1,95 @@ +require File.dirname(__FILE__) + '/../test_helper' + +require 'mocha' + +class GithubHookControllerTest < ActionController::TestCase + + 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" + } + }, + "commits" => [ + { + "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"] + }, + { + "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" + } + ], + "after" => "de8251ff97ee194a289832576287d6f8ad74e3d0", + "ref" => "refs/heads/master" + } + @project = Project.first + @repository = Repository::Git.new + @project.stubs(:repository).returns(@repository) + @controller.stubs(:exec) + end + + def do_post + post :index, @json + end + + def test_should_use_the_repository_name_as_project_identifier + Project.expects(:find_by_identifier).with('github').returns(@project) + do_post + end + + def test_should_update_the_repository_using_git_on_the_commandline + Project.expects(:find_by_identifier).with('github').returns(@project) + @controller.expects(:exec).returns(true) + do_post + end + + def test_should_return_404_if_project_not_found + assert_raises ActiveRecord::RecordNotFound do + post :index, :repository => {:name => 'foobar'} + end + end + + def test_should_return_404_if_project_has_no_repository + assert_raises ActiveRecord::RecordNotFound do + project = mock('project') + project.expects(:repository).returns(nil) + Project.expects(:find_by_identifier).with('github').returns(project) + post :index, :repository => {:name => 'github'} + end + end + + def test_should_return_500_if_repository_is_not_git + assert_raises TypeError do + project = mock('project') + 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'} + end + end + +end