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

related_work_item_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: fb0069541fb08548b762713918e5472acae660c7 (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
# frozen_string_literal: true

module WorkItems
  class RelatedWorkItemLink < ApplicationRecord
    include LinkableItem

    self.table_name = 'issue_links'

    MAX_LINKS_COUNT = 100

    belongs_to :source, class_name: 'WorkItem'
    belongs_to :target, class_name: 'WorkItem'

    validate :validate_related_link_restrictions

    class << self
      extend ::Gitlab::Utils::Override

      # Used as issuable table name for calculating blocked and blocking count in IssuableLink
      override :issuable_type
      def issuable_type
        :issue
      end

      override :issuable_name
      def issuable_name
        'work item'
      end
    end

    private

    def validate_related_link_restrictions
      return unless source && target

      source_type = source.work_item_type
      target_type = target.work_item_type

      return if link_restriction_exists?(source_type.id, target_type.id)

      errors.add :source, format(
        s_('%{source_type} cannot be related to %{type_type}'),
        source_type: source_type.name.downcase.pluralize,
        type_type: target_type.name.downcase.pluralize
      )
    end

    def link_restriction_exists?(source_type_id, target_type_id)
      source_restriction = find_restriction(source_type_id, target_type_id)
      return true if source_restriction.present?
      return false if source_type_id == target_type_id

      find_restriction(target_type_id, source_type_id).present?
    end

    def find_restriction(source_type_id, target_type_id)
      ::WorkItems::RelatedLinkRestriction.find_by_source_type_id_and_target_type_id_and_link_type(
        source_type_id,
        target_type_id,
        link_type
      )
    end
  end
end

WorkItems::RelatedWorkItemLink.prepend_mod