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
|
|
|
|
QUERY = %q(
|
|
|
|
descendant-or-self::a[contains(concat(" ", @class, " "), " gfm ")]
|
|
|
|
[@data-reference-type="issue" or @data-reference-type="merge_request"]
|
|
|
|
).freeze
|
|
|
|
|
2018-04-03 09:45:17 -04:00
|
|
|
attr_reader :context
|
2016-08-24 13:11:48 -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|
|
|
|
|
document.xpath(QUERY)
|
|
|
|
end
|
|
|
|
|
2018-04-03 09:45:17 -04:00
|
|
|
issue_parser = Banzai::ReferenceParser::IssueParser.new(context)
|
|
|
|
|
|
|
|
merge_request_parser =
|
|
|
|
Banzai::ReferenceParser::MergeRequestParser.new(context)
|
2016-08-24 13:11:48 -04:00
|
|
|
|
2017-12-01 04:21:05 -05:00
|
|
|
issuables_for_nodes = issue_parser.records_for_nodes(nodes).merge(
|
|
|
|
merge_request_parser.records_for_nodes(nodes)
|
2016-08-24 13:11:48 -04:00
|
|
|
)
|
2017-04-25 06:36:18 -04:00
|
|
|
|
2017-04-26 01:11:49 -04:00
|
|
|
# The project for the issue/MR might be pending for deletion!
|
2017-04-25 14:29:36 -04:00
|
|
|
# Filter them out because we don't care about them.
|
2017-04-26 01:11:49 -04:00
|
|
|
issuables_for_nodes.select { |node, issuable| issuable.project }
|
2016-08-24 13:11:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|