diff options
author | Adam Niedzielski <adamsunday@gmail.com> | 2017-04-07 14:40:11 +0200 |
---|---|---|
committer | Adam Niedzielski <adamsunday@gmail.com> | 2017-04-10 12:42:20 +0200 |
commit | 3fa898549c52c603e32dcce16942edb505d9eabe (patch) | |
tree | 53adc63bb639bbf5fb2e26bfb7a51a9942e09104 | |
parent | c98add157732004d9a2eaa39770edf84eaca6896 (diff) | |
download | gitlab-ce-merge-request-parser-merge-requests-for-nodes.tar.gz |
Reuse "merge_requests_for_nodes" in "MergeRequestParser"merge-request-parser-merge-requests-for-nodes
-rw-r--r-- | lib/banzai/reference_parser/merge_request_parser.rb | 41 |
1 files changed, 30 insertions, 11 deletions
diff --git a/lib/banzai/reference_parser/merge_request_parser.rb b/lib/banzai/reference_parser/merge_request_parser.rb index 7d7dcce017e..84a28b33d7c 100644 --- a/lib/banzai/reference_parser/merge_request_parser.rb +++ b/lib/banzai/reference_parser/merge_request_parser.rb @@ -3,23 +3,42 @@ module Banzai class MergeRequestParser < BaseParser self.reference_type = :merge_request + def nodes_visible_to_user(user, nodes) + merge_requests = merge_requests_for_nodes(nodes) + + nodes.select do |node| + merge_request = merge_requests[node] + + merge_request && can?(user, :read_merge_request, merge_request.project) + end + end + + def referenced_by(nodes) + merge_requests = merge_requests_for_nodes(nodes) + + nodes.map { |node| merge_requests[node] }.compact.uniq + end + def merge_requests_for_nodes(nodes) @merge_requests_for_nodes ||= grouped_objects_for_nodes( nodes, - MergeRequest.all, + MergeRequest.includes( + :author, + :assignee, + { + # 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: :owner }, + { group: [:owners, :group_members] }, + :invited_groups, + :project_members + ] + }), self.class.data_attribute ) end - - def references_relation - MergeRequest.includes(:author, :assignee, :target_project) - end - - private - - def can_read_reference?(user, ref_project) - can?(user, :read_merge_request, ref_project) - end end end end |