debian-mirror-gitlab/lib/banzai/reference_parser/merge_request_parser.rb

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

58 lines
1.7 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
module Banzai
module ReferenceParser
2018-03-17 18:26:18 +05:30
class MergeRequestParser < IssuableParser
2021-06-08 01:23:25 +05:30
include Gitlab::Utils::StrongMemoize
self.reference_type = :merge_request
2021-09-04 01:27:46 +05:30
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
2018-03-17 18:26:18 +05:30
def records_for_nodes(nodes)
2023-05-27 22:25:52 +05:30
node_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]
}
]
node_includes << :milestone if context.options[:extended_preload]
2017-08-17 22:00:37 +05:30
@merge_requests_for_nodes ||= grouped_objects_for_nodes(
nodes,
2023-05-27 22:25:52 +05:30
MergeRequest.includes(node_includes),
2017-08-17 22:00:37 +05:30
self.class.data_attribute
)
end
2021-06-08 01:23:25 +05:30
2021-09-04 01:27:46 +05:30
def can_read_reference?(user, object)
2021-06-08 01:23:25 +05:30
memo = strong_memoize(:can_read_reference) { {} }
2021-09-04 01:27:46 +05:30
project_id = object.project_id
2021-06-08 01:23:25 +05:30
return memo[project_id] if memo.key?(project_id)
2021-09-04 01:27:46 +05:30
memo[project_id] = can?(user, :read_merge_request_iid, object)
end
def projects_for_nodes(nodes)
@projects_for_nodes ||=
grouped_objects_for_nodes(nodes, Project.includes(:project_feature, :group, :namespace), 'data-project')
2021-06-08 01:23:25 +05:30
end
end
end
end