2015-12-15 14:51:16 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
2015-04-14 22:34:01 +00:00
|
|
|
# HTML filter that replaces label references with links.
|
2016-02-25 12:49:42 +00:00
|
|
|
class LabelReferenceFilter < AbstractReferenceFilter
|
2016-05-26 11:16:43 +00:00
|
|
|
self.reference_type = :label
|
|
|
|
|
2016-02-25 12:49:42 +00:00
|
|
|
def self.object_class
|
|
|
|
Label
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
|
2018-04-12 09:37:01 +00:00
|
|
|
def find_object(parent_object, id)
|
|
|
|
find_labels(parent_object).find(id)
|
2015-10-07 15:00:48 +00:00
|
|
|
end
|
|
|
|
|
2016-02-25 12:49:42 +00:00
|
|
|
def self.references_in(text, pattern = Label.reference_pattern)
|
2016-06-20 21:33:01 +00:00
|
|
|
unescape_html_entities(text).gsub(pattern) do |match|
|
2016-11-02 23:49:13 +00:00
|
|
|
yield match, $~[:label_id].to_i, $~[:label_name], $~[:project], $~[:namespace], $~
|
2015-12-01 15:25:56 +00:00
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
|
2016-02-25 12:49:42 +00:00
|
|
|
def references_in(text, pattern = Label.reference_pattern)
|
2016-06-20 21:33:01 +00:00
|
|
|
unescape_html_entities(text).gsub(pattern) do |match|
|
2016-11-02 23:49:13 +00:00
|
|
|
namespace, project = $~[:namespace], $~[:project]
|
|
|
|
project_path = full_project_path(namespace, project)
|
|
|
|
label = find_label(project_path, $~[:label_id], $~[:label_name])
|
2015-12-01 15:25:56 +00:00
|
|
|
|
2016-02-25 12:49:42 +00:00
|
|
|
if label
|
2016-11-02 23:49:13 +00:00
|
|
|
yield match, label.id, project, namespace, $~
|
2015-04-03 00:46:43 +00:00
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-30 17:27:03 +00:00
|
|
|
def find_label(parent_ref, label_id, label_name)
|
|
|
|
parent = parent_from_ref(parent_ref)
|
|
|
|
return unless parent
|
2015-04-03 00:46:43 +00:00
|
|
|
|
2016-04-19 10:07:13 +00:00
|
|
|
label_params = label_params(label_id, label_name)
|
2018-03-30 17:27:03 +00:00
|
|
|
find_labels(parent).find_by(label_params)
|
2016-09-20 03:59:26 +00:00
|
|
|
end
|
|
|
|
|
2018-03-30 17:27:03 +00:00
|
|
|
def find_labels(parent)
|
|
|
|
params = if parent.is_a?(Group)
|
|
|
|
{ group_id: parent.id,
|
|
|
|
include_ancestor_groups: true,
|
|
|
|
only_group_labels: true }
|
|
|
|
else
|
|
|
|
{ project_id: parent.id,
|
|
|
|
include_ancestor_groups: true }
|
|
|
|
end
|
|
|
|
|
|
|
|
LabelsFinder.new(nil, params).execute(skip_authorization: true)
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
|
2015-04-10 20:38:34 +00:00
|
|
|
# Parameters to pass to `Label.find_by` based on the given arguments
|
|
|
|
#
|
|
|
|
# id - Integer ID to pass. If present, returns {id: id}
|
|
|
|
# name - String name to pass. If `id` is absent, finds by name without
|
|
|
|
# surrounding quotes.
|
|
|
|
#
|
|
|
|
# Returns a Hash.
|
|
|
|
def label_params(id, name)
|
2015-05-25 20:06:34 +00:00
|
|
|
if name
|
2015-05-15 20:07:25 +00:00
|
|
|
{ name: name.tr('"', '') }
|
2015-05-25 20:06:34 +00:00
|
|
|
else
|
2016-04-19 10:07:13 +00:00
|
|
|
{ id: id.to_i }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-30 17:27:03 +00:00
|
|
|
def url_for_object(label, parent)
|
2016-04-19 10:07:13 +00:00
|
|
|
h = Gitlab::Routing.url_helpers
|
2018-03-30 17:27:03 +00:00
|
|
|
|
|
|
|
if parent.is_a?(Project)
|
|
|
|
h.project_issues_url(parent, label_name: label.name, only_path: context[:only_path])
|
|
|
|
elsif context[:label_url_method]
|
|
|
|
h.public_send(context[:label_url_method], parent, label_name: label.name, only_path: context[:only_path]) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
2016-04-19 10:07:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def object_link_text(object, matches)
|
2018-03-30 17:27:03 +00:00
|
|
|
label_suffix = ''
|
|
|
|
|
|
|
|
if project || full_path_ref?(matches)
|
|
|
|
project_path = full_project_path(matches[:namespace], matches[:project])
|
|
|
|
parent_from_ref = from_ref_cached(project_path)
|
|
|
|
reference = parent_from_ref.to_human_reference(project || group)
|
|
|
|
|
|
|
|
label_suffix = " <i>in #{reference}</i>" if reference.present?
|
|
|
|
end
|
2016-09-29 03:21:47 +00:00
|
|
|
|
2016-11-02 23:49:13 +00:00
|
|
|
LabelsHelper.render_colored_label(object, label_suffix)
|
2016-09-29 03:21:47 +00:00
|
|
|
end
|
|
|
|
|
2018-03-30 17:27:03 +00:00
|
|
|
def full_path_ref?(matches)
|
|
|
|
matches[:namespace] && matches[:project]
|
|
|
|
end
|
|
|
|
|
2016-06-20 21:33:01 +00:00
|
|
|
def unescape_html_entities(text)
|
2016-06-29 20:47:37 +00:00
|
|
|
CGI.unescapeHTML(text.to_s)
|
2016-06-20 21:33:01 +00:00
|
|
|
end
|
2016-07-17 00:03:56 +00:00
|
|
|
|
2018-04-02 23:25:36 +00:00
|
|
|
def object_link_title(object, matches)
|
2016-07-17 00:03:56 +00:00
|
|
|
# use title of wrapped element instead
|
|
|
|
nil
|
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|