2018-10-06 19:10:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-24 13:11:48 -04:00
|
|
|
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
|
2018-04-03 09:45:17 -04:00
|
|
|
attr_reader :context
|
2016-08-24 13:11:48 -04:00
|
|
|
|
2019-09-10 04:11:43 -04:00
|
|
|
ISSUE_REFERENCE_TYPE = '@data-reference-type="issue"'
|
|
|
|
MERGE_REQUEST_REFERENCE_TYPE = '@data-reference-type="merge_request"'
|
2018-10-15 04:36:07 -04:00
|
|
|
|
2018-04-03 09:45:17 -04:00
|
|
|
# context - An instance of Banzai::RenderContext.
|
|
|
|
def initialize(context)
|
|
|
|
@context = context
|
2016-08-24 13:11:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns Hash in the form { node => issuable_instance }
|
|
|
|
def extract(documents)
|
|
|
|
nodes = documents.flat_map do |document|
|
2018-10-15 04:36:07 -04:00
|
|
|
document.xpath(query)
|
2016-08-24 13:11:48 -04:00
|
|
|
end
|
|
|
|
|
2018-10-15 04:36:07 -04:00
|
|
|
# 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
|
2018-04-03 09:45:17 -04:00
|
|
|
|
2018-10-15 04:36:07 -04:00
|
|
|
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),
|
2018-04-03 09:45:17 -04:00
|
|
|
Banzai::ReferenceParser::MergeRequestParser.new(context)
|
2018-10-15 04:36:07 -04:00
|
|
|
]
|
|
|
|
end
|
2016-08-24 13:11:48 -04:00
|
|
|
|
2018-10-15 04:36:07 -04:00
|
|
|
def query
|
|
|
|
%Q(
|
|
|
|
descendant-or-self::a[contains(concat(" ", @class, " "), " gfm ")]
|
|
|
|
[#{reference_types.join(' or ')}]
|
2016-08-24 13:11:48 -04:00
|
|
|
)
|
2018-10-15 04:36:07 -04:00
|
|
|
end
|
2017-04-25 06:36:18 -04:00
|
|
|
|
2018-10-15 04:36:07 -04:00
|
|
|
def reference_types
|
|
|
|
[ISSUE_REFERENCE_TYPE, MERGE_REQUEST_REFERENCE_TYPE]
|
2016-08-24 13:11:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-09 08:09:24 -04:00
|
|
|
|
|
|
|
Banzai::IssuableExtractor.prepend_if_ee('EE::Banzai::IssuableExtractor')
|