Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor the controller action into smaller, more well-defined methods

  • Loading branch information...
commit fc07ecc104df6b228f4d3dc287e10d47a5e12b63 1 parent 02eeffb
@koppen koppen authored
View
51 app/controllers/github_hook_controller.rb
@@ -6,23 +6,10 @@ class GithubHookController < ApplicationController
skip_before_filter :verify_authenticity_token, :check_if_login_required
def index
- payload = JSON.parse(params[:payload])
- logger.debug { "Received from Github: #{payload.inspect}" }
-
- # For now, we assume that the repository name is the same as the project identifier
- identifier = params[:project_id] || payload['repository']['name']
- raise ActiveRecord::RecordNotFound, "Project identifier not specified" if identifier.nil?
+ repository = find_repository
- project = Project.find_by_identifier(identifier.downcase)
- raise ActiveRecord::RecordNotFound, "No project found with identifier '#{identifier}'" if project.nil?
-
- repository = project.repository
- raise TypeError, "Project '#{identifier}' has no repository" if repository.nil?
- raise TypeError, "Repository for project '#{identifier}' is not a Git repository" unless repository.is_a?(Repository::Git)
-
- # Get updates from the Github repository
- command = "cd '#{repository.url}' && git fetch origin && git reset --soft refs/remotes/origin/master"
- exec(command)
+ # Fetch the changes from Github
+ update_repository(repository)
# Fetch the new changesets into Redmine
repository.fetch_changesets
@@ -44,4 +31,36 @@ def exec(command)
logger.debug { "GithubHook: * STDERR: #{output}"}
end
+ # Fetches updates from the remote repository
+ def update_repository(repository)
+ command = "cd '#{repository.url}' && git fetch origin && git reset --soft refs/remotes/origin/master"
+ exec(command)
+ end
+
+ # Gets the project identifier from the querystring parameters and if that's not supplied, assume
+ # the Github repository name is the same as the project identifier.
+ def get_identifier
+ payload = JSON.parse(params[:payload])
+ identifier = params[:project_id] || payload['repository']['name']
+ raise ActiveRecord::RecordNotFound, "Project identifier not specified" if identifier.nil?
+ return identifier
+ end
+
+ # Finds the Redmine project in the database based on the given project identifier
+ def find_project
+ identifier = get_identifier
+ project = Project.find_by_identifier(identifier.downcase)
+ raise ActiveRecord::RecordNotFound, "No project found with identifier '#{identifier}'" if project.nil?
+ return project
+ end
+
+ # Returns the Redmine Repository object we are trying to update
+ def find_repository
+ project = find_project
+ repository = project.repository
+ raise TypeError, "Project '#{project.to_s}' ('#{project.identifier}') has no repository" if repository.nil?
+ raise TypeError, "Repository for project '#{project.to_s}' ('#{project.identifier}') is not a Git repository" unless repository.is_a?(Repository::Git)
+ return repository
+ end
+
end
View
4 test/functional/github_hook_controller_test.rb
@@ -126,7 +126,7 @@ def test_should_return_404_if_project_not_found
def test_should_return_500_if_project_has_no_repository
assert_raises TypeError do
- project = mock('project')
+ project = mock('project', :to_s => 'My Project', :identifier => 'github')
project.expects(:repository).returns(nil)
Project.expects(:find_by_identifier).with('github').returns(project)
do_post :repository => {:name => 'github'}
@@ -135,7 +135,7 @@ def test_should_return_500_if_project_has_no_repository
def test_should_return_500_if_repository_is_not_git
assert_raises TypeError do
- project = mock('project')
+ project = mock('project', :to_s => 'My Project', :identifier => 'github')
repository = Repository::Subversion.new
project.expects(:repository).at_least(1).returns(repository)
Project.expects(:find_by_identifier).with('github').returns(project)
Please sign in to comment.
Something went wrong with that request. Please try again.