Class: Banzai::Filter::MilestoneReferenceFilter

Inherits:
AbstractReferenceFilter show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
lib/banzai/filter/milestone_reference_filter.rb

Overview

HTML filter that replaces milestone references with links.

Constant Summary

Constants inherited from AbstractReferenceFilter

AbstractReferenceFilter::REFERENCE_PLACEHOLDER, AbstractReferenceFilter::REFERENCE_PLACEHOLDER_PATTERN

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods inherited from AbstractReferenceFilter

#call, #current_parent_path, #current_project_namespace_path, #data_attributes_for, #find_for_paths, #find_object_cached, #find_object_from_link_cached, #from_ref_cached, #identifier, identifier, #object_class, #object_link_filter, #object_link_text_extras, object_name, #object_sym, object_sym, #parent_per_reference, parse_symbol, #record_identifier, #records_per_parent, references_in, #references_per_parent, #relation_for_paths, symbol_from_match, #url_for_object_cached, #wrap_link

Methods included from CrossProjectReference

#parent_from_ref

Methods inherited from ReferenceFilter

call, #call_and_update_nodes, #data_attribute, #each_node, #element_node?, #group, #ignore_ancestor_query, #initialize, #nodes, #project, #reference_class, #replace_link_node_with_href, #replace_link_node_with_text, #replace_text_when_pattern_matches, #skip_project_check?, #text_node?, #user, #validate, #yield_valid_link

Methods included from OutputSafety

#escape_once

Methods included from RequestStoreReferenceCache

#cached_call, #get_or_set_cache

Constructor Details

This class inherits a constructor from Banzai::Filter::ReferenceFilter

Class Method Details

.object_classObject


11
12
13
# File 'lib/banzai/filter/milestone_reference_filter.rb', line 11

def self.object_class
  Milestone
end

Instance Method Details

#find_milestone(project_ref, namespace_ref, milestone_id, milestone_name) ⇒ Object


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/banzai/filter/milestone_reference_filter.rb', line 71

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

#find_milestone_with_finder(parent, params) ⇒ Object


92
93
94
95
96
# File 'lib/banzai/filter/milestone_reference_filter.rb', line 92

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

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

#find_object(parent, id) ⇒ Object

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.


18
19
20
21
22
# File 'lib/banzai/filter/milestone_reference_filter.rb', line 18

def find_object(parent, id)
  return unless valid_context?(parent)

  find_milestone_with_finder(parent, id: id)
end

24
25
26
27
28
# File 'lib/banzai/filter/milestone_reference_filter.rb', line 24

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

  find_milestone_with_finder(parent, iid: iid)
end

#group_context?(parent) ⇒ Boolean

Returns:

  • (Boolean)

36
37
38
39
40
# File 'lib/banzai/filter/milestone_reference_filter.rb', line 36

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

#milestone_finder_params(parent, find_by_iid) ⇒ Object


98
99
100
101
102
103
104
105
106
# File 'lib/banzai/filter/milestone_reference_filter.rb', line 98

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

#milestone_params(iid, name) ⇒ Object


84
85
86
87
88
89
90
# File 'lib/banzai/filter/milestone_reference_filter.rb', line 84

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

122
123
124
125
126
127
128
129
130
131
# File 'lib/banzai/filter/milestone_reference_filter.rb', line 122

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

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

133
134
135
# File 'lib/banzai/filter/milestone_reference_filter.rb', line 133

def object_link_title(object, matches)
  nil
end

#project_context?(parent) ⇒ Boolean

Returns:

  • (Boolean)

42
43
44
45
46
# File 'lib/banzai/filter/milestone_reference_filter.rb', line 42

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

#references_in(text, pattern = Milestone.reference_pattern) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/banzai/filter/milestone_reference_filter.rb', line 48

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

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

    if milestone
      milestones[milestone.id] = yield match, milestone.id, $~[:project], $~[:namespace], $~
      "#{REFERENCE_PLACEHOLDER}#{milestone.id}"
    else
      match
    end
  end

  return text if milestones.empty?

  escape_with_placeholders(unescaped_html, milestones)
end

#self_and_ancestors_ids(parent) ⇒ Object


108
109
110
111
112
113
114
# File 'lib/banzai/filter/milestone_reference_filter.rb', line 108

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

#url_for_object(milestone, project) ⇒ Object


116
117
118
119
120
# File 'lib/banzai/filter/milestone_reference_filter.rb', line 116

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

#valid_context?(parent) ⇒ Boolean

Returns:

  • (Boolean)

30
31
32
33
34
# File 'lib/banzai/filter/milestone_reference_filter.rb', line 30

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