Skip to content

Commit 3136e05

Browse files
committed
Limit directory-changing to a single method
1 parent c0568fb commit 3136e05

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

app/controllers/github_hook_controller.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,27 @@ def system(command)
3131
Kernel.system(command)
3232
end
3333

34-
# Executes shell command. Returns true if the shell command exits with a success status code
35-
def exec(command)
34+
# Executes shell command. Returns true if the shell command exits with a
35+
# success status code.
36+
#
37+
# If directory is given the current directory will be changed to that
38+
# directory before executing command.
39+
def exec(command, directory)
3640
logger.debug { "GithubHook: Executing command: '#{command}'" }
3741

3842
# Get a path to a temp file
3943
logfile = Tempfile.new('github_hook_exec')
4044
logfile.close
4145

42-
success = system("#{command} > #{logfile.path} 2>&1")
46+
full_command = "#{command} > #{logfile.path} 2>&1"
47+
success = if directory.present?
48+
Dir.chdir(directory) do
49+
system(full_command)
50+
end
51+
else
52+
system(full_command)
53+
end
54+
4355
output_from_command = File.readlines(logfile.path)
4456
if success
4557
logger.debug { "GithubHook: Command output: #{output_from_command.inspect}"}
@@ -58,12 +70,10 @@ def git_command(command)
5870

5971
# Fetches updates from the remote repository
6072
def update_repository(repository)
61-
Dir.chdir(repository.url) do
62-
command = git_command('fetch origin')
63-
if exec(command)
64-
command = git_command("fetch origin \"+refs/heads/*:refs/heads/*\"")
65-
exec(command)
66-
end
73+
command = git_command('fetch origin')
74+
if exec(command, repository.url)
75+
command = git_command("fetch origin \"+refs/heads/*:refs/heads/*\"")
76+
exec(command, repository.url)
6777
end
6878
end
6979

test/functional/github_hook_controller_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,21 @@ def test_should_use_the_repository_name_as_project_identifier
9292

9393
def test_should_fetch_changes_from_origin
9494
Project.expects(:find_by_identifier).with('github').returns(project)
95-
@controller.expects(:exec).with("git --git-dir=\"#{repository.url}\" fetch origin")
95+
@controller.expects(:exec).with("git fetch origin", repository.url)
9696
do_post
9797
end
9898

9999
def test_should_reset_repository_when_fetch_origin_succeeds
100100
Project.expects(:find_by_identifier).with('github').returns(project)
101-
@controller.expects(:exec).with("git --git-dir=\"#{repository.url}\" fetch origin").returns(true)
102-
@controller.expects(:exec).with("git --git-dir=\"#{repository.url}\" fetch origin \"+refs/heads/*:refs/heads/*\"")
101+
@controller.expects(:exec).with("git fetch origin", repository.url).returns(true)
102+
@controller.expects(:exec).with("git fetch origin \"+refs/heads/*:refs/heads/*\"", repository.url)
103103
do_post
104104
end
105105

106106
def test_should_not_reset_repository_when_fetch_origin_fails
107107
Project.expects(:find_by_identifier).with('github').returns(project)
108-
@controller.expects(:exec).with("git --git-dir=\"#{repository.url}\" fetch origin").returns(false)
109-
@controller.expects(:exec).with("git --git-dir=\"#{repository.url}\" reset --soft refs\/remotes\/origin\/master").never
108+
@controller.expects(:exec).with("git fetch origin", repository.url).returns(false)
109+
@controller.expects(:exec).with("git reset --soft refs\/remotes\/origin\/master", repository.url).never
110110
do_post
111111
end
112112

0 commit comments

Comments
 (0)