Prepare Banzai to work with group issuables

This commit is contained in:
Jarka Košanová 2018-10-15 10:36:07 +02:00
parent 116117084c
commit 1d7737ae86
2 changed files with 41 additions and 19 deletions

View file

@ -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?

View file

@ -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)
merge_request_parser =
Banzai::ReferenceParser::MergeRequestParser.new(context)
issuables_for_nodes = issue_parser.records_for_nodes(nodes).merge(
merge_request_parser.records_for_nodes(nodes)
)
# The project for the issue/MR might be pending for deletion!
# 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.select { |node, issuable| issuable.project }
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