debian-mirror-gitlab/lib/banzai/filter/label_reference_filter.rb

120 lines
3.6 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2015-12-23 02:04:40 +05:30
module Banzai
module Filter
2015-09-11 14:41:01 +05:30
# HTML filter that replaces label references with links.
2016-06-02 11:05:42 +05:30
class LabelReferenceFilter < AbstractReferenceFilter
self.reference_type = :label
2016-06-02 11:05:42 +05:30
def self.object_class
Label
2015-09-11 14:41:01 +05:30
end
2018-05-09 12:01:36 +05:30
def find_object(parent_object, id)
find_labels(parent_object).find(id)
2015-10-24 18:46:33 +05:30
end
2016-06-02 11:05:42 +05:30
def self.references_in(text, pattern = Label.reference_pattern)
2016-08-24 12:49:21 +05:30
unescape_html_entities(text).gsub(pattern) do |match|
2017-08-17 22:00:37 +05:30
yield match, $~[:label_id].to_i, $~[:label_name], $~[:project], $~[:namespace], $~
2015-12-23 02:04:40 +05:30
end
2015-09-11 14:41:01 +05:30
end
2016-06-02 11:05:42 +05:30
def references_in(text, pattern = Label.reference_pattern)
2016-08-24 12:49:21 +05:30
unescape_html_entities(text).gsub(pattern) do |match|
2017-08-17 22:00:37 +05:30
namespace, project = $~[:namespace], $~[:project]
project_path = full_project_path(namespace, project)
label = find_label(project_path, $~[:label_id], $~[:label_name])
2015-09-11 14:41:01 +05:30
2016-06-02 11:05:42 +05:30
if label
2017-08-17 22:00:37 +05:30
yield match, label.id, project, namespace, $~
2015-09-11 14:41:01 +05:30
else
2019-01-03 12:48:30 +05:30
escape_html_entities(match)
2015-09-11 14:41:01 +05:30
end
end
end
2018-05-09 12:01:36 +05:30
def find_label(parent_ref, label_id, label_name)
parent = parent_from_ref(parent_ref)
return unless parent
2015-09-11 14:41:01 +05:30
2016-06-02 11:05:42 +05:30
label_params = label_params(label_id, label_name)
2018-05-09 12:01:36 +05:30
find_labels(parent).find_by(label_params)
2016-11-03 12:29:30 +05:30
end
2018-05-09 12:01:36 +05:30
def find_labels(parent)
params = if parent.is_a?(Group)
{ group_id: parent.id,
include_ancestor_groups: true,
only_group_labels: true }
else
2018-12-05 23:21:45 +05:30
{ project: parent,
2018-05-09 12:01:36 +05:30
include_ancestor_groups: true }
end
LabelsFinder.new(nil, params).execute(skip_authorization: true)
2015-09-11 14:41:01 +05:30
end
# 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)
if name
{ name: name.tr('"', '') }
else
2016-06-02 11:05:42 +05:30
{ id: id.to_i }
end
end
2018-05-09 12:01:36 +05:30
def url_for_object(label, parent)
2016-06-02 11:05:42 +05:30
h = Gitlab::Routing.url_helpers
2018-05-09 12:01:36 +05:30
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-06-02 11:05:42 +05:30
end
def object_link_text(object, matches)
2018-05-09 12:01:36 +05:30
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-11-03 12:29:30 +05:30
2019-07-07 11:18:12 +05:30
LabelsHelper.render_colored_label(object, label_suffix: label_suffix, title: tooltip_title(object))
end
def tooltip_title(label)
nil
2016-11-03 12:29:30 +05:30
end
2018-05-09 12:01:36 +05:30
def full_path_ref?(matches)
matches[:namespace] && matches[:project]
end
2016-08-24 12:49:21 +05:30
def unescape_html_entities(text)
CGI.unescapeHTML(text.to_s)
end
2016-09-29 09:46:39 +05:30
2019-01-03 12:48:30 +05:30
def escape_html_entities(text)
CGI.escapeHTML(text.to_s)
end
2018-05-09 12:01:36 +05:30
def object_link_title(object, matches)
2016-09-29 09:46:39 +05:30
# use title of wrapped element instead
nil
end
2015-09-11 14:41:01 +05:30
end
end
end