From 078f11a6b9c0197a3cfde6b3658ca268097e315c Mon Sep 17 00:00:00 2001
From: Jacob Vosmaer <jacob@gitlab.com>
Date: Wed, 27 Feb 2019 10:58:21 +0000
Subject: [PATCH] Prepare test suite for switch to Gitaly-embedded Git hooks

---
 config/gitlab.yml.example              |  2 --
 config/initializers/1_settings.rb      |  2 +-
 lib/gitlab/shell.rb                    |  8 ++++----
 lib/tasks/gitlab/info.rake             |  4 ++--
 spec/lib/gitlab/git/repository_spec.rb |  4 ++--
 spec/lib/gitlab/shell_spec.rb          |  1 -
 spec/support/helpers/test_env.rb       | 12 ++++++++----
 spec/tasks/gitlab/shell_rake_spec.rb   |  2 +-
 8 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/config/gitlab.yml.example b/config/gitlab.yml.example
index be23166cb7bd4..1a5b9ec3f0238 100644
--- a/config/gitlab.yml.example
+++ b/config/gitlab.yml.example
@@ -662,7 +662,6 @@ production: &base
   ## GitLab Shell settings
   gitlab_shell:
     path: /home/git/gitlab-shell/
-    hooks_path: /home/git/gitlab-shell/hooks/
 
     # File that contains the secret key for verifying access for gitlab-shell.
     # Default is '.gitlab_shell_secret' relative to Rails.root (i.e. root of the GitLab app).
@@ -820,7 +819,6 @@ test:
     path: tmp/tests/backups
   gitlab_shell:
     path: tmp/tests/gitlab-shell/
-    hooks_path: tmp/tests/gitlab-shell/hooks/
   issues_tracker:
     redmine:
       title: "Redmine"
diff --git a/config/initializers/1_settings.rb b/config/initializers/1_settings.rb
index dfcf1e648b48c..1344b3cb1f6f2 100644
--- a/config/initializers/1_settings.rb
+++ b/config/initializers/1_settings.rb
@@ -337,7 +337,7 @@
 #
 Settings['gitlab_shell'] ||= Settingslogic.new({})
 Settings.gitlab_shell['path']           = Settings.absolute(Settings.gitlab_shell['path'] || Settings.gitlab['user_home'] + '/gitlab-shell/')
-Settings.gitlab_shell['hooks_path']     = Settings.absolute(Settings.gitlab_shell['hooks_path'] || Settings.gitlab['user_home'] + '/gitlab-shell/hooks/')
+Settings.gitlab_shell['hooks_path']     = :deprecated_use_gitlab_shell_path_instead
 Settings.gitlab_shell['secret_file'] ||= Rails.root.join('.gitlab_shell_secret')
 Settings.gitlab_shell['receive_pack']   = true if Settings.gitlab_shell['receive_pack'].nil?
 Settings.gitlab_shell['upload_pack']    = true if Settings.gitlab_shell['upload_pack'].nil?
diff --git a/lib/gitlab/shell.rb b/lib/gitlab/shell.rb
index c7d8dfcd495be..40b641b831714 100644
--- a/lib/gitlab/shell.rb
+++ b/lib/gitlab/shell.rb
@@ -340,16 +340,16 @@ def exists?(storage, dir_name)
     end
     # rubocop: enable CodeReuse/ActiveRecord
 
+    def hooks_path
+      File.join(gitlab_shell_path, 'hooks')
+    end
+
     protected
 
     def gitlab_shell_path
       File.expand_path(Gitlab.config.gitlab_shell.path)
     end
 
-    def gitlab_shell_hooks_path
-      File.expand_path(Gitlab.config.gitlab_shell.hooks_path)
-    end
-
     def gitlab_shell_user_home
       File.expand_path("~#{Gitlab.config.gitlab_shell.ssh_user}")
     end
diff --git a/lib/tasks/gitlab/info.rake b/lib/tasks/gitlab/info.rake
index e97d77d20e016..b8798fb3cfd55 100644
--- a/lib/tasks/gitlab/info.rake
+++ b/lib/tasks/gitlab/info.rake
@@ -58,7 +58,7 @@ namespace :gitlab do
       puts "Omniauth Providers: #{omniauth_providers.join(', ')}" if Gitlab::Auth.omniauth_enabled?
 
       # check Gitolite version
-      gitlab_shell_version_file = "#{Gitlab.config.gitlab_shell.hooks_path}/../VERSION"
+      gitlab_shell_version_file = "#{Gitlab.config.gitlab_shell.path}/VERSION"
       if File.readable?(gitlab_shell_version_file)
         gitlab_shell_version = File.read(gitlab_shell_version_file)
       end
@@ -72,7 +72,7 @@ namespace :gitlab do
           puts "- #{name}: \t#{repository_storage.legacy_disk_path}"
         end
       end
-      puts "Hooks:\t\t#{Gitlab.config.gitlab_shell.hooks_path}"
+      puts "GitLab Shell path:\t\t#{Gitlab.config.gitlab_shell.path}"
       puts "Git:\t\t#{Gitlab.config.git.bin_path}"
     end
   end
diff --git a/spec/lib/gitlab/git/repository_spec.rb b/spec/lib/gitlab/git/repository_spec.rb
index b3a728c139ed1..e3dd02f147810 100644
--- a/spec/lib/gitlab/git/repository_spec.rb
+++ b/spec/lib/gitlab/git/repository_spec.rb
@@ -31,7 +31,7 @@
   describe '.create_hooks' do
     let(:repo_path) { File.join(storage_path, 'hook-test.git') }
     let(:hooks_dir) { File.join(repo_path, 'hooks') }
-    let(:target_hooks_dir) { Gitlab.config.gitlab_shell.hooks_path }
+    let(:target_hooks_dir) { Gitlab::Shell.new.hooks_path }
     let(:existing_target) { File.join(repo_path, 'foobar') }
 
     before do
@@ -1945,7 +1945,7 @@ def merge_to_ref
       imported_repo.create_from_bundle(valid_bundle_path)
       hooks_path = Gitlab::GitalyClient::StorageSettings.allow_disk_access { File.join(imported_repo.path, 'hooks') }
 
-      expect(File.readlink(hooks_path)).to eq(Gitlab.config.gitlab_shell.hooks_path)
+      expect(File.readlink(hooks_path)).to eq(Gitlab::Shell.new.hooks_path)
     end
 
     it 'raises an error if the bundle is an attempted malicious payload' do
diff --git a/spec/lib/gitlab/shell_spec.rb b/spec/lib/gitlab/shell_spec.rb
index 033e1bf81a110..d6aadf0f7de91 100644
--- a/spec/lib/gitlab/shell_spec.rb
+++ b/spec/lib/gitlab/shell_spec.rb
@@ -393,7 +393,6 @@
 
     before do
       allow(Gitlab.config.gitlab_shell).to receive(:path).and_return(gitlab_shell_path)
-      allow(Gitlab.config.gitlab_shell).to receive(:hooks_path).and_return(gitlab_shell_hooks_path)
       allow(Gitlab.config.gitlab_shell).to receive(:git_timeout).and_return(800)
     end
 
diff --git a/spec/support/helpers/test_env.rb b/spec/support/helpers/test_env.rb
index 06bcf4f80136a..80a22134021fa 100644
--- a/spec/support/helpers/test_env.rb
+++ b/spec/support/helpers/test_env.rb
@@ -147,12 +147,15 @@ def setup_gitlab_shell
       version: Gitlab::Shell.version_required,
       task: 'gitlab:shell:install')
 
-    create_fake_git_hooks
+    # gitlab-shell hooks don't work in our test environment because they try to make internal API calls
+    sabotage_gitlab_shell_hooks
   end
 
-  def create_fake_git_hooks
-    # gitlab-shell hooks don't work in our test environment because they try to make internal API calls
-    hooks_dir = File.join(Gitlab.config.gitlab_shell.path, 'hooks')
+  def sabotage_gitlab_shell_hooks
+    create_fake_git_hooks(Gitlab::Shell.new.hooks_path)
+  end
+
+  def create_fake_git_hooks(hooks_dir)
     %w[pre-receive post-receive update].each do |hook|
       File.open(File.join(hooks_dir, hook), 'w', 0755) { |f| f.puts '#!/bin/sh' }
     end
@@ -169,6 +172,7 @@ def setup_gitaly
       task: "gitlab:gitaly:install[#{install_gitaly_args}]") do
 
         Gitlab::SetupHelper.create_gitaly_configuration(gitaly_dir, { 'default' => repos_path }, force: true)
+        create_fake_git_hooks(File.join(gitaly_dir, 'ruby/git-hooks'))
         start_gitaly(gitaly_dir)
       end
   end
diff --git a/spec/tasks/gitlab/shell_rake_spec.rb b/spec/tasks/gitlab/shell_rake_spec.rb
index 0ed5d3e27b989..a9d1407017732 100644
--- a/spec/tasks/gitlab/shell_rake_spec.rb
+++ b/spec/tasks/gitlab/shell_rake_spec.rb
@@ -8,7 +8,7 @@
   end
 
   after do
-    TestEnv.create_fake_git_hooks
+    TestEnv.sabotage_gitlab_shell_hooks
   end
 
   describe 'install task' do
-- 
GitLab