From b3231a9559d5407659bae80913766abee36ebbda Mon Sep 17 00:00:00 2001
From: Gabriel Mazetto <gabriel@gitlab.com>
Date: Tue, 23 Jul 2024 16:12:09 +0200
Subject: [PATCH] Refactor SourceContext and move it into Context module

---
 .../lib/gitlab/backup/cli.rb                  |   2 +-
 .../lib/gitlab/backup/cli/context.rb          |  11 ++
 .../backup/cli/context/source_context.rb      | 152 ++++++++++++++++++
 .../lib/gitlab/backup/cli/source_context.rb   | 150 -----------------
 .../lib/gitlab/backup/cli/tasks/registry.rb   |   2 +-
 .../lib/gitlab/backup/cli/tasks/task.rb       |   2 +-
 .../cli/{ => context}/source_context_spec.rb  |   2 +-
 7 files changed, 167 insertions(+), 154 deletions(-)
 create mode 100644 gems/gitlab-backup-cli/lib/gitlab/backup/cli/context.rb
 create mode 100644 gems/gitlab-backup-cli/lib/gitlab/backup/cli/context/source_context.rb
 delete mode 100644 gems/gitlab-backup-cli/lib/gitlab/backup/cli/source_context.rb
 rename gems/gitlab-backup-cli/spec/gitlab/backup/cli/{ => context}/source_context_spec.rb (99%)

diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb
index 4e3413ce77196..bb9fccbe3c6ee 100644
--- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb
+++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb
@@ -13,13 +13,13 @@ module Backup
     module Cli
       autoload :BackupExecutor, 'gitlab/backup/cli/backup_executor'
       autoload :Commands, 'gitlab/backup/cli/commands'
+      autoload :Context, 'gitlab/backup/cli/context'
       autoload :Dependencies, 'gitlab/backup/cli/dependencies'
       autoload :GitlabConfig, 'gitlab/backup/cli/gitlab_config'
       autoload :Metadata, 'gitlab/backup/cli/metadata'
       autoload :Output, 'gitlab/backup/cli/output'
       autoload :RestoreExecutor, 'gitlab/backup/cli/restore_executor'
       autoload :Runner, 'gitlab/backup/cli/runner'
-      autoload :SourceContext, 'gitlab/backup/cli/source_context'
       autoload :Shell, 'gitlab/backup/cli/shell'
       autoload :Targets, 'gitlab/backup/cli/targets'
       autoload :Tasks, 'gitlab/backup/cli/tasks'
diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/context.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/context.rb
new file mode 100644
index 0000000000000..415add20103e5
--- /dev/null
+++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/context.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+module Gitlab
+  module Backup
+    module Cli
+      module Context
+        autoload :SourceContext, 'gitlab/backup/cli/context/source_context'
+      end
+    end
+  end
+end
diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/context/source_context.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/context/source_context.rb
new file mode 100644
index 0000000000000..902fc19f21a51
--- /dev/null
+++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/context/source_context.rb
@@ -0,0 +1,152 @@
+# frozen_string_literal: true
+
+module Gitlab
+  module Backup
+    module Cli
+      module Context
+        # This context is equivalent to a Source Install or GDK instance
+        #
+        # Any specific information from the GitLab installation will be
+        # automatically discovered from the current machine
+        class SourceContext
+          # Defaults defined in `config/initializers/1_settings.rb`
+          DEFAULT_SHARED_PATH = 'shared'
+          DEFAULT_CI_BUILDS_PATH = 'builds'
+          DEFAULT_JOBS_ARTIFACTS_PATH = 'artifacts'
+          DEFAULT_SECURE_FILES_PATH = 'ci_secure_files'
+          DEFAULT_CI_LFS_PATH = 'lfs-objects'
+          DEFAULT_PACKAGES = 'packages'
+          DEFAULT_PAGES = 'pages'
+          DEFAULT_REGISTRY_PATH = 'registry'
+          DEFAULT_TERRAFORM_STATE_PATH = 'terraform_state'
+          DEFAULT_UPLOADS_PATH = 'public' # based on GitLab's root folder
+
+          def gitlab_version
+            File.read(gitlab_basepath.join("VERSION")).strip.freeze
+          end
+
+          def backup_basedir
+            path = gitlab_config[env]['backup']['path']
+
+            absolute_path(path)
+          end
+
+          # CI Builds basepath
+          def ci_builds_path
+            path = gitlab_config.dig(env, 'gitlab_ci', 'builds_path') || DEFAULT_CI_BUILDS_PATH
+
+            absolute_path(path)
+          end
+
+          # Job Artifacts basepath
+          def ci_job_artifacts_path
+            path = gitlab_config.dig(env, 'artifacts', 'path') ||
+              gitlab_config.dig(env, 'artifacts', 'storage_path') ||
+              gitlab_shared_path.join(DEFAULT_JOBS_ARTIFACTS_PATH)
+
+            absolute_path(path)
+          end
+
+          # CI Secure Files basepath
+          def ci_secure_files_path
+            path = gitlab_config.dig(env, 'ci_secure_files', 'storage_path') ||
+              gitlab_shared_path.join(DEFAULT_SECURE_FILES_PATH)
+
+            absolute_path(path)
+          end
+
+          # CI LFS basepath
+          def ci_lfs_path
+            path = gitlab_config.dig(env, 'lfs', 'storage_path') ||
+              gitlab_shared_path.join(DEFAULT_CI_LFS_PATH)
+
+            absolute_path(path)
+          end
+
+          # Packages basepath
+          def packages_path
+            path = gitlab_config.dig(env, 'packages', 'storage_path') ||
+              gitlab_shared_path.join(DEFAULT_PACKAGES)
+
+            absolute_path(path)
+          end
+
+          # GitLab Pages basepath
+          def pages_path
+            path = gitlab_config.dig(env, 'pages', 'path') ||
+              gitlab_shared_path.join(DEFAULT_PAGES)
+
+            absolute_path(path)
+          end
+
+          # Registry basepath
+          def registry_path
+            path = gitlab_config.dig(env, 'registry', 'path') ||
+              gitlab_shared_path.join(DEFAULT_REGISTRY_PATH)
+
+            absolute_path(path)
+          end
+
+          # Terraform State basepath
+          def terraform_state_path
+            path = gitlab_config.dig(env, 'terraform_state', 'storage_path') ||
+              gitlab_shared_path.join(DEFAULT_TERRAFORM_STATE_PATH)
+
+            absolute_path(path)
+          end
+
+          # Upload basepath
+          def upload_path
+            path = gitlab_config.dig(env, 'uploads', 'storage_path') ||
+              gitlab_basepath.join(DEFAULT_UPLOADS_PATH)
+
+            absolute_path(path).join('uploads')
+          end
+
+          def config(object_type)
+            Gitlab.config[object_type]
+          end
+
+          def env
+            @env ||= ActiveSupport::EnvironmentInquirer.new(
+              ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence || "development")
+          end
+
+          private
+
+          # Return the shared path used as a fallback base location to each blob type
+          # We use this to determine the storage location when everything else fails
+          # @return [Pathname]
+          def gitlab_shared_path
+            shared_path = gitlab_config.dig(env, 'shared', 'path') || DEFAULT_SHARED_PATH
+
+            Pathname(shared_path)
+          end
+
+          # Return a fullpath for a given path
+          #
+          # When the path is already a full one return itself as a Pathname
+          # otherwise uses gitlab_basepath as its base
+          # @param [String|Pathname] path
+          # @return [Pathname]
+          def absolute_path(path)
+            # Joins with gitlab_basepath when relative, otherwise return full path
+            Pathname(File.expand_path(path, gitlab_basepath))
+          end
+
+          # Return the GitLab base directory
+          # @return [Pathname]
+          def gitlab_basepath
+            return Pathname.new(GITLAB_PATH) if GITLAB_PATH
+
+            raise ::Gitlab::Backup::Cli::Error, 'GITLAB_PATH is missing'
+          end
+
+          def gitlab_config
+            @gitlab_config ||= Gitlab::Backup::Cli::GitlabConfig.new(gitlab_basepath.join('config/gitlab.yml'))
+          end
+        end
+      end
+    end
+  end
+end
diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/source_context.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/source_context.rb
deleted file mode 100644
index aaab5fe18d1d6..0000000000000
--- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/source_context.rb
+++ /dev/null
@@ -1,150 +0,0 @@
-# frozen_string_literal: true
-
-module Gitlab
-  module Backup
-    module Cli
-      # This context is equivalent to a Source Install or GDK instance
-      #
-      # Any specific information from the GitLab installation will be
-      # automatically discovered from the current machine
-      class SourceContext
-        # Defaults defined in `config/initializers/1_settings.rb`
-        DEFAULT_SHARED_PATH = 'shared'
-        DEFAULT_CI_BUILDS_PATH = 'builds'
-        DEFAULT_JOBS_ARTIFACTS_PATH = 'artifacts'
-        DEFAULT_SECURE_FILES_PATH = 'ci_secure_files'
-        DEFAULT_CI_LFS_PATH = 'lfs-objects'
-        DEFAULT_PACKAGES = 'packages'
-        DEFAULT_PAGES = 'pages'
-        DEFAULT_REGISTRY_PATH = 'registry'
-        DEFAULT_TERRAFORM_STATE_PATH = 'terraform_state'
-        DEFAULT_UPLOADS_PATH = 'public' # based on GitLab's root folder
-
-        def gitlab_version
-          File.read(gitlab_basepath.join("VERSION")).strip.freeze
-        end
-
-        def backup_basedir
-          path = gitlab_config[env]['backup']['path']
-
-          absolute_path(path)
-        end
-
-        # CI Builds basepath
-        def ci_builds_path
-          path = gitlab_config.dig(env, 'gitlab_ci', 'builds_path') || DEFAULT_CI_BUILDS_PATH
-
-          absolute_path(path)
-        end
-
-        # Job Artifacts basepath
-        def ci_job_artifacts_path
-          path = gitlab_config.dig(env, 'artifacts', 'path') ||
-            gitlab_config.dig(env, 'artifacts', 'storage_path') ||
-            gitlab_shared_path.join(DEFAULT_JOBS_ARTIFACTS_PATH)
-
-          absolute_path(path)
-        end
-
-        # CI Secure Files basepath
-        def ci_secure_files_path
-          path = gitlab_config.dig(env, 'ci_secure_files', 'storage_path') ||
-            gitlab_shared_path.join(DEFAULT_SECURE_FILES_PATH)
-
-          absolute_path(path)
-        end
-
-        # CI LFS basepath
-        def ci_lfs_path
-          path = gitlab_config.dig(env, 'lfs', 'storage_path') ||
-            gitlab_shared_path.join(DEFAULT_CI_LFS_PATH)
-
-          absolute_path(path)
-        end
-
-        # Packages basepath
-        def packages_path
-          path = gitlab_config.dig(env, 'packages', 'storage_path') ||
-            gitlab_shared_path.join(DEFAULT_PACKAGES)
-
-          absolute_path(path)
-        end
-
-        # GitLab Pages basepath
-        def pages_path
-          path = gitlab_config.dig(env, 'pages', 'path') ||
-            gitlab_shared_path.join(DEFAULT_PAGES)
-
-          absolute_path(path)
-        end
-
-        # Registry basepath
-        def registry_path
-          path = gitlab_config.dig(env, 'registry', 'path') ||
-            gitlab_shared_path.join(DEFAULT_REGISTRY_PATH)
-
-          absolute_path(path)
-        end
-
-        # Terraform State basepath
-        def terraform_state_path
-          path = gitlab_config.dig(env, 'terraform_state', 'storage_path') ||
-            gitlab_shared_path.join(DEFAULT_TERRAFORM_STATE_PATH)
-
-          absolute_path(path)
-        end
-
-        # Upload basepath
-        def upload_path
-          path = gitlab_config.dig(env, 'uploads', 'storage_path') ||
-            gitlab_basepath.join(DEFAULT_UPLOADS_PATH)
-
-          absolute_path(path).join('uploads')
-        end
-
-        def config(object_type)
-          Gitlab.config[object_type]
-        end
-
-        def env
-          @env ||= ActiveSupport::EnvironmentInquirer.new(
-            ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence || "development")
-        end
-
-        private
-
-        # Return the shared path used as a fallback base location to each blob type
-        # We use this to determine the storage location when everything else fails
-        # @return [Pathname]
-        def gitlab_shared_path
-          shared_path = gitlab_config.dig(env, 'shared', 'path') || DEFAULT_SHARED_PATH
-
-          Pathname(shared_path)
-        end
-
-        # Return a fullpath for a given path
-        #
-        # When the path is already a full one return itself as a Pathname
-        # otherwise uses gitlab_basepath as its base
-        # @param [String|Pathname] path
-        # @return [Pathname]
-        def absolute_path(path)
-          # Joins with gitlab_basepath when relative, otherwise return full path
-          Pathname(File.expand_path(path, gitlab_basepath))
-        end
-
-        # Return the GitLab base directory
-        # @return [Pathname]
-        def gitlab_basepath
-          return Pathname.new(GITLAB_PATH) if GITLAB_PATH
-
-          raise ::Gitlab::Backup::Cli::Error, 'GITLAB_PATH is missing'
-        end
-
-        def gitlab_config
-          @gitlab_config ||= Gitlab::Backup::Cli::GitlabConfig.new(gitlab_basepath.join('config/gitlab.yml'))
-        end
-      end
-    end
-  end
-end
diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/registry.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/registry.rb
index b9b6d9f8fbe7a..962a8dab09848 100644
--- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/registry.rb
+++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/registry.rb
@@ -21,7 +21,7 @@ def object_storage?
           def config
             settings = {
               object_store: {
-                connection: Gitlab::Backup::Cli::SourceContext.new.config('object_store').connection.to_hash,
+                connection: context.config('object_store').connection.to_hash,
                 remote_directory: options.container_registry_bucket
               }
             }
diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/task.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/task.rb
index 2a290cfd4bf41..e26ab4df32ef0 100644
--- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/task.rb
+++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/task.rb
@@ -69,7 +69,7 @@ def enabled?
           end
 
           def config
-            Gitlab::Backup::Cli::SourceContext.new.config(id)
+            context.config(id)
           end
 
           def object_storage?
diff --git a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/source_context_spec.rb b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/context/source_context_spec.rb
similarity index 99%
rename from gems/gitlab-backup-cli/spec/gitlab/backup/cli/source_context_spec.rb
rename to gems/gitlab-backup-cli/spec/gitlab/backup/cli/context/source_context_spec.rb
index 584d527349d7b..36c5d7206e461 100644
--- a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/source_context_spec.rb
+++ b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/context/source_context_spec.rb
@@ -1,6 +1,6 @@
 # frozen_string_literal: true
 
-RSpec.describe Gitlab::Backup::Cli::SourceContext do
+RSpec.describe Gitlab::Backup::Cli::Context::SourceContext do
   subject(:context) { described_class.new }
 
   let(:fake_gitlab_basepath) { Pathname.new(Dir.mktmpdir('gitlab', temp_path)) }
-- 
GitLab