From b88f27c8d1cb44201490bf51db143f4267735775 Mon Sep 17 00:00:00 2001
From: Gabriel Mazetto <brodock@gmail.com>
Date: Tue, 15 Jan 2019 04:37:27 +0100
Subject: [PATCH] Extract BaseRepositoryService from MigrateRepository

This will serve as new base class for both Migrate and Rollback
---
 .../hashed_storage/base_repository_service.rb | 57 +++++++++++++++++++
 .../migrate_repository_service.rb             | 46 +--------------
 2 files changed, 58 insertions(+), 45 deletions(-)
 create mode 100644 app/services/projects/hashed_storage/base_repository_service.rb

diff --git a/app/services/projects/hashed_storage/base_repository_service.rb b/app/services/projects/hashed_storage/base_repository_service.rb
new file mode 100644
index 00000000000..cc5366f1f61
--- /dev/null
+++ b/app/services/projects/hashed_storage/base_repository_service.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+module Projects
+  module HashedStorage
+    # Returned when there is an error with the Hashed Storage migration
+    RepositoryMigrationError = Class.new(StandardError)
+
+    # Returned when there is an error with the Hashed Storage rollback
+    RepositoryRollbackError = Class.new(StandardError)
+
+    class BaseRepositoryService < BaseService
+      include Gitlab::ShellAdapter
+
+      attr_reader :old_disk_path, :new_disk_path, :old_wiki_disk_path, :old_storage_version, :logger, :move_wiki
+
+      def initialize(project, old_disk_path, logger: nil)
+        @project = project
+        @logger = logger || Rails.logger
+        @old_disk_path = old_disk_path
+        @old_wiki_disk_path = "#{old_disk_path}.wiki"
+        @move_wiki = has_wiki?
+      end
+
+      protected
+
+      # rubocop: disable CodeReuse/ActiveRecord
+      def has_wiki?
+        gitlab_shell.exists?(project.repository_storage, "#{old_wiki_disk_path}.git")
+      end
+      # rubocop: enable CodeReuse/ActiveRecord
+
+      # rubocop: disable CodeReuse/ActiveRecord
+      def move_repository(from_name, to_name)
+        from_exists = gitlab_shell.exists?(project.repository_storage, "#{from_name}.git")
+        to_exists = gitlab_shell.exists?(project.repository_storage, "#{to_name}.git")
+
+        # If we don't find the repository on either original or target we should log that as it could be an issue if the
+        # project was not originally empty.
+        if !from_exists && !to_exists
+          logger.warn "Can't find a repository on either source or target paths for #{project.full_path} (ID=#{project.id}) ..."
+          return false
+        elsif !from_exists
+          # Repository have been moved already.
+          return true
+        end
+
+        gitlab_shell.mv_repository(project.repository_storage, from_name, to_name)
+      end
+      # rubocop: enable CodeReuse/ActiveRecord
+
+      def rollback_folder_move
+        move_repository(new_disk_path, old_disk_path)
+        move_repository("#{new_disk_path}.wiki", old_wiki_disk_path)
+      end
+    end
+  end
+end
diff --git a/app/services/projects/hashed_storage/migrate_repository_service.rb b/app/services/projects/hashed_storage/migrate_repository_service.rb
index 2d851866a18..9c672283c7e 100644
--- a/app/services/projects/hashed_storage/migrate_repository_service.rb
+++ b/app/services/projects/hashed_storage/migrate_repository_service.rb
@@ -2,21 +2,7 @@
 
 module Projects
   module HashedStorage
-    RepositoryMigrationError = Class.new(StandardError)
-
-    class MigrateRepositoryService < BaseService
-      include Gitlab::ShellAdapter
-
-      attr_reader :old_disk_path, :new_disk_path, :old_wiki_disk_path, :old_storage_version, :logger, :move_wiki
-
-      def initialize(project, old_disk_path, logger: nil)
-        @project = project
-        @logger = logger || Rails.logger
-        @old_disk_path = old_disk_path
-        @old_wiki_disk_path = "#{old_disk_path}.wiki"
-        @move_wiki = has_wiki?
-      end
-
+    class MigrateRepositoryService < BaseRepositoryService
       def execute
         try_to_set_repository_read_only!
 
@@ -61,36 +47,6 @@ module Projects
           raise RepositoryMigrationError, migration_error
         end
       end
-
-      # rubocop: disable CodeReuse/ActiveRecord
-      def has_wiki?
-        gitlab_shell.exists?(project.repository_storage, "#{old_wiki_disk_path}.git")
-      end
-      # rubocop: enable CodeReuse/ActiveRecord
-
-      # rubocop: disable CodeReuse/ActiveRecord
-      def move_repository(from_name, to_name)
-        from_exists = gitlab_shell.exists?(project.repository_storage, "#{from_name}.git")
-        to_exists = gitlab_shell.exists?(project.repository_storage, "#{to_name}.git")
-
-        # If we don't find the repository on either original or target we should log that as it could be an issue if the
-        # project was not originally empty.
-        if !from_exists && !to_exists
-          logger.warn "Can't find a repository on either source or target paths for #{project.full_path} (ID=#{project.id}) ..."
-          return false
-        elsif !from_exists
-          # Repository have been moved already.
-          return true
-        end
-
-        gitlab_shell.mv_repository(project.repository_storage, from_name, to_name)
-      end
-      # rubocop: enable CodeReuse/ActiveRecord
-
-      def rollback_folder_move
-        move_repository(new_disk_path, old_disk_path)
-        move_repository("#{new_disk_path}.wiki", old_wiki_disk_path)
-      end
     end
   end
 end
-- 
2.30.9