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

parent_link.rb « work_items « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ea7755b03b4e12d2cd07813e85a14e46cbba3352 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# frozen_string_literal: true

module WorkItems
  class ParentLink < ApplicationRecord
    include RelativePositioning

    self.table_name = 'work_item_parent_links'

    MAX_CHILDREN = 100

    belongs_to :work_item
    belongs_to :work_item_parent, class_name: 'WorkItem'

    validates :work_item_parent, presence: true
    validates :work_item, presence: true, uniqueness: true
    validate :validate_hierarchy_restrictions
    validate :validate_cyclic_reference
    validate :validate_same_project
    validate :validate_max_children
    validate :validate_confidentiality
    validate :check_existing_related_link

    scope :for_parents, ->(parent_ids) { where(work_item_parent_id: parent_ids) }
    scope :for_children, ->(children_ids) { where(work_item: children_ids) }

    class << self
      def has_public_children?(parent_id)
        joins(:work_item).where(work_item_parent_id: parent_id, 'issues.confidential': false).exists?
      end

      def has_confidential_parent?(id)
        link = find_by_work_item_id(id)
        return false unless link

        link.work_item_parent.confidential?
      end

      def relative_positioning_query_base(parent_link)
        where(work_item_parent_id: parent_link.work_item_parent_id)
      end

      def relative_positioning_parent_column
        :work_item_parent_id
      end

      def for_work_item(work_item)
        find_or_initialize_by(work_item: work_item)
      end
    end

    private

    def validate_same_project
      return if work_item.nil? || work_item_parent.nil?

      if work_item.resource_parent != work_item_parent.resource_parent
        errors.add :work_item_parent, _('parent must be in the same project as child.')
      end
    end

    def validate_max_children
      return unless work_item_parent

      max = persisted? ? MAX_CHILDREN : MAX_CHILDREN - 1
      if work_item_parent.child_links.count > max
        errors.add :work_item_parent, _('parent already has maximum number of children.')
      end
    end

    def validate_confidentiality
      return unless work_item_parent && work_item

      if work_item_parent.confidential? && !work_item.confidential?
        errors.add :work_item, _("cannot assign a non-confidential work item to a confidential "\
                                 "parent. Make the work item confidential and try again.")
      end
    end

    def validate_hierarchy_restrictions
      return unless work_item && work_item_parent

      restriction = ::WorkItems::HierarchyRestriction
        .find_by_parent_type_id_and_child_type_id(work_item_parent.work_item_type_id, work_item.work_item_type_id)

      if restriction.nil?
        errors.add :work_item, _('is not allowed to add this type of parent')
        return
      end

      validate_depth(restriction.maximum_depth)
    end

    def validate_depth(depth)
      return unless depth
      return if work_item.work_item_type_id != work_item_parent.work_item_type_id

      if work_item_parent.same_type_base_and_ancestors.count + work_item.same_type_descendants_depth > depth
        errors.add :work_item, _('reached maximum depth')
      end
    end

    def validate_cyclic_reference
      return unless work_item_parent&.id && work_item&.id

      if work_item.id == work_item_parent.id
        errors.add :work_item, _('is not allowed to point to itself')
      end

      if work_item_parent.ancestors.detect { |ancestor| work_item.id == ancestor.id }
        errors.add :work_item, _('is already present in ancestors')
      end
    end

    def check_existing_related_link
      return unless work_item && work_item_parent

      existing_link = WorkItems::RelatedWorkItemLink.for_items(work_item, work_item_parent)
      return if existing_link.none?

      errors.add(:work_item, _('cannot assign a linked work item as a parent'))
    end
  end
end

WorkItems::ParentLink.prepend_mod