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
issuables = extractor.extract([doc])
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)
node.content += " (#{issuable.state})"
......@@ -31,7 +31,14 @@ module Banzai
private
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
def can_read_cross_project?
......
......@@ -9,13 +9,11 @@ module Banzai
# 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
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.
def initialize(context)
@context = context
......@@ -24,21 +22,38 @@ module Banzai
# Returns Hash in the form { node => issuable_instance }
def extract(documents)
nodes = documents.flat_map do |document|
document.xpath(QUERY)
document.xpath(query)
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)
]
end
issuables_for_nodes = issue_parser.records_for_nodes(nodes).merge(
merge_request_parser.records_for_nodes(nodes)
def query
%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!
# Filter them out because we don't care about them.
issuables_for_nodes.select { |node, issuable| issuable.project }
def reference_types
[ISSUE_REFERENCE_TYPE, MERGE_REQUEST_REFERENCE_TYPE]
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