summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/namespace.rb15
-rw-r--r--app/models/pages/lookup_path.rb11
-rw-r--r--app/models/pages/virtual_domain.rb7
-rw-r--r--app/models/pages_domain.rb6
-rw-r--r--app/models/project.rb26
-rw-r--r--app/models/project_pages_metadatum.rb25
6 files changed, 82 insertions, 8 deletions
diff --git a/app/models/namespace.rb b/app/models/namespace.rb
index 9a7c3dc03c3..fb90ddc1048 100644
--- a/app/models/namespace.rb
+++ b/app/models/namespace.rb
@@ -120,6 +120,13 @@ class Namespace < ApplicationRecord
uniquify = Uniquify.new
uniquify.string(path) { |s| Namespace.find_by_path_or_name(s) }
end
+
+ def find_by_pages_host(host)
+ gitlab_host = "." + Settings.pages.host.downcase
+ name = host.downcase.delete_suffix(gitlab_host)
+
+ Namespace.find_by_full_path(name)
+ end
end
def visibility_level_field
@@ -305,8 +312,16 @@ class Namespace < ApplicationRecord
aggregation_schedule.present?
end
+ def pages_virtual_domain
+ Pages::VirtualDomain.new(all_projects_with_pages, trim_prefix: full_path)
+ end
+
private
+ def all_projects_with_pages
+ all_projects.with_pages_deployed
+ end
+
def parent_changed?
parent_id_changed?
end
diff --git a/app/models/pages/lookup_path.rb b/app/models/pages/lookup_path.rb
index 1b3183a2a43..51c496c77d3 100644
--- a/app/models/pages/lookup_path.rb
+++ b/app/models/pages/lookup_path.rb
@@ -2,9 +2,10 @@
module Pages
class LookupPath
- def initialize(project, domain: nil)
+ def initialize(project, trim_prefix: nil, domain: nil)
@project = project
@domain = domain
+ @trim_prefix = trim_prefix || project.full_path
end
def project_id
@@ -28,11 +29,15 @@ module Pages
end
def prefix
- '/'
+ if project.pages_group_root?
+ '/'
+ else
+ project.full_path.delete_prefix(trim_prefix) + '/'
+ end
end
private
- attr_reader :project, :domain
+ attr_reader :project, :trim_prefix, :domain
end
end
diff --git a/app/models/pages/virtual_domain.rb b/app/models/pages/virtual_domain.rb
index 3a876dc06a2..7e42b8e6ae2 100644
--- a/app/models/pages/virtual_domain.rb
+++ b/app/models/pages/virtual_domain.rb
@@ -2,8 +2,9 @@
module Pages
class VirtualDomain
- def initialize(projects, domain: nil)
+ def initialize(projects, trim_prefix: nil, domain: nil)
@projects = projects
+ @trim_prefix = trim_prefix
@domain = domain
end
@@ -17,12 +18,12 @@ module Pages
def lookup_paths
projects.map do |project|
- project.pages_lookup_path(domain: domain)
+ project.pages_lookup_path(trim_prefix: trim_prefix, domain: domain)
end.sort_by(&:prefix).reverse
end
private
- attr_reader :projects, :domain
+ attr_reader :projects, :trim_prefix, :domain
end
end
diff --git a/app/models/pages_domain.rb b/app/models/pages_domain.rb
index 22a6bae7cf7..85abdbc8e07 100644
--- a/app/models/pages_domain.rb
+++ b/app/models/pages_domain.rb
@@ -186,11 +186,17 @@ class PagesDomain < ApplicationRecord
end
def pages_virtual_domain
+ return unless pages_deployed?
+
Pages::VirtualDomain.new([project], domain: self)
end
private
+ def pages_deployed?
+ project.project_pages_metadatum&.deployed?
+ end
+
def set_verification_code
return if self.verification_code.present?
diff --git a/app/models/project.rb b/app/models/project.rb
index 18afccf7ddc..1115b370448 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -104,6 +104,9 @@ class Project < ApplicationRecord
unless: :ci_cd_settings,
if: proc { ProjectCiCdSetting.available? }
+ after_create :create_project_pages_metadatum,
+ unless: :project_pages_metadatum
+
after_create :set_timestamps_for_create
after_update :update_forks_visibility_level
@@ -295,6 +298,8 @@ class Project < ApplicationRecord
has_many :external_pull_requests, inverse_of: :project
+ has_one :project_pages_metadatum, inverse_of: :project
+
accepts_nested_attributes_for :variables, allow_destroy: true
accepts_nested_attributes_for :project_feature, update_only: true
accepts_nested_attributes_for :import_data
@@ -425,6 +430,10 @@ class Project < ApplicationRecord
.where(project_ci_cd_settings: { group_runners_enabled: true })
end
+ scope :with_pages_deployed, -> do
+ where('EXISTS (?)', ProjectPagesMetadatum.project_scoped.deployed.select(1))
+ end
+
enum auto_cancel_pending_pipelines: { disabled: 0, enabled: 1 }
chronic_duration_attr :build_timeout_human_readable, :build_timeout,
@@ -1643,6 +1652,10 @@ class Project < ApplicationRecord
"#{url}/#{url_path}"
end
+ def pages_group_root?
+ pages_group_url == pages_url
+ end
+
def pages_subdomain
full_path.partition('/').first
end
@@ -1681,6 +1694,7 @@ class Project < ApplicationRecord
# Projects with a missing namespace cannot have their pages removed
return unless namespace
+ mark_pages_as_not_deployed unless destroyed?
::Projects::UpdatePagesConfigurationService.new(self).execute
# 1. We rename pages to temporary directory
@@ -1694,6 +1708,14 @@ class Project < ApplicationRecord
end
# rubocop: enable CodeReuse/ServiceClass
+ def mark_pages_as_deployed
+ ProjectPagesMetadatum.update_pages_deployed(self, true)
+ end
+
+ def mark_pages_as_not_deployed
+ ProjectPagesMetadatum.update_pages_deployed(self, false)
+ end
+
# rubocop:disable Gitlab/RailsLogger
def write_repository_config(gl_full_path: full_path)
# We'd need to keep track of project full path otherwise directory tree
@@ -2213,8 +2235,8 @@ class Project < ApplicationRecord
members.maintainers.order_recent_sign_in.limit(ACCESS_REQUEST_APPROVERS_TO_BE_NOTIFIED_LIMIT)
end
- def pages_lookup_path(domain: nil)
- Pages::LookupPath.new(self, domain: domain)
+ def pages_lookup_path(trim_prefix: nil, domain: nil)
+ Pages::LookupPath.new(self, trim_prefix: trim_prefix, domain: domain)
end
private
diff --git a/app/models/project_pages_metadatum.rb b/app/models/project_pages_metadatum.rb
new file mode 100644
index 00000000000..6774fb5c045
--- /dev/null
+++ b/app/models/project_pages_metadatum.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+class ProjectPagesMetadatum < ApplicationRecord
+ self.primary_key = :project_id
+
+ belongs_to :project, inverse_of: :project_pages_metadatum
+
+ scope :project_scoped, -> { where('projects.id=project_pages_metadata.project_id') }
+ scope :deployed, -> { where(deployed: true) }
+
+ def self.update_pages_deployed(project, flag)
+ flag = flag ? 'TRUE' : 'FALSE'
+
+ upsert = <<~SQL
+ INSERT INTO project_pages_metadata (project_id, deployed)
+ VALUES (#{project.id.to_i}, #{flag})
+ ON CONFLICT (project_id) DO UPDATE
+ SET deployed = EXCLUDED.deployed
+ SQL
+
+ connection_pool.with_connection do |connection|
+ connection.execute(upsert)
+ end
+ end
+end