BigW Consortium Gitlab

issue_parser.rb 1.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
module Banzai
  module ReferenceParser
    class IssueParser < BaseParser
      self.reference_type = :issue

      def nodes_visible_to_user(user, nodes)
        # It is not possible to check access rights for external issue trackers
        return nodes if project && project.external_issue_tracker

        issues = issues_for_nodes(nodes)

12 13
        readable_issues = Ability.
          issues_readable_by_user(issues.values, user).to_set
14

15
        nodes.select do |node|
16
          readable_issues.include?(issues[node])
17 18 19 20 21 22
        end
      end

      def referenced_by(nodes)
        issues = issues_for_nodes(nodes)

23
        nodes.map { |node| issues[node] }.compact.uniq
24 25 26 27 28
      end

      def issues_for_nodes(nodes)
        @issues_for_nodes ||= grouped_objects_for_nodes(
          nodes,
29 30
          Issue.all.includes(
            :author,
31
            :assignees,
32 33 34 35 36 37 38 39 40 41 42 43
            {
              # These associations are primarily used for checking permissions.
              # Eager loading these ensures we don't end up running dozens of
              # queries in this process.
              project: [
                { namespace: :owner },
                { group: [:owners, :group_members] },
                :invited_groups,
                :project_members
              ]
            }
          ),
44 45 46 47 48 49
          self.class.data_attribute
        )
      end
    end
  end
end