Commit 5e65d498 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'ce-7341-epics-extractor' into 'master'

Prepare Banzai to work with group issuables

See merge request gitlab-org/gitlab-ce!22711
parents dd807d46 1d7737ae
...@@ -18,7 +18,7 @@ module Banzai ...@@ -18,7 +18,7 @@ module Banzai
issuables = extractor.extract([doc]) issuables = extractor.extract([doc])
issuables.each do |node, issuable| issuables.each do |node, issuable|
next if !can_read_cross_project? && issuable.project != project next if !can_read_cross_project? && cross_reference?(issuable)
if VISIBLE_STATES.include?(issuable.state) && issuable_reference?(node.inner_html, issuable) if VISIBLE_STATES.include?(issuable.state) && issuable_reference?(node.inner_html, issuable)
node.content += " (#{issuable.state})" node.content += " (#{issuable.state})"
...@@ -31,7 +31,14 @@ module Banzai ...@@ -31,7 +31,14 @@ module Banzai
private private
def issuable_reference?(text, issuable) def issuable_reference?(text, issuable)
text == issuable.reference_link_text(project || group) CGI.unescapeHTML(text) == issuable.reference_link_text(project || group)
end
def cross_reference?(issuable)
return true if issuable.project != project
return true if issuable.respond_to?(:group) && issuable.group != group
false
end end
def can_read_cross_project? def can_read_cross_project?
......
...@@ -9,13 +9,11 @@ module Banzai ...@@ -9,13 +9,11 @@ module Banzai
# so we can avoid N+1 queries problem # so we can avoid N+1 queries problem
class IssuableExtractor class IssuableExtractor
QUERY = %q(
descendant-or-self::a[contains(concat(" ", @class, " "), " gfm ")]
[@data-reference-type="issue" or @data-reference-type="merge_request"]
).freeze
attr_reader :context attr_reader :context
ISSUE_REFERENCE_TYPE = '@data-reference-type="issue"'.freeze
MERGE_REQUEST_REFERENCE_TYPE = '@data-reference-type="merge_request"'.freeze
# context - An instance of Banzai::RenderContext. # context - An instance of Banzai::RenderContext.
def initialize(context) def initialize(context)
@context = context @context = context
...@@ -24,21 +22,38 @@ module Banzai ...@@ -24,21 +22,38 @@ module Banzai
# Returns Hash in the form { node => issuable_instance } # Returns Hash in the form { node => issuable_instance }
def extract(documents) def extract(documents)
nodes = documents.flat_map do |document| nodes = documents.flat_map do |document|
document.xpath(QUERY) document.xpath(query)
end end
issue_parser = Banzai::ReferenceParser::IssueParser.new(context) # 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
merge_request_parser = 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) Banzai::ReferenceParser::MergeRequestParser.new(context)
]
end
issuables_for_nodes = issue_parser.records_for_nodes(nodes).merge( def query
merge_request_parser.records_for_nodes(nodes) %Q(
descendant-or-self::a[contains(concat(" ", @class, " "), " gfm ")]
[#{reference_types.join(' or ')}]
) )
end
# The project for the issue/MR might be pending for deletion! def reference_types
# Filter them out because we don't care about them. [ISSUE_REFERENCE_TYPE, MERGE_REQUEST_REFERENCE_TYPE]
issuables_for_nodes.select { |node, issuable| issuable.project }
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment