2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-22 09:35:28 +00:00
|
|
|
class Namespace < ApplicationRecord
|
2016-10-06 21:17:11 +00:00
|
|
|
include CacheMarkdownField
|
2015-02-05 22:20:55 +00:00
|
|
|
include Sortable
|
2017-07-17 10:40:17 +00:00
|
|
|
include Gitlab::VisibilityLevel
|
2016-10-31 11:00:53 +00:00
|
|
|
include Routable
|
2017-06-01 21:36:04 +00:00
|
|
|
include AfterCommitQueue
|
2017-07-05 00:19:02 +00:00
|
|
|
include Storage::LegacyNamespace
|
2017-11-24 10:45:19 +00:00
|
|
|
include Gitlab::SQL::Pattern
|
2018-08-15 19:48:05 +00:00
|
|
|
include FeatureGate
|
2018-09-11 15:31:34 +00:00
|
|
|
include FromUnion
|
2019-03-12 10:15:33 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2020-04-15 00:09:27 +00:00
|
|
|
include IgnorableColumns
|
2021-02-05 15:09:28 +00:00
|
|
|
include Namespaces::Traversal::Recursive
|
2021-03-19 21:08:58 +00:00
|
|
|
include Namespaces::Traversal::Linear
|
2021-05-13 09:10:44 +00:00
|
|
|
include EachBatch
|
2020-04-15 00:09:27 +00:00
|
|
|
|
2021-03-25 21:09:13 +00:00
|
|
|
ignore_column :delayed_project_removal, remove_with: '14.1', remove_after: '2021-05-22'
|
2021-11-12 15:12:37 +00:00
|
|
|
# Temporary column used for back-filling project namespaces.
|
|
|
|
# Remove it once the back-filling of all project namespaces is done.
|
|
|
|
ignore_column :tmp_project_id, remove_with: '14.7', remove_after: '2022-01-22'
|
2021-03-25 21:09:13 +00:00
|
|
|
|
2021-10-19 00:10:29 +00:00
|
|
|
# Tells ActiveRecord not to store the full class name, in order to save some space
|
2021-09-08 21:10:58 +00:00
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/merge_requests/69794
|
|
|
|
self.store_full_sti_class = false
|
|
|
|
self.store_full_class_name = false
|
|
|
|
|
2017-02-06 15:16:50 +00:00
|
|
|
# Prevent users from creating unreasonably deep level of nesting.
|
|
|
|
# The number 20 was taken based on maximum nesting level of
|
|
|
|
# Android repo (15) + some extra backup.
|
|
|
|
NUMBER_OF_ANCESTORS_ALLOWED = 20
|
|
|
|
|
2021-10-13 15:12:51 +00:00
|
|
|
SR_DISABLED_AND_UNOVERRIDABLE = 'disabled_and_unoverridable'
|
|
|
|
SR_DISABLED_WITH_OVERRIDE = 'disabled_with_override'
|
|
|
|
SR_ENABLED = 'enabled'
|
|
|
|
SHARED_RUNNERS_SETTINGS = [SR_DISABLED_AND_UNOVERRIDABLE, SR_DISABLED_WITH_OVERRIDE, SR_ENABLED].freeze
|
2021-07-21 12:09:35 +00:00
|
|
|
URL_MAX_LENGTH = 255
|
2020-09-30 15:09:46 +00:00
|
|
|
|
2021-10-28 18:14:18 +00:00
|
|
|
PATH_TRAILING_VIOLATIONS = %w[.git .atom .].freeze
|
|
|
|
|
2016-10-06 21:17:11 +00:00
|
|
|
cache_markdown_field :description, pipeline: :description
|
|
|
|
|
2017-06-08 15:16:27 +00:00
|
|
|
has_many :projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
|
2016-11-22 16:58:10 +00:00
|
|
|
has_many :project_statistics
|
2020-07-16 15:09:38 +00:00
|
|
|
has_one :namespace_settings, inverse_of: :namespace, class_name: 'NamespaceSetting', autosave: true
|
2018-01-31 16:51:05 +00:00
|
|
|
|
2018-05-28 11:07:28 +00:00
|
|
|
has_many :runner_namespaces, inverse_of: :namespace, class_name: 'Ci::RunnerNamespace'
|
2018-05-02 15:43:20 +00:00
|
|
|
has_many :runners, through: :runner_namespaces, source: :runner, class_name: 'Ci::Runner'
|
2021-08-03 12:09:42 +00:00
|
|
|
has_many :pending_builds, class_name: 'Ci::PendingBuild'
|
2021-01-11 12:10:41 +00:00
|
|
|
has_one :onboarding_progress
|
2018-05-02 15:43:20 +00:00
|
|
|
|
2018-01-31 16:51:05 +00:00
|
|
|
# This should _not_ be `inverse_of: :namespace`, because that would also set
|
|
|
|
# `user.namespace` when this user creates a group with themselves as `owner`.
|
2021-10-04 12:11:58 +00:00
|
|
|
# TODO: can this be moved into the UserNamespace class?
|
|
|
|
# evaluate in issue https://gitlab.com/gitlab-org/gitlab/-/issues/341070
|
2012-11-22 18:34:16 +00:00
|
|
|
belongs_to :owner, class_name: "User"
|
|
|
|
|
2016-10-31 11:00:53 +00:00
|
|
|
belongs_to :parent, class_name: "Namespace"
|
2021-10-20 15:12:43 +00:00
|
|
|
has_many :children, -> { where(type: Group.sti_name) }, class_name: "Namespace", foreign_key: :parent_id
|
2020-06-30 12:08:57 +00:00
|
|
|
has_many :custom_emoji, inverse_of: :namespace
|
2017-06-08 15:16:27 +00:00
|
|
|
has_one :chat_team, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
|
2019-06-12 21:11:14 +00:00
|
|
|
has_one :root_storage_statistics, class_name: 'Namespace::RootStorageStatistics'
|
|
|
|
has_one :aggregation_schedule, class_name: 'Namespace::AggregationSchedule'
|
2021-01-05 18:10:25 +00:00
|
|
|
has_one :package_setting_relation, inverse_of: :namespace, class_name: 'PackageSetting'
|
2016-10-31 11:00:53 +00:00
|
|
|
|
2021-03-29 18:09:37 +00:00
|
|
|
has_one :admin_note, inverse_of: :namespace
|
|
|
|
accepts_nested_attributes_for :admin_note, update_only: true
|
|
|
|
|
2021-09-16 12:09:35 +00:00
|
|
|
validates :owner, presence: true, if: ->(n) { n.owner_required? }
|
2015-02-03 05:15:44 +00:00
|
|
|
validates :name,
|
2015-12-07 21:17:24 +00:00
|
|
|
presence: true,
|
2019-06-24 16:15:03 +00:00
|
|
|
length: { maximum: 255 }
|
2015-02-03 05:15:44 +00:00
|
|
|
|
2016-12-02 12:54:57 +00:00
|
|
|
validates :description, length: { maximum: 255 }
|
2021-10-02 12:10:14 +00:00
|
|
|
|
2015-02-03 05:15:44 +00:00
|
|
|
validates :path,
|
2015-12-07 21:17:12 +00:00
|
|
|
presence: true,
|
2021-10-02 12:10:14 +00:00
|
|
|
length: { maximum: URL_MAX_LENGTH }
|
|
|
|
|
|
|
|
validates :path, namespace_path: true, if: ->(n) { !n.project_namespace? }
|
|
|
|
# Project path validator is used for project namespaces for now to assure
|
|
|
|
# compatibility with project paths
|
|
|
|
# Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/341764
|
|
|
|
validates :path, project_path: true, if: ->(n) { n.project_namespace? }
|
2012-11-22 18:34:16 +00:00
|
|
|
|
2020-07-06 09:09:20 +00:00
|
|
|
# Introduce minimal path length of 2 characters.
|
|
|
|
# Allow change of other attributes without forcing users to
|
|
|
|
# rename their user or group. At the same time prevent changing
|
|
|
|
# the path without complying with new 2 chars requirement.
|
|
|
|
# Issue https://gitlab.com/gitlab-org/gitlab/-/issues/225214
|
2021-10-02 12:10:14 +00:00
|
|
|
#
|
|
|
|
# For ProjectNamespace we don't check minimal path length to keep
|
|
|
|
# compatibility with existing project restrictions.
|
|
|
|
# Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/341764
|
|
|
|
validates :path, length: { minimum: 2 }, if: :enforce_minimum_path_length?
|
2020-07-06 09:09:20 +00:00
|
|
|
|
2019-12-20 09:24:38 +00:00
|
|
|
validates :max_artifacts_size, numericality: { only_integer: true, greater_than: 0, allow_nil: true }
|
|
|
|
|
2021-09-23 09:09:51 +00:00
|
|
|
validate :validate_parent_type, if: -> { Feature.enabled?(:validate_namespace_parent_type, default_enabled: :yaml) }
|
2021-10-27 15:13:41 +00:00
|
|
|
|
|
|
|
# ProjectNamespaces excluded as they are not meant to appear in the group hierarchy at the moment.
|
|
|
|
validate :nesting_level_allowed, unless: -> { project_namespace? }
|
|
|
|
validate :changing_shared_runners_enabled_is_allowed, unless: -> { project_namespace? }
|
|
|
|
validate :changing_allow_descendants_override_disabled_shared_runners_is_allowed, unless: -> { project_namespace? }
|
2017-02-06 15:16:50 +00:00
|
|
|
|
2012-11-22 18:34:16 +00:00
|
|
|
delegate :name, to: :owner, allow_nil: true, prefix: true
|
2019-04-16 16:16:52 +00:00
|
|
|
delegate :avatar_url, to: :owner, allow_nil: true
|
2012-11-22 18:34:16 +00:00
|
|
|
|
2016-11-21 13:33:58 +00:00
|
|
|
after_commit :refresh_access_of_projects_invited_groups, on: :update, if: -> { previous_changes.key?('share_with_group_lock') }
|
2016-06-22 21:04:51 +00:00
|
|
|
|
2017-09-01 23:49:09 +00:00
|
|
|
before_create :sync_share_with_group_lock_with_parent
|
|
|
|
before_update :sync_share_with_group_lock_with_parent, if: :parent_changed?
|
2019-01-15 21:05:36 +00:00
|
|
|
after_update :force_share_with_group_lock_on_descendants, if: -> { saved_change_to_share_with_group_lock? && share_with_group_lock? }
|
2017-09-01 23:49:09 +00:00
|
|
|
|
2017-07-05 00:19:02 +00:00
|
|
|
# Legacy Storage specific hooks
|
|
|
|
|
2021-10-04 18:12:46 +00:00
|
|
|
after_update :move_dir, if: :saved_change_to_path_or_parent?, unless: -> { is_a?(Namespaces::ProjectNamespace) }
|
2017-02-16 07:50:05 +00:00
|
|
|
before_destroy(prepend: true) { prepare_for_destroy }
|
2012-11-21 05:54:05 +00:00
|
|
|
after_destroy :rm_dir
|
2021-05-13 09:10:44 +00:00
|
|
|
after_commit :expire_child_caches, on: :update, if: -> {
|
|
|
|
Feature.enabled?(:cached_route_lookups, self, type: :ops, default_enabled: :yaml) &&
|
|
|
|
saved_change_to_name? || saved_change_to_path? || saved_change_to_parent_id?
|
|
|
|
}
|
2012-11-23 06:11:09 +00:00
|
|
|
|
2021-09-23 12:11:29 +00:00
|
|
|
# TODO: change to `type: Namespaces::UserNamespace.sti_name` when
|
|
|
|
# working on issue https://gitlab.com/gitlab-org/gitlab/-/issues/341070
|
|
|
|
scope :user_namespaces, -> { where(type: [nil, Namespaces::UserNamespace.sti_name]) }
|
2021-10-12 21:09:47 +00:00
|
|
|
# TODO: this can be simplified with `type != 'Project'` when working on issue
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/341070
|
2021-11-02 12:12:25 +00:00
|
|
|
scope :without_project_namespaces, -> { where(Namespace.arel_table[:type].is_distinct_from(Namespaces::ProjectNamespace.sti_name)) }
|
2020-02-20 09:09:13 +00:00
|
|
|
scope :sort_by_type, -> { order(Gitlab::Database.nulls_first_order(:type)) }
|
2020-10-12 03:08:20 +00:00
|
|
|
scope :include_route, -> { includes(:route) }
|
2021-04-19 18:09:09 +00:00
|
|
|
scope :by_parent, -> (parent) { where(parent_id: parent) }
|
|
|
|
scope :filter_by_path, -> (query) { where('lower(path) = :query', query: query.downcase) }
|
2012-11-23 04:24:09 +00:00
|
|
|
|
2016-11-22 16:58:10 +00:00
|
|
|
scope :with_statistics, -> do
|
|
|
|
joins('LEFT JOIN project_statistics ps ON ps.namespace_id = namespaces.id')
|
|
|
|
.group('namespaces.id')
|
|
|
|
.select(
|
|
|
|
'namespaces.*',
|
|
|
|
'COALESCE(SUM(ps.storage_size), 0) AS storage_size',
|
|
|
|
'COALESCE(SUM(ps.repository_size), 0) AS repository_size',
|
2019-02-13 22:38:11 +00:00
|
|
|
'COALESCE(SUM(ps.wiki_size), 0) AS wiki_size',
|
2020-06-26 15:08:45 +00:00
|
|
|
'COALESCE(SUM(ps.snippets_size), 0) AS snippets_size',
|
2016-11-22 16:58:10 +00:00
|
|
|
'COALESCE(SUM(ps.lfs_objects_size), 0) AS lfs_objects_size',
|
2019-05-02 16:04:15 +00:00
|
|
|
'COALESCE(SUM(ps.build_artifacts_size), 0) AS build_artifacts_size',
|
2021-10-28 12:10:22 +00:00
|
|
|
'COALESCE(SUM(ps.pipeline_artifacts_size), 0) AS pipeline_artifacts_size',
|
2020-11-14 00:09:32 +00:00
|
|
|
'COALESCE(SUM(ps.packages_size), 0) AS packages_size',
|
|
|
|
'COALESCE(SUM(ps.uploads_size), 0) AS uploads_size'
|
2016-11-22 16:58:10 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-06-17 15:10:03 +00:00
|
|
|
scope :sorted_by_similarity_and_parent_id_desc, -> (search) do
|
|
|
|
order_expression = Gitlab::Database::SimilarityScore.build_expression(search: search, rules: [
|
|
|
|
{ column: arel_table["path"], multiplier: 1 },
|
|
|
|
{ column: arel_table["name"], multiplier: 0.7 }
|
|
|
|
])
|
|
|
|
reorder(order_expression.desc, Namespace.arel_table['parent_id'].desc.nulls_last, Namespace.arel_table['id'].desc)
|
|
|
|
end
|
|
|
|
|
2021-01-25 12:09:07 +00:00
|
|
|
# Make sure that the name is same as strong_memoize name in root_ancestor
|
|
|
|
# method
|
2021-04-08 15:09:06 +00:00
|
|
|
attr_writer :root_ancestor, :emails_disabled_memoized
|
2021-01-25 12:09:07 +00:00
|
|
|
|
2015-03-24 13:53:30 +00:00
|
|
|
class << self
|
2021-09-08 21:10:58 +00:00
|
|
|
def sti_class_for(type_name)
|
|
|
|
case type_name
|
2021-09-22 12:12:04 +00:00
|
|
|
when Group.sti_name
|
2021-09-08 21:10:58 +00:00
|
|
|
Group
|
2021-09-22 12:12:04 +00:00
|
|
|
when Namespaces::ProjectNamespace.sti_name
|
2021-09-08 21:10:58 +00:00
|
|
|
Namespaces::ProjectNamespace
|
2021-09-22 12:12:04 +00:00
|
|
|
when Namespaces::UserNamespace.sti_name
|
2021-09-23 12:11:29 +00:00
|
|
|
Namespaces::UserNamespace
|
2021-09-08 21:10:58 +00:00
|
|
|
else
|
|
|
|
Namespace
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-24 13:53:30 +00:00
|
|
|
def by_path(path)
|
2015-12-15 02:53:52 +00:00
|
|
|
find_by('lower(path) = :value', value: path.downcase)
|
2015-03-24 13:53:30 +00:00
|
|
|
end
|
|
|
|
|
2018-10-30 10:53:01 +00:00
|
|
|
# Case insensitive search for namespace by path or name
|
2015-03-24 13:53:30 +00:00
|
|
|
def find_by_path_or_name(path)
|
|
|
|
find_by("lower(path) = :path OR lower(name) = :path", path: path.downcase)
|
|
|
|
end
|
|
|
|
|
2016-03-04 11:06:25 +00:00
|
|
|
# Searches for namespaces matching the given query.
|
|
|
|
#
|
2020-06-16 03:08:24 +00:00
|
|
|
# This method uses ILIKE on PostgreSQL.
|
2016-03-04 11:06:25 +00:00
|
|
|
#
|
2020-06-16 03:08:24 +00:00
|
|
|
# query - The search query as a String.
|
2016-03-04 11:06:25 +00:00
|
|
|
#
|
2020-06-16 03:08:24 +00:00
|
|
|
# Returns an ActiveRecord::Relation.
|
2020-10-26 03:08:41 +00:00
|
|
|
def search(query, include_parents: false)
|
|
|
|
if include_parents
|
2021-11-02 12:12:25 +00:00
|
|
|
without_project_namespaces.where(id: Route.for_routable_type(Namespace.name).fuzzy_search(query, [Route.arel_table[:path], Route.arel_table[:name]]).select(:source_id))
|
2020-10-26 03:08:41 +00:00
|
|
|
else
|
2021-11-02 12:12:25 +00:00
|
|
|
without_project_namespaces.fuzzy_search(query, [:path, :name])
|
2020-10-26 03:08:41 +00:00
|
|
|
end
|
2015-03-24 13:53:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def clean_path(path)
|
2015-04-17 10:03:54 +00:00
|
|
|
path = path.dup
|
2015-04-21 13:49:16 +00:00
|
|
|
# Get the email username by removing everything after an `@` sign.
|
2016-08-25 16:48:08 +00:00
|
|
|
path.gsub!(/@.*\z/, "")
|
2015-04-21 13:49:16 +00:00
|
|
|
# Remove everything that's not in the list of allowed characters.
|
2016-08-25 16:48:08 +00:00
|
|
|
path.gsub!(/[^a-zA-Z0-9_\-\.]/, "")
|
|
|
|
# Remove trailing violations ('.atom', '.git', or '.')
|
2021-10-28 18:14:18 +00:00
|
|
|
loop do
|
|
|
|
orig = path
|
|
|
|
PATH_TRAILING_VIOLATIONS.each { |ext| path = path.chomp(ext) }
|
|
|
|
break if orig == path
|
|
|
|
end
|
|
|
|
|
2016-08-25 16:48:08 +00:00
|
|
|
# Remove leading violations ('-')
|
2021-10-28 18:14:18 +00:00
|
|
|
path.gsub!(/\A\-+/, "")
|
2015-03-24 13:53:30 +00:00
|
|
|
|
2015-04-21 10:00:08 +00:00
|
|
|
# Users with the great usernames of "." or ".." would end up with a blank username.
|
2015-06-03 12:57:12 +00:00
|
|
|
# Work around that by setting their username to "blank", followed by a counter.
|
2015-04-21 10:00:08 +00:00
|
|
|
path = "blank" if path.blank?
|
|
|
|
|
2017-02-03 12:55:50 +00:00
|
|
|
uniquify = Uniquify.new
|
2017-02-16 07:05:10 +00:00
|
|
|
uniquify.string(path) { |s| Namespace.find_by_path_or_name(s) }
|
2015-03-24 13:53:30 +00:00
|
|
|
end
|
2019-09-25 09:06:04 +00:00
|
|
|
|
2020-08-25 12:04:30 +00:00
|
|
|
def clean_name(value)
|
|
|
|
value.scan(Gitlab::Regex.group_name_regex_chars).join(' ')
|
|
|
|
end
|
|
|
|
|
2019-09-25 09:06:04 +00:00
|
|
|
def find_by_pages_host(host)
|
|
|
|
gitlab_host = "." + Settings.pages.host.downcase
|
2019-12-11 12:08:10 +00:00
|
|
|
host = host.downcase
|
|
|
|
return unless host.ends_with?(gitlab_host)
|
2019-09-25 09:06:04 +00:00
|
|
|
|
2019-12-11 12:08:10 +00:00
|
|
|
name = host.delete_suffix(gitlab_host)
|
2020-03-20 09:09:22 +00:00
|
|
|
Namespace.where(parent_id: nil).by_path(name)
|
2019-09-25 09:06:04 +00:00
|
|
|
end
|
2021-02-18 18:10:41 +00:00
|
|
|
|
|
|
|
def top_most
|
|
|
|
where(parent_id: nil)
|
|
|
|
end
|
2012-11-27 06:31:15 +00:00
|
|
|
end
|
|
|
|
|
2021-01-05 18:10:25 +00:00
|
|
|
def package_settings
|
|
|
|
package_setting_relation || build_package_setting_relation
|
|
|
|
end
|
|
|
|
|
2020-03-02 12:07:57 +00:00
|
|
|
def default_branch_protection
|
|
|
|
super || Gitlab::CurrentSettings.default_branch_protection
|
|
|
|
end
|
|
|
|
|
2017-07-17 10:40:17 +00:00
|
|
|
def visibility_level_field
|
|
|
|
:visibility_level
|
|
|
|
end
|
|
|
|
|
2012-11-22 18:34:16 +00:00
|
|
|
def to_param
|
2016-10-31 11:00:53 +00:00
|
|
|
full_path
|
2012-11-22 18:34:16 +00:00
|
|
|
end
|
2012-11-23 04:11:09 +00:00
|
|
|
|
|
|
|
def human_name
|
|
|
|
owner_name
|
|
|
|
end
|
2012-11-23 06:11:09 +00:00
|
|
|
|
2016-05-09 19:41:48 +00:00
|
|
|
def any_project_has_container_registry_tags?
|
2021-04-29 12:09:58 +00:00
|
|
|
all_projects.includes(:container_repositories).any?(&:has_container_registry_tags?)
|
2018-10-02 03:13:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def first_project_with_container_registry_tags
|
|
|
|
all_projects.find(&:has_container_registry_tags?)
|
2016-05-09 19:41:48 +00:00
|
|
|
end
|
|
|
|
|
2017-08-01 05:45:04 +00:00
|
|
|
def send_update_instructions
|
|
|
|
projects.each do |project|
|
2019-04-23 09:30:18 +00:00
|
|
|
project.send_move_instructions("#{full_path_before_last_save}/#{project.path}")
|
2017-08-01 05:45:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-15 13:25:37 +00:00
|
|
|
def kind
|
2021-09-21 15:12:11 +00:00
|
|
|
return 'group' if group_namespace?
|
|
|
|
return 'project' if project_namespace?
|
2014-11-14 14:06:39 +00:00
|
|
|
|
2021-09-08 21:10:58 +00:00
|
|
|
'user' # defaults to user
|
2019-04-16 16:16:52 +00:00
|
|
|
end
|
|
|
|
|
2021-09-21 15:12:11 +00:00
|
|
|
def group_namespace?
|
2021-09-08 21:10:58 +00:00
|
|
|
type == Group.sti_name
|
|
|
|
end
|
|
|
|
|
2021-09-21 15:12:11 +00:00
|
|
|
def project_namespace?
|
2021-09-08 21:10:58 +00:00
|
|
|
type == Namespaces::ProjectNamespace.sti_name
|
|
|
|
end
|
|
|
|
|
2021-09-21 15:12:11 +00:00
|
|
|
def user_namespace?
|
2021-09-08 21:10:58 +00:00
|
|
|
# That last bit ensures we're considered a user namespace as a default
|
2021-09-21 15:12:11 +00:00
|
|
|
type.nil? || type == Namespaces::UserNamespace.sti_name || !(group_namespace? || project_namespace?)
|
2020-05-07 15:09:29 +00:00
|
|
|
end
|
|
|
|
|
2021-09-16 12:09:35 +00:00
|
|
|
def owner_required?
|
2021-09-21 15:12:11 +00:00
|
|
|
user_namespace?
|
2021-09-16 12:09:35 +00:00
|
|
|
end
|
|
|
|
|
2014-11-14 14:06:39 +00:00
|
|
|
def find_fork_of(project)
|
2019-02-08 12:19:53 +00:00
|
|
|
return unless project.fork_network
|
2017-10-03 15:06:09 +00:00
|
|
|
|
2018-09-20 22:40:15 +00:00
|
|
|
if Gitlab::SafeRequestStore.active?
|
|
|
|
forks_in_namespace = Gitlab::SafeRequestStore.fetch("namespaces:#{id}:forked_projects") do
|
2017-11-30 13:26:08 +00:00
|
|
|
Hash.new do |found_forks, project|
|
|
|
|
found_forks[project] = project.fork_network.find_forks_in(projects).first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
forks_in_namespace[project]
|
|
|
|
else
|
|
|
|
project.fork_network.find_forks_in(projects).first
|
|
|
|
end
|
2014-11-14 14:06:39 +00:00
|
|
|
end
|
2016-06-22 21:04:51 +00:00
|
|
|
|
2019-08-15 17:37:36 +00:00
|
|
|
# any ancestor can disable emails for all descendants
|
|
|
|
def emails_disabled?
|
2021-04-08 15:09:06 +00:00
|
|
|
strong_memoize(:emails_disabled_memoized) do
|
2020-01-16 21:08:24 +00:00
|
|
|
if parent_id
|
|
|
|
self_and_ancestors.where(emails_disabled: true).exists?
|
|
|
|
else
|
|
|
|
!!emails_disabled
|
|
|
|
end
|
2019-08-15 17:37:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-01 23:49:48 +00:00
|
|
|
def lfs_enabled?
|
|
|
|
# User namespace will always default to the global setting
|
|
|
|
Gitlab.config.lfs.enabled
|
|
|
|
end
|
|
|
|
|
2020-07-06 09:09:20 +00:00
|
|
|
def any_project_with_shared_runners_enabled?
|
2017-01-20 11:25:53 +00:00
|
|
|
projects.with_shared_runners.any?
|
|
|
|
end
|
|
|
|
|
2017-02-07 13:55:42 +00:00
|
|
|
def user_ids_for_project_authorizations
|
|
|
|
[owner_id]
|
|
|
|
end
|
|
|
|
|
2017-04-03 15:48:17 +00:00
|
|
|
# Includes projects from this namespace and projects from all subgroups
|
|
|
|
# that belongs to this namespace
|
|
|
|
def all_projects
|
2021-06-30 15:08:27 +00:00
|
|
|
if Feature.enabled?(:recursive_approach_for_all_projects, default_enabled: :yaml)
|
2021-09-21 15:12:11 +00:00
|
|
|
namespace = user_namespace? ? self : self_and_descendant_ids
|
2021-06-30 15:08:27 +00:00
|
|
|
Project.where(namespace: namespace)
|
|
|
|
else
|
|
|
|
Project.inside_path(full_path)
|
|
|
|
end
|
2017-04-03 15:48:17 +00:00
|
|
|
end
|
|
|
|
|
2017-04-11 14:21:52 +00:00
|
|
|
def has_parent?
|
2020-05-22 09:08:09 +00:00
|
|
|
parent_id.present? || parent.present?
|
2017-04-11 14:21:52 +00:00
|
|
|
end
|
|
|
|
|
2017-08-29 10:15:19 +00:00
|
|
|
def subgroup?
|
|
|
|
has_parent?
|
|
|
|
end
|
|
|
|
|
2018-03-06 14:15:19 +00:00
|
|
|
# Overridden on EE module
|
2018-02-19 19:06:16 +00:00
|
|
|
def multiple_issue_boards_available?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2021-03-29 18:09:37 +00:00
|
|
|
# Deprecated, use #licensed_feature_available? instead. Remove once Namespace#feature_available? isn't used anymore.
|
|
|
|
def feature_available?(feature)
|
|
|
|
licensed_feature_available?(feature)
|
|
|
|
end
|
|
|
|
|
2019-07-11 17:22:35 +00:00
|
|
|
# Overridden in EE::Namespace
|
2021-03-29 18:09:37 +00:00
|
|
|
def licensed_feature_available?(_feature)
|
2019-07-11 17:22:35 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2019-04-23 09:30:18 +00:00
|
|
|
def full_path_before_last_save
|
|
|
|
if parent_id_before_last_save.nil?
|
|
|
|
path_before_last_save
|
2018-02-06 00:10:58 +00:00
|
|
|
else
|
2019-04-23 09:30:18 +00:00
|
|
|
previous_parent = Group.find_by(id: parent_id_before_last_save)
|
|
|
|
previous_parent.full_path + '/' + path_before_last_save
|
2018-02-06 00:10:58 +00:00
|
|
|
end
|
2018-01-23 19:03:02 +00:00
|
|
|
end
|
|
|
|
|
2018-04-06 15:23:49 +00:00
|
|
|
def refresh_project_authorizations
|
|
|
|
owner.refresh_authorized_projects
|
|
|
|
end
|
|
|
|
|
2019-03-12 10:15:33 +00:00
|
|
|
def auto_devops_enabled?
|
|
|
|
first_auto_devops_config[:status]
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_auto_devops_config
|
|
|
|
return { scope: :group, status: auto_devops_enabled } unless auto_devops_enabled.nil?
|
|
|
|
|
|
|
|
strong_memoize(:first_auto_devops_config) do
|
|
|
|
if has_parent?
|
|
|
|
parent.first_auto_devops_config
|
|
|
|
else
|
|
|
|
{ scope: :instance, status: Gitlab::CurrentSettings.auto_devops_enabled? }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-02 14:44:39 +00:00
|
|
|
def aggregation_scheduled?
|
|
|
|
aggregation_schedule.present?
|
|
|
|
end
|
|
|
|
|
2019-09-25 09:06:04 +00:00
|
|
|
def pages_virtual_domain
|
2020-02-20 09:09:13 +00:00
|
|
|
Pages::VirtualDomain.new(
|
2020-11-10 21:08:51 +00:00
|
|
|
all_projects_with_pages.includes(:route, :project_feature, pages_metadatum: :pages_deployment),
|
2020-02-20 09:09:13 +00:00
|
|
|
trim_prefix: full_path
|
|
|
|
)
|
2019-09-25 09:06:04 +00:00
|
|
|
end
|
|
|
|
|
2020-08-27 15:10:21 +00:00
|
|
|
def any_project_with_pages_deployed?
|
|
|
|
all_projects.with_pages_deployed.any?
|
|
|
|
end
|
|
|
|
|
2019-10-08 12:06:01 +00:00
|
|
|
def closest_setting(name)
|
|
|
|
self_and_ancestors(hierarchy_order: :asc)
|
|
|
|
.find { |n| !n.read_attribute(name).nil? }
|
|
|
|
.try(name)
|
|
|
|
end
|
|
|
|
|
2020-04-22 15:09:27 +00:00
|
|
|
def actual_plan
|
|
|
|
Plan.default
|
|
|
|
end
|
|
|
|
|
2021-03-26 12:09:15 +00:00
|
|
|
def paid?
|
|
|
|
root? && actual_plan.paid?
|
|
|
|
end
|
|
|
|
|
2020-04-22 15:09:27 +00:00
|
|
|
def actual_limits
|
|
|
|
# We default to PlanLimits.new otherwise a lot of specs would fail
|
|
|
|
# On production each plan should already have associated limits record
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/issues/36037
|
2020-05-05 21:09:42 +00:00
|
|
|
actual_plan.actual_limits
|
2020-04-22 15:09:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def actual_plan_name
|
|
|
|
actual_plan.name
|
|
|
|
end
|
|
|
|
|
2020-09-30 15:09:46 +00:00
|
|
|
def changing_shared_runners_enabled_is_allowed
|
|
|
|
return unless new_record? || changes.has_key?(:shared_runners_enabled)
|
|
|
|
|
2021-10-13 15:12:51 +00:00
|
|
|
if shared_runners_enabled && has_parent? && parent.shared_runners_setting == SR_DISABLED_AND_UNOVERRIDABLE
|
2020-09-30 15:09:46 +00:00
|
|
|
errors.add(:shared_runners_enabled, _('cannot be enabled because parent group has shared Runners disabled'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def changing_allow_descendants_override_disabled_shared_runners_is_allowed
|
|
|
|
return unless new_record? || changes.has_key?(:allow_descendants_override_disabled_shared_runners)
|
|
|
|
|
|
|
|
if shared_runners_enabled && !new_record?
|
|
|
|
errors.add(:allow_descendants_override_disabled_shared_runners, _('cannot be changed if shared runners are enabled'))
|
|
|
|
end
|
|
|
|
|
2021-10-13 15:12:51 +00:00
|
|
|
if allow_descendants_override_disabled_shared_runners && has_parent? && parent.shared_runners_setting == SR_DISABLED_AND_UNOVERRIDABLE
|
2020-09-30 15:09:46 +00:00
|
|
|
errors.add(:allow_descendants_override_disabled_shared_runners, _('cannot be enabled because parent group does not allow it'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def shared_runners_setting
|
|
|
|
if shared_runners_enabled
|
2021-10-13 15:12:51 +00:00
|
|
|
SR_ENABLED
|
2020-09-30 15:09:46 +00:00
|
|
|
else
|
|
|
|
if allow_descendants_override_disabled_shared_runners
|
2021-10-13 15:12:51 +00:00
|
|
|
SR_DISABLED_WITH_OVERRIDE
|
2020-09-30 15:09:46 +00:00
|
|
|
else
|
2021-10-13 15:12:51 +00:00
|
|
|
SR_DISABLED_AND_UNOVERRIDABLE
|
2020-09-30 15:09:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def shared_runners_setting_higher_than?(other_setting)
|
2021-10-13 15:12:51 +00:00
|
|
|
if other_setting == SR_ENABLED
|
2020-09-30 15:09:46 +00:00
|
|
|
false
|
2021-10-13 15:12:51 +00:00
|
|
|
elsif other_setting == SR_DISABLED_WITH_OVERRIDE
|
|
|
|
shared_runners_setting == SR_ENABLED
|
|
|
|
elsif other_setting == SR_DISABLED_AND_UNOVERRIDABLE
|
|
|
|
shared_runners_setting == SR_ENABLED || shared_runners_setting == SR_DISABLED_WITH_OVERRIDE
|
2020-09-30 15:09:46 +00:00
|
|
|
else
|
|
|
|
raise ArgumentError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-23 12:10:26 +00:00
|
|
|
def root?
|
|
|
|
!has_parent?
|
|
|
|
end
|
|
|
|
|
2021-02-18 15:09:43 +00:00
|
|
|
def recent?
|
|
|
|
created_at >= 90.days.ago
|
|
|
|
end
|
|
|
|
|
2021-05-17 18:10:42 +00:00
|
|
|
def issue_repositioning_disabled?
|
|
|
|
Feature.enabled?(:block_issue_repositioning, self, type: :ops, default_enabled: :yaml)
|
|
|
|
end
|
|
|
|
|
2021-11-10 18:12:35 +00:00
|
|
|
def project_namespace_creation_enabled?
|
|
|
|
Feature.enabled?(:create_project_namespace_on_project_create, self, default_enabled: :yaml)
|
|
|
|
end
|
|
|
|
|
2016-06-22 21:04:51 +00:00
|
|
|
private
|
|
|
|
|
2021-05-13 09:10:44 +00:00
|
|
|
def expire_child_caches
|
|
|
|
Namespace.where(id: descendants).each_batch do |namespaces|
|
|
|
|
namespaces.touch_all
|
|
|
|
end
|
|
|
|
|
|
|
|
all_projects.each_batch do |projects|
|
|
|
|
projects.touch_all
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-25 09:06:04 +00:00
|
|
|
def all_projects_with_pages
|
|
|
|
all_projects.with_pages_deployed
|
|
|
|
end
|
|
|
|
|
2019-04-23 09:30:18 +00:00
|
|
|
def parent_changed?
|
|
|
|
parent_id_changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def saved_change_to_parent?
|
|
|
|
saved_change_to_parent_id?
|
|
|
|
end
|
|
|
|
|
|
|
|
def saved_change_to_path_or_parent?
|
2019-01-15 21:05:36 +00:00
|
|
|
saved_change_to_path? || saved_change_to_parent_id?
|
2018-02-06 00:10:58 +00:00
|
|
|
end
|
|
|
|
|
2016-11-21 13:33:58 +00:00
|
|
|
def refresh_access_of_projects_invited_groups
|
2021-07-26 12:10:08 +00:00
|
|
|
if Feature.enabled?(:specialized_worker_for_group_lock_update_auth_recalculation)
|
|
|
|
Project
|
|
|
|
.where(namespace_id: id)
|
|
|
|
.joins(:project_group_links)
|
|
|
|
.distinct
|
|
|
|
.find_each do |project|
|
|
|
|
AuthorizedProjectUpdate::ProjectRecalculateWorker.perform_async(project.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Until we compare the inconsistency rates of the new specialized worker and
|
|
|
|
# the old approach, we still run AuthorizedProjectsWorker
|
|
|
|
# but with some delay and lower urgency as a safety net.
|
2021-10-28 18:14:18 +00:00
|
|
|
enqueue_jobs_for_groups_requiring_authorizations_refresh(priority: UserProjectAccessChangedService::LOW_PRIORITY)
|
2021-07-26 12:10:08 +00:00
|
|
|
else
|
2021-10-28 18:14:18 +00:00
|
|
|
enqueue_jobs_for_groups_requiring_authorizations_refresh(priority: UserProjectAccessChangedService::HIGH_PRIORITY)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def enqueue_jobs_for_groups_requiring_authorizations_refresh(priority:)
|
|
|
|
groups_requiring_authorizations_refresh = Group
|
|
|
|
.joins(project_group_links: :project)
|
|
|
|
.where(projects: { namespace_id: id })
|
|
|
|
.distinct
|
|
|
|
|
|
|
|
groups_requiring_authorizations_refresh.find_each do |group|
|
|
|
|
group.refresh_members_authorized_projects(
|
|
|
|
blocking: false,
|
|
|
|
priority: priority
|
|
|
|
)
|
2021-07-26 12:10:08 +00:00
|
|
|
end
|
2016-11-21 13:33:58 +00:00
|
|
|
end
|
2016-10-31 11:00:53 +00:00
|
|
|
|
2017-02-06 15:16:50 +00:00
|
|
|
def nesting_level_allowed
|
|
|
|
if ancestors.count > Group::NUMBER_OF_ANCESTORS_ALLOWED
|
2021-09-16 15:12:47 +00:00
|
|
|
errors.add(:parent_id, _('has too deep level of nesting'))
|
2017-02-06 15:16:50 +00:00
|
|
|
end
|
|
|
|
end
|
2017-09-01 23:49:09 +00:00
|
|
|
|
2021-03-01 15:10:53 +00:00
|
|
|
def validate_parent_type
|
2021-09-16 15:12:47 +00:00
|
|
|
unless has_parent?
|
2021-09-21 15:12:11 +00:00
|
|
|
if project_namespace?
|
2021-09-16 15:12:47 +00:00
|
|
|
errors.add(:parent_id, _('must be set for a project namespace'))
|
|
|
|
end
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-09-21 15:12:11 +00:00
|
|
|
if parent.project_namespace?
|
2021-09-16 15:12:47 +00:00
|
|
|
errors.add(:parent_id, _('project namespace cannot be the parent of another namespace'))
|
|
|
|
end
|
2021-03-01 15:10:53 +00:00
|
|
|
|
2021-09-21 15:12:11 +00:00
|
|
|
if user_namespace?
|
2021-10-20 15:12:43 +00:00
|
|
|
errors.add(:parent_id, _('cannot be used for user namespace'))
|
2021-09-21 15:12:11 +00:00
|
|
|
elsif group_namespace?
|
|
|
|
errors.add(:parent_id, _('user namespace cannot be the parent of another namespace')) if parent.user_namespace?
|
2021-03-01 15:10:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-01 23:49:09 +00:00
|
|
|
def sync_share_with_group_lock_with_parent
|
2017-09-06 00:10:30 +00:00
|
|
|
if parent&.share_with_group_lock?
|
2017-09-01 23:49:09 +00:00
|
|
|
self.share_with_group_lock = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def force_share_with_group_lock_on_descendants
|
2017-09-15 13:34:41 +00:00
|
|
|
# We can't use `descendants.update_all` since Rails will throw away the WITH
|
|
|
|
# RECURSIVE statement. We also can't use WHERE EXISTS since we can't use
|
|
|
|
# different table aliases, hence we're just using WHERE IN. Since we have a
|
|
|
|
# maximum of 20 nested groups this should be fine.
|
|
|
|
Namespace.where(id: descendants.select(:id))
|
|
|
|
.update_all(share_with_group_lock: true)
|
2017-09-01 23:49:09 +00:00
|
|
|
end
|
2017-12-08 17:42:43 +00:00
|
|
|
|
2017-12-21 17:32:08 +00:00
|
|
|
def write_projects_repository_config
|
|
|
|
all_projects.find_each do |project|
|
2021-07-29 06:10:15 +00:00
|
|
|
project.set_full_path
|
2018-12-21 15:31:14 +00:00
|
|
|
project.track_project_repository
|
2017-12-19 19:42:51 +00:00
|
|
|
end
|
|
|
|
end
|
2021-10-02 12:10:14 +00:00
|
|
|
|
|
|
|
def enforce_minimum_path_length?
|
|
|
|
path_changed? && !project_namespace?
|
|
|
|
end
|
2012-11-22 18:34:16 +00:00
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
Namespace.prepend_mod_with('Namespace')
|