summaryrefslogtreecommitdiff
path: root/lib/banzai/filter/milestone_reference_filter.rb
blob: fce042e894685097df5fdf2f079312df91c023a1 (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
126
127
128
129
130
131
132
# frozen_string_literal: true

module Banzai
  module Filter
    # HTML filter that replaces milestone references with links.
    class MilestoneReferenceFilter < AbstractReferenceFilter
      include Gitlab::Utils::StrongMemoize

      self.reference_type = :milestone

      def self.object_class
        Milestone
      end

      # Links to project milestones contain the IID, but when we're handling
      # 'regular' references, we need to use the global ID to disambiguate
      # between group and project milestones.
      def find_object(parent, id)
        return unless valid_context?(parent)

        find_milestone_with_finder(parent, id: id)
      end

      def find_object_from_link(parent, iid)
        return unless valid_context?(parent)

        find_milestone_with_finder(parent, iid: iid)
      end

      def valid_context?(parent)
        strong_memoize(:valid_context) do
          group_context?(parent) || project_context?(parent)
        end
      end

      def group_context?(parent)
        strong_memoize(:group_context) do
          parent.is_a?(Group)
        end
      end

      def project_context?(parent)
        strong_memoize(:project_context) do
          parent.is_a?(Project)
        end
      end

      def references_in(text, pattern = Milestone.reference_pattern)
        # We'll handle here the references that follow the `reference_pattern`.
        # Other patterns (for example, the link pattern) are handled by the
        # default implementation.
        return super(text, pattern) if pattern != Milestone.reference_pattern

        text.gsub(pattern) do |match|
          milestone = find_milestone($~[:project], $~[:namespace], $~[:milestone_iid], $~[:milestone_name])

          if milestone
            yield match, milestone.id, $~[:project], $~[:namespace], $~
          else
            match
          end
        end
      end

      def find_milestone(project_ref, namespace_ref, milestone_id, milestone_name)
        project_path = full_project_path(namespace_ref, project_ref)

        # Returns group if project is not found by path
        parent = parent_from_ref(project_path)

        return unless parent

        milestone_params = milestone_params(milestone_id, milestone_name)

        find_milestone_with_finder(parent, milestone_params)
      end

      def milestone_params(iid, name)
        if name
          { name: name.tr('"', '') }
        else
          { iid: iid.to_i }
        end
      end

      def find_milestone_with_finder(parent, params)
        finder_params = milestone_finder_params(parent, params[:iid].present?)

        MilestonesFinder.new(finder_params).find_by(params)
      end

      def milestone_finder_params(parent, find_by_iid)
        { order: nil, state: 'all' }.tap do |params|
          params[:project_ids] = parent.id if project_context?(parent)

          # We don't support IID lookups because IIDs can clash between
          # group/project milestones and group/subgroup milestones.
          params[:group_ids] = self_and_ancestors_ids(parent) unless find_by_iid
        end
      end

      def self_and_ancestors_ids(parent)
        if group_context?(parent)
          parent.self_and_ancestors.select(:id)
        elsif project_context?(parent)
          parent.group&.self_and_ancestors&.select(:id)
        end
      end

      def url_for_object(milestone, project)
        Gitlab::Routing
          .url_helpers
          .milestone_url(milestone, only_path: context[:only_path])
      end

      def object_link_text(object, matches)
        milestone_link = escape_once(super)
        reference = object.project&.to_reference(project)

        if reference.present?
          "#{milestone_link} <i>in #{reference}</i>".html_safe
        else
          milestone_link
        end
      end

      def object_link_title(object, matches)
        nil
      end
    end
  end
end