2018-07-23 09:32:32 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-15 14:51:16 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
2015-12-01 15:25:56 +00:00
|
|
|
# Issues, Merge Requests, Snippets, Commits and Commit Ranges share
|
2015-12-01 16:13:47 +00:00
|
|
|
# similar functionality in reference filtering.
|
2015-11-16 20:56:28 +00:00
|
|
|
class AbstractReferenceFilter < ReferenceFilter
|
|
|
|
include CrossProjectReference
|
|
|
|
|
|
|
|
def self.object_class
|
|
|
|
# Implement in child class
|
|
|
|
# Example: MergeRequest
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.object_name
|
2016-03-24 15:27:52 +00:00
|
|
|
@object_name ||= object_class.name.underscore
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.object_sym
|
2016-03-24 15:27:52 +00:00
|
|
|
@object_sym ||= object_name.to_sym
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Public: Find references in text (like `!123` for merge requests)
|
|
|
|
#
|
2015-12-01 11:49:22 +00:00
|
|
|
# AnyReferenceFilter.references_in(text) do |match, id, project_ref, matches|
|
|
|
|
# object = find_object(project_ref, id)
|
|
|
|
# "<a href=...>#{object.to_reference}</a>"
|
2015-11-16 20:56:28 +00:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# text - String text to search.
|
|
|
|
#
|
2015-12-01 11:49:22 +00:00
|
|
|
# Yields the String match, the Integer referenced object ID, an optional String
|
|
|
|
# of the external project reference, and all of the matchdata.
|
2015-11-16 20:56:28 +00:00
|
|
|
#
|
|
|
|
# Returns a String replaced with the return of the block.
|
2015-12-01 14:51:27 +00:00
|
|
|
def self.references_in(text, pattern = object_class.reference_pattern)
|
|
|
|
text.gsub(pattern) do |match|
|
2017-02-16 20:06:22 +00:00
|
|
|
symbol = $~[object_sym]
|
|
|
|
if object_class.reference_valid?(symbol)
|
|
|
|
yield match, symbol.to_i, $~[:project], $~[:namespace], $~
|
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-30 16:16:59 +00:00
|
|
|
def object_class
|
|
|
|
self.class.object_class
|
|
|
|
end
|
|
|
|
|
|
|
|
def object_sym
|
|
|
|
self.class.object_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
def references_in(*args, &block)
|
|
|
|
self.class.references_in(*args, &block)
|
|
|
|
end
|
2015-11-16 20:56:28 +00:00
|
|
|
|
2017-08-07 10:54:18 +00:00
|
|
|
# Implement in child class
|
|
|
|
# Example: project.merge_requests.find
|
2018-04-12 09:37:01 +00:00
|
|
|
def find_object(parent_object, id)
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
|
|
|
|
2017-08-03 11:50:06 +00:00
|
|
|
# Override if the link reference pattern produces a different ID (global
|
|
|
|
# ID vs internal ID, for instance) to the regular reference pattern.
|
2018-04-12 09:37:01 +00:00
|
|
|
def find_object_from_link(parent_object, id)
|
|
|
|
find_object(parent_object, id)
|
2017-08-03 11:50:06 +00:00
|
|
|
end
|
|
|
|
|
2017-08-07 10:54:18 +00:00
|
|
|
# Implement in child class
|
|
|
|
# Example: project_merge_request_url
|
2018-04-12 09:37:01 +00:00
|
|
|
def url_for_object(object, parent_object)
|
2017-08-07 10:54:18 +00:00
|
|
|
end
|
2016-03-24 14:53:38 +00:00
|
|
|
|
2018-04-12 09:37:01 +00:00
|
|
|
def find_object_cached(parent_object, id)
|
|
|
|
cached_call(:banzai_find_object, id, path: [object_class, parent_object.id]) do
|
|
|
|
find_object(parent_object, id)
|
2016-03-24 14:53:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-12 09:37:01 +00:00
|
|
|
def find_object_from_link_cached(parent_object, id)
|
|
|
|
cached_call(:banzai_find_object_from_link, id, path: [object_class, parent_object.id]) do
|
|
|
|
find_object_from_link(parent_object, id)
|
2017-08-03 11:50:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-01 09:21:05 +00:00
|
|
|
def from_ref_cached(ref)
|
|
|
|
cached_call("banzai_#{parent_type}_refs".to_sym, ref) do
|
|
|
|
parent_from_ref(ref)
|
2016-03-24 14:53:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-12 09:37:01 +00:00
|
|
|
def url_for_object_cached(object, parent_object)
|
|
|
|
cached_call(:banzai_url_for_object, object, path: [object_class, parent_object.id]) do
|
|
|
|
url_for_object(object, parent_object)
|
2016-03-24 14:53:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-16 20:56:28 +00:00
|
|
|
def call
|
2017-11-01 17:35:14 +00:00
|
|
|
return doc unless project || group
|
2015-12-24 13:43:07 +00:00
|
|
|
|
2016-03-24 14:57:22 +00:00
|
|
|
ref_pattern = object_class.reference_pattern
|
|
|
|
link_pattern = object_class.link_reference_pattern
|
2015-12-01 14:51:27 +00:00
|
|
|
|
2018-07-07 11:53:45 +00:00
|
|
|
# Compile often used regexps only once outside of the loop
|
|
|
|
ref_pattern_anchor = /\A#{ref_pattern}\z/
|
|
|
|
link_pattern_start = /\A#{link_pattern}/
|
|
|
|
link_pattern_anchor = /\A#{link_pattern}\z/
|
|
|
|
|
2016-06-01 15:39:12 +00:00
|
|
|
nodes.each do |node|
|
2016-03-24 14:57:22 +00:00
|
|
|
if text_node?(node) && ref_pattern
|
|
|
|
replace_text_when_pattern_matches(node, ref_pattern) do |content|
|
|
|
|
object_link_filter(content, ref_pattern)
|
|
|
|
end
|
|
|
|
|
|
|
|
elsif element_node?(node)
|
2016-09-30 09:03:16 +00:00
|
|
|
yield_valid_link(node) do |link, inner_html|
|
2018-07-07 11:53:45 +00:00
|
|
|
if ref_pattern && link =~ ref_pattern_anchor
|
2016-03-24 14:57:22 +00:00
|
|
|
replace_link_node_with_href(node, link) do
|
2016-09-30 09:03:16 +00:00
|
|
|
object_link_filter(link, ref_pattern, link_content: inner_html)
|
2016-03-24 14:57:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
next unless link_pattern
|
|
|
|
|
2018-07-07 11:53:45 +00:00
|
|
|
if link == inner_html && inner_html =~ link_pattern_start
|
2016-03-24 14:57:22 +00:00
|
|
|
replace_link_node_with_text(node, link) do
|
2017-08-03 11:50:06 +00:00
|
|
|
object_link_filter(inner_html, link_pattern, link_reference: true)
|
2016-03-24 14:57:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2018-07-07 11:53:45 +00:00
|
|
|
if link =~ link_pattern_anchor
|
2016-03-24 14:57:22 +00:00
|
|
|
replace_link_node_with_href(node, link) do
|
2017-08-03 11:50:06 +00:00
|
|
|
object_link_filter(link, link_pattern, link_content: inner_html, link_reference: true)
|
2016-03-24 14:57:22 +00:00
|
|
|
end
|
2015-12-24 13:43:07 +00:00
|
|
|
|
2016-03-24 14:57:22 +00:00
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
2015-12-24 13:43:07 +00:00
|
|
|
end
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
2016-02-25 11:45:38 +00:00
|
|
|
|
|
|
|
doc
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Replace references (like `!123` for merge requests) in text with links
|
|
|
|
# to the referenced object's details page.
|
|
|
|
#
|
|
|
|
# text - String text to replace references in.
|
2015-12-01 16:13:47 +00:00
|
|
|
# pattern - Reference pattern to match against.
|
2016-09-30 09:03:16 +00:00
|
|
|
# link_content - Original content of the link being replaced.
|
2017-08-03 11:50:06 +00:00
|
|
|
# link_reference - True if this was using the link reference pattern,
|
|
|
|
# false otherwise.
|
2015-11-16 20:56:28 +00:00
|
|
|
#
|
|
|
|
# Returns a String with references replaced with links. All links
|
|
|
|
# have `gfm` and `gfm-OBJECT_NAME` class names attached for styling.
|
2017-08-03 11:50:06 +00:00
|
|
|
def object_link_filter(text, pattern, link_content: nil, link_reference: false)
|
2016-11-02 23:49:13 +00:00
|
|
|
references_in(text, pattern) do |match, id, project_ref, namespace_ref, matches|
|
2017-12-01 09:21:05 +00:00
|
|
|
parent_path = if parent_type == :group
|
|
|
|
full_group_path(namespace_ref)
|
|
|
|
else
|
|
|
|
full_project_path(namespace_ref, project_ref)
|
|
|
|
end
|
2015-11-16 20:56:28 +00:00
|
|
|
|
2017-12-01 09:21:05 +00:00
|
|
|
parent = from_ref_cached(parent_path)
|
|
|
|
|
|
|
|
if parent
|
2017-08-03 11:50:06 +00:00
|
|
|
object =
|
|
|
|
if link_reference
|
2017-12-01 09:21:05 +00:00
|
|
|
find_object_from_link_cached(parent, id)
|
2017-08-03 11:50:06 +00:00
|
|
|
else
|
2017-12-01 09:21:05 +00:00
|
|
|
find_object_cached(parent, id)
|
2017-08-03 11:50:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if object
|
2018-04-02 23:25:36 +00:00
|
|
|
title = object_link_title(object, matches)
|
2015-11-16 20:56:28 +00:00
|
|
|
klass = reference_class(object_sym)
|
2015-12-01 16:13:47 +00:00
|
|
|
|
2018-02-26 15:28:49 +00:00
|
|
|
data = data_attributes_for(link_content || match, parent, object,
|
|
|
|
link_content: !!link_content,
|
|
|
|
link_reference: link_reference)
|
2015-12-01 14:51:27 +00:00
|
|
|
|
2017-02-22 15:10:32 +00:00
|
|
|
url =
|
|
|
|
if matches.names.include?("url") && matches[:url]
|
|
|
|
matches[:url]
|
|
|
|
else
|
2017-12-01 09:21:05 +00:00
|
|
|
url_for_object_cached(object, parent)
|
2017-02-22 15:10:32 +00:00
|
|
|
end
|
2015-11-30 20:14:46 +00:00
|
|
|
|
2016-09-30 09:03:16 +00:00
|
|
|
content = link_content || object_link_text(object, matches)
|
2015-11-16 20:56:28 +00:00
|
|
|
|
|
|
|
%(<a href="#{url}" #{data}
|
2015-12-24 20:19:03 +00:00
|
|
|
title="#{escape_once(title)}"
|
2016-09-30 09:03:16 +00:00
|
|
|
class="#{klass}">#{content}</a>)
|
2015-11-16 20:56:28 +00:00
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-30 17:27:03 +00:00
|
|
|
def data_attributes_for(text, parent, object, link_content: false, link_reference: false)
|
|
|
|
object_parent_type = parent.is_a?(Group) ? :group : :project
|
|
|
|
|
2016-06-28 15:50:17 +00:00
|
|
|
data_attribute(
|
2018-03-30 17:27:03 +00:00
|
|
|
original: text,
|
|
|
|
link: link_content,
|
|
|
|
link_reference: link_reference,
|
|
|
|
object_parent_type => parent.id,
|
|
|
|
object_sym => object.id
|
2016-06-28 15:50:17 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-11-30 20:14:46 +00:00
|
|
|
def object_link_text_extras(object, matches)
|
|
|
|
extras = []
|
|
|
|
|
2015-12-01 14:51:27 +00:00
|
|
|
if matches.names.include?("anchor") && matches[:anchor] && matches[:anchor] =~ /\A\#note_(\d+)\z/
|
2015-11-30 20:14:46 +00:00
|
|
|
extras << "comment #{$1}"
|
|
|
|
end
|
|
|
|
|
2018-04-06 15:13:18 +00:00
|
|
|
extension = matches[:extension] if matches.names.include?("extension")
|
|
|
|
|
|
|
|
extras << extension if extension
|
|
|
|
|
2015-11-30 20:14:46 +00:00
|
|
|
extras
|
|
|
|
end
|
|
|
|
|
2018-04-02 23:25:36 +00:00
|
|
|
def object_link_title(object, matches)
|
2016-07-17 00:03:56 +00:00
|
|
|
object.title
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
2015-12-24 13:38:23 +00:00
|
|
|
|
|
|
|
def object_link_text(object, matches)
|
2017-11-22 13:20:35 +00:00
|
|
|
parent = context[:project] || context[:group]
|
|
|
|
text = object.reference_link_text(parent)
|
2015-12-24 13:38:23 +00:00
|
|
|
|
|
|
|
extras = object_link_text_extras(object, matches)
|
|
|
|
text += " (#{extras.join(", ")})" if extras.any?
|
|
|
|
|
|
|
|
text
|
|
|
|
end
|
2016-03-24 14:53:38 +00:00
|
|
|
|
2016-06-01 15:39:12 +00:00
|
|
|
# Returns a Hash containing all object references (e.g. issue IDs) per the
|
|
|
|
# project they belong to.
|
2017-12-01 09:21:05 +00:00
|
|
|
def references_per_parent
|
|
|
|
@references_per ||= {}
|
|
|
|
|
|
|
|
@references_per[parent_type] ||= begin
|
2016-06-01 15:39:12 +00:00
|
|
|
refs = Hash.new { |hash, key| hash[key] = Set.new }
|
|
|
|
|
2017-06-30 12:47:53 +00:00
|
|
|
regex = Regexp.union(object_class.reference_pattern, object_class.link_reference_pattern)
|
2016-06-01 15:39:12 +00:00
|
|
|
|
|
|
|
nodes.each do |node|
|
|
|
|
node.to_html.scan(regex) do
|
2017-12-01 09:21:05 +00:00
|
|
|
path = if parent_type == :project
|
|
|
|
full_project_path($~[:namespace], $~[:project])
|
|
|
|
else
|
|
|
|
full_group_path($~[:group])
|
|
|
|
end
|
|
|
|
|
2016-06-18 17:55:45 +00:00
|
|
|
symbol = $~[object_sym]
|
2017-12-01 09:21:05 +00:00
|
|
|
refs[path] << symbol if object_class.reference_valid?(symbol)
|
2016-06-01 15:39:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
refs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a Hash containing referenced projects grouped per their full
|
|
|
|
# path.
|
2017-12-01 09:21:05 +00:00
|
|
|
def parent_per_reference
|
|
|
|
@per_reference ||= {}
|
|
|
|
|
|
|
|
@per_reference[parent_type] ||= begin
|
2016-06-01 15:39:12 +00:00
|
|
|
refs = Set.new
|
|
|
|
|
2017-12-01 09:21:05 +00:00
|
|
|
references_per_parent.each do |ref, _|
|
|
|
|
refs << ref
|
2016-06-01 15:39:12 +00:00
|
|
|
end
|
|
|
|
|
2017-12-01 09:21:05 +00:00
|
|
|
find_for_paths(refs.to_a).index_by(&:full_path)
|
2016-06-01 15:39:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-01 09:21:05 +00:00
|
|
|
def relation_for_paths(paths)
|
|
|
|
klass = parent_type.to_s.camelize.constantize
|
|
|
|
result = klass.where_full_path_in(paths)
|
|
|
|
return result if parent_type == :group
|
|
|
|
|
|
|
|
result.includes(:namespace) if parent_type == :project
|
2016-06-01 15:39:12 +00:00
|
|
|
end
|
|
|
|
|
2016-09-27 15:13:08 +00:00
|
|
|
# Returns projects for the given paths.
|
2017-12-01 09:21:05 +00:00
|
|
|
def find_for_paths(paths)
|
2018-09-20 22:40:15 +00:00
|
|
|
if Gitlab::SafeRequestStore.active?
|
2017-12-01 09:21:05 +00:00
|
|
|
cache = refs_cache
|
2016-12-08 07:52:20 +00:00
|
|
|
to_query = paths - cache.keys
|
2016-09-27 15:13:08 +00:00
|
|
|
|
|
|
|
unless to_query.empty?
|
2017-12-01 09:21:05 +00:00
|
|
|
records = relation_for_paths(to_query)
|
2016-12-08 07:52:20 +00:00
|
|
|
|
|
|
|
found = []
|
2017-12-01 09:21:05 +00:00
|
|
|
records.each do |record|
|
|
|
|
ref = record.full_path
|
|
|
|
get_or_set_cache(cache, ref) { record }
|
2016-12-08 07:52:20 +00:00
|
|
|
found << ref
|
|
|
|
end
|
|
|
|
|
|
|
|
not_found = to_query - found
|
|
|
|
not_found.each do |ref|
|
|
|
|
get_or_set_cache(cache, ref) { nil }
|
2016-09-27 15:13:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-08 07:52:20 +00:00
|
|
|
cache.slice(*paths).values.compact
|
2016-09-27 15:13:08 +00:00
|
|
|
else
|
2017-12-01 09:21:05 +00:00
|
|
|
relation_for_paths(paths)
|
2016-09-27 15:13:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-01 09:21:05 +00:00
|
|
|
def current_parent_path
|
|
|
|
@current_parent_path ||= parent&.full_path
|
2016-06-01 15:39:12 +00:00
|
|
|
end
|
|
|
|
|
2016-11-02 23:49:13 +00:00
|
|
|
def current_project_namespace_path
|
2017-12-01 09:21:05 +00:00
|
|
|
@current_project_namespace_path ||= project&.namespace&.full_path
|
2016-11-02 23:49:13 +00:00
|
|
|
end
|
|
|
|
|
2016-03-24 14:53:38 +00:00
|
|
|
private
|
|
|
|
|
2016-11-02 23:49:13 +00:00
|
|
|
def full_project_path(namespace, project_ref)
|
2017-12-01 09:21:05 +00:00
|
|
|
return current_parent_path unless project_ref
|
2016-11-02 23:49:13 +00:00
|
|
|
|
|
|
|
namespace_ref = namespace || current_project_namespace_path
|
|
|
|
"#{namespace_ref}/#{project_ref}"
|
|
|
|
end
|
|
|
|
|
2017-12-01 09:21:05 +00:00
|
|
|
def refs_cache
|
2018-09-20 22:40:15 +00:00
|
|
|
Gitlab::SafeRequestStore["banzai_#{parent_type}_refs".to_sym] ||= {}
|
2017-12-01 09:21:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def parent_type
|
|
|
|
:project
|
|
|
|
end
|
|
|
|
|
|
|
|
def parent
|
|
|
|
parent_type == :project ? project : group
|
2016-03-24 14:53:38 +00:00
|
|
|
end
|
2018-03-30 17:27:03 +00:00
|
|
|
|
|
|
|
def full_group_path(group_ref)
|
|
|
|
return current_parent_path unless group_ref
|
|
|
|
|
|
|
|
group_ref
|
|
|
|
end
|
2015-11-16 20:56:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|