Class: Banzai::ReferenceParser::MergeRequestParser

Inherits:
IssuableParser show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
lib/banzai/reference_parser/merge_request_parser.rb

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods inherited from IssuableParser

#referenced_by

Methods inherited from BaseParser

#can?, #collection_cache_key, #collection_objects_for_ids, data_attribute, #find_projects_for_hash_keys, #gather_attributes_per_project, #gather_references, #grouped_objects_for_nodes, #initialize, #nodes_user_can_reference, #process, #project_for_node, #referenced_by, #references_relation, #unique_attribute_values

Constructor Details

This class inherits a constructor from Banzai::ReferenceParser::BaseParser

Instance Method Details

#can_read_reference?(user, object) ⇒ Boolean

Returns:

  • (Boolean)

37
38
39
40
41
42
43
44
45
# File 'lib/banzai/reference_parser/merge_request_parser.rb', line 37

def can_read_reference?(user, object)
  memo = strong_memoize(:can_read_reference) { {} }

  project_id = object.project_id

  return memo[project_id] if memo.key?(project_id)

  memo[project_id] = can?(user, :read_merge_request_iid, object)
end

#nodes_visible_to_user(user, nodes) ⇒ Object


10
11
12
13
14
15
16
17
18
19
# File 'lib/banzai/reference_parser/merge_request_parser.rb', line 10

def nodes_visible_to_user(user, nodes)
  merge_request_nodes = nodes.select { |node| node.has_attribute?(self.class.data_attribute) }
  records = projects_for_nodes(merge_request_nodes)

  merge_request_nodes.select do |node|
    project = records[node]

    project && can_read_reference?(user, project)
  end
end

#projects_for_nodes(nodes) ⇒ Object


47
48
49
50
# File 'lib/banzai/reference_parser/merge_request_parser.rb', line 47

def projects_for_nodes(nodes)
  @projects_for_nodes ||=
    grouped_objects_for_nodes(nodes, Project.includes(:project_feature, :group, :namespace), 'data-project')
end

#records_for_nodes(nodes) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/banzai/reference_parser/merge_request_parser.rb', line 21

def records_for_nodes(nodes)
  @merge_requests_for_nodes ||= grouped_objects_for_nodes(
    nodes,
    MergeRequest.includes(
      :author,
      :assignees,
      {
        # These associations are primarily used for checking permissions.
        # Eager loading these ensures we don't end up running dozens of
        # queries in this process.
        target_project: [{ namespace: :route }, :project_feature, :route]
      }),
    self.class.data_attribute
  )
end