blob: 34b6ca99e32530df84137e7b78075529529496f4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# frozen_string_literal: true
module Banzai
# Extract references to issuables from multiple documents
# This populates RequestStore cache used in Banzai::ReferenceParser::IssueParser
# and Banzai::ReferenceParser::MergeRequestParser
# Populating the cache should happen before processing documents one-by-one
# so we can avoid N+1 queries problem
class IssuableExtractor
attr_reader :context
ISSUE_REFERENCE_TYPE = '@data-reference-type="issue"'
MERGE_REQUEST_REFERENCE_TYPE = '@data-reference-type="merge_request"'
# context - An instance of Banzai::RenderContext.
def initialize(context)
@context = context
end
# Returns Hash in the form { node => issuable_instance }
def extract(documents)
nodes = documents.flat_map do |document|
document.xpath(query)
end
# The project or group for the issuable might be pending for deletion!
# Filter them out because we don't care about them.
issuables_for_nodes(nodes).select { |node, issuable| issuable.project || issuable.group }
end
private
def issuables_for_nodes(nodes)
parsers.each_with_object({}) do |parser, result|
result.merge!(parser.records_for_nodes(nodes))
end
end
def parsers
[
Banzai::ReferenceParser::IssueParser.new(context),
Banzai::ReferenceParser::MergeRequestParser.new(context)
]
end
def query
%Q(
descendant-or-self::a[contains(concat(" ", @class, " "), " gfm ")]
[#{reference_types.join(' or ')}]
)
end
def reference_types
[ISSUE_REFERENCE_TYPE, MERGE_REQUEST_REFERENCE_TYPE]
end
end
end
Banzai::IssuableExtractor.prepend_mod_with('Banzai::IssuableExtractor')
|