Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/namespaces/project_namespace.rb')
-rw-r--r--app/models/namespaces/project_namespace.rb39
1 files changed, 37 insertions, 2 deletions
diff --git a/app/models/namespaces/project_namespace.rb b/app/models/namespaces/project_namespace.rb
index bf23fc21124..288c5c0d2d4 100644
--- a/app/models/namespaces/project_namespace.rb
+++ b/app/models/namespaces/project_namespace.rb
@@ -6,10 +6,10 @@ module Namespaces
# project.namespace/project.namespace_id attribute.
#
# TODO: we can remove these attribute aliases when we no longer need to sync these with project model,
- # see project#sync_attributes
+ # see ProjectNamespace#sync_attributes_from_project
alias_attribute :namespace, :parent
alias_attribute :namespace_id, :parent_id
- has_one :project, foreign_key: :project_namespace_id, inverse_of: :project_namespace
+ has_one :project, inverse_of: :project_namespace
delegate :execute_hooks, :execute_integrations, :group, to: :project, allow_nil: true
delegate :external_references_supported?, :default_issues_tracker?, to: :project
@@ -21,5 +21,40 @@ module Namespaces
def self.polymorphic_name
'Namespaces::ProjectNamespace'
end
+
+ def self.create_from_project!(project)
+ return unless project.new_record?
+ return unless project.namespace
+
+ proj_namespace = project.project_namespace || project.build_project_namespace
+ project.project_namespace.sync_attributes_from_project(project)
+ proj_namespace.save!
+ proj_namespace
+ end
+
+ def sync_attributes_from_project(project)
+ attributes_to_sync = project
+ .changes
+ .slice(*%w[name path namespace_id namespace visibility_level shared_runners_enabled])
+ .transform_values { |val| val[1] }
+
+ # if visibility_level is not set explicitly for project, it defaults to 0,
+ # but for namespace visibility_level defaults to 20,
+ # so it gets out of sync right away if we do not set it explicitly when creating the project namespace
+ attributes_to_sync['visibility_level'] ||= project.visibility_level if project.new_record?
+
+ # when a project is associated with a group while the group is created we need to ensure we associate the new
+ # group with the project namespace as well.
+ # E.g.
+ # project = create(:project) <- project is saved
+ # create(:group, projects: [project]) <- associate project with a group that is not yet created.
+ if attributes_to_sync.has_key?('namespace_id') &&
+ attributes_to_sync['namespace_id'].blank? &&
+ project.namespace.present?
+ attributes_to_sync['parent'] = project.namespace
+ end
+
+ assign_attributes(attributes_to_sync)
+ end
end
end