File: project_namespace.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (72 lines) | stat: -rw-r--r-- 2,768 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true

module Namespaces
  class ProjectNamespace < Namespace
    self.allow_legacy_sti_class = true

    # These aliases are added to make it easier to sync parent/parent_id attribute with
    # 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 ProjectNamespace#sync_attributes_from_project
    alias_attribute :namespace, :parent
    alias_attribute :namespace_id, :parent_id
    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?, :pending_delete?, to: :project
    delegate :service_desk_alias_address, to: :project

    def self.sti_name
      'Project'
    end

    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)
      attribute_list = %w[name path namespace_id namespace visibility_level shared_runners_enabled organization_id]

      attributes_to_sync = project
                             .changes
                             .slice(*attribute_list)
                             .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

    # It's always 1 project but it has to be an AR relation
    def all_projects
      Project.where(id: project.id)
    end
  end
end

Namespaces::ProjectNamespace.prepend_mod