From 3ef229df51784a61d573255af21f9512bb4c99cc Mon Sep 17 00:00:00 2001
From: Naman Jagdish Gala <ngala@gitlab.com>
Date: Wed, 12 Feb 2025 22:12:09 +0000
Subject: [PATCH] Fix: Removing all domains does not reset primary domain

Related: https://gitlab.com/gitlab-org/gitlab/-/issues/514767

Changelog: fixed
---
 .../pages/reset_pages_default_domain_redirect_worker.rb      | 4 +++-
 .../pages/reset_pages_default_domain_redirect_worker_spec.rb | 5 +++--
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/app/workers/pages/reset_pages_default_domain_redirect_worker.rb b/app/workers/pages/reset_pages_default_domain_redirect_worker.rb
index d6d3755a1934c..4349fdb7fa2e0 100644
--- a/app/workers/pages/reset_pages_default_domain_redirect_worker.rb
+++ b/app/workers/pages/reset_pages_default_domain_redirect_worker.rb
@@ -13,7 +13,9 @@ def handle_event(event)
 
       return unless project_settings
       return unless project_settings.pages_primary_domain
-      return unless event.data['domain'] == project_settings.pages_primary_domain
+
+      primary_domain = URI.parse(project_settings.pages_primary_domain).host
+      return unless event.data['domain'] == primary_domain
 
       project_settings.update!(pages_primary_domain: nil)
     end
diff --git a/spec/workers/pages/reset_pages_default_domain_redirect_worker_spec.rb b/spec/workers/pages/reset_pages_default_domain_redirect_worker_spec.rb
index f2f6cd11ac3e0..7492d2d8bf438 100644
--- a/spec/workers/pages/reset_pages_default_domain_redirect_worker_spec.rb
+++ b/spec/workers/pages/reset_pages_default_domain_redirect_worker_spec.rb
@@ -4,12 +4,13 @@
 
 RSpec.describe Pages::ResetPagesDefaultDomainRedirectWorker, feature_category: :pages do
   let_it_be_with_reload(:project) { create(:project) }
+  let_it_be(:primary_domain_url_with_scheme) { 'https://primary.domain.com' }
   let_it_be(:primary_domain_url) { 'primary.domain.com' }
   let_it_be(:non_primary_domain_url) { 'non-primary.domain.com' }
   let_it_be(:primary_domain) { create(:pages_domain, project: project, domain: primary_domain_url) }
   let_it_be(:non_primary_domain) { create(:pages_domain, project: project, domain: non_primary_domain_url) }
   let_it_be(:project_setting) do
-    create(:project_setting, project: project, pages_primary_domain: primary_domain_url)
+    create(:project_setting, project: project, pages_primary_domain: primary_domain_url_with_scheme)
   end
 
   let(:event) do
@@ -33,7 +34,7 @@
 
       expect { use_event }
         .to change { project.reload.project_setting.pages_primary_domain }
-              .from(primary_domain_url).to(nil)
+              .from(primary_domain_url_with_scheme).to(nil)
     end
   end
 
-- 
GitLab