debian-mirror-gitlab/app/models/work_items/parent_link.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

100 lines
3 KiB
Ruby
Raw Normal View History

2022-07-23 23:45:48 +05:30
# frozen_string_literal: true
module WorkItems
class ParentLink < ApplicationRecord
self.table_name = 'work_item_parent_links'
MAX_CHILDREN = 100
2022-08-13 15:12:31 +05:30
PARENT_TYPES = [:issue, :incident].freeze
2022-07-23 23:45:48 +05:30
belongs_to :work_item
belongs_to :work_item_parent, class_name: 'WorkItem'
2022-08-13 15:12:31 +05:30
validates :work_item_parent, presence: true
validates :work_item, presence: true, uniqueness: true
2023-03-04 22:38:38 +05:30
validate :validate_hierarchy_restrictions
validate :validate_cyclic_reference
2022-07-23 23:45:48 +05:30
validate :validate_same_project
validate :validate_max_children
2022-08-27 11:52:29 +05:30
validate :validate_confidentiality
2023-03-04 22:38:38 +05:30
scope :for_parents, ->(parent_ids) { where(work_item_parent_id: parent_ids) }
2022-08-27 11:52:29 +05:30
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
end
2022-07-23 23:45:48 +05:30
private
def validate_same_project
return if work_item.nil? || work_item_parent.nil?
if work_item.resource_parent != work_item_parent.resource_parent
2022-08-13 15:12:31 +05:30
errors.add :work_item_parent, _('parent must be in the same project as child.')
2022-07-23 23:45:48 +05:30
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
2022-08-13 15:12:31 +05:30
errors.add :work_item_parent, _('parent already has maximum number of children.')
2022-07-23 23:45:48 +05:30
end
end
2022-08-27 11:52:29 +05:30
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
2023-03-04 22:38:38 +05:30
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
2022-07-23 23:45:48 +05:30
end
end