From 5ad263afe4bdd1a00cf51a596abe2475690eb63a Mon Sep 17 00:00:00 2001
From: Gabriel Mazetto <gabriel@gitlab.com>
Date: Mon, 15 Jul 2024 13:53:03 +0200
Subject: [PATCH] Decouple `upload_path` in `SourceContext`

---
 .../lib/gitlab/backup/cli/source_context.rb   |  7 +++--
 .../spec/fixtures/gitlab.yml                  |  2 +-
 .../gitlab/backup/cli/source_context_spec.rb  | 26 +++++++++++++++++++
 3 files changed, 32 insertions(+), 3 deletions(-)

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
index d845bd2e573f..c5cc9b2caec6 100644
--- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/source_context.rb
+++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/source_context.rb
@@ -17,6 +17,7 @@ class SourceContext
         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
@@ -94,8 +95,10 @@ def terraform_state_path
 
         # Upload basepath
         def upload_path
-          # TODO: Use configuration solver
-          File.join(Gitlab.config.uploads.storage_path, 'uploads')
+          path = gitlab_config.dig(env, 'uploads', 'storage_path') ||
+            gitlab_basepath.join(DEFAULT_UPLOADS_PATH)
+
+          absolute_path(path).join('uploads')
         end
 
         def env
diff --git a/gems/gitlab-backup-cli/spec/fixtures/gitlab.yml b/gems/gitlab-backup-cli/spec/fixtures/gitlab.yml
index 2581e09b3a94..b72c82705cf9 100644
--- a/gems/gitlab-backup-cli/spec/fixtures/gitlab.yml
+++ b/gems/gitlab-backup-cli/spec/fixtures/gitlab.yml
@@ -53,7 +53,7 @@ test:
         aws_secret_access_key: AWS_SECRET_ACCESS_KEY
         region: us-east-1
   uploads:
-    storage_path: tmp/tests/public
+    storage_path: /tmp/gitlab/full/public
     object_store:
       enabled: false
       connection:
diff --git a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/source_context_spec.rb b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/source_context_spec.rb
index 3a8fa242c286..431b7d3606e8 100644
--- a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/source_context_spec.rb
+++ b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/source_context_spec.rb
@@ -251,6 +251,32 @@
     end
   end
 
+  describe '#upload_path' do
+    context 'with a missing configuration value' do
+      it 'returns the default value in full path' do
+        use_gitlab_config_fixture('gitlab-missingconfigs.yml')
+
+        expect(context.upload_path).to eq(fake_gitlab_basepath.join('public/uploads'))
+      end
+    end
+
+    context 'with a relative path configured in gitlab.yml' do
+      it 'returns a full path based on gitlab basepath' do
+        use_gitlab_config_fixture('gitlab-relativepaths.yml')
+
+        expect(context.upload_path).to eq(fake_gitlab_basepath.join('tmp/tests/public/uploads'))
+      end
+    end
+
+    context 'with a full path configured in gitlab.yml' do
+      it 'returns a full path as configured in gitlab.yml' do
+        use_gitlab_config_fixture('gitlab.yml')
+
+        expect(context.upload_path).to eq(Pathname('/tmp/gitlab/full/public/uploads'))
+      end
+    end
+  end
+
   describe '#gitlab_shared_path' do
     context 'with shared path not configured in gitlab.yml' do
       it 'raises an error' do
-- 
GitLab