2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
module Banzai
|
|
|
|
module Filter
|
|
|
|
# HTML filter that replaces milestone references with links.
|
|
|
|
class MilestoneReferenceFilter < AbstractReferenceFilter
|
2016-06-16 23:09:34 +05:30
|
|
|
self.reference_type = :milestone
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
def self.object_class
|
|
|
|
Milestone
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
# Links to project milestones contain the IID, but when we're handling
|
|
|
|
# 'regular' references, we need to use the global ID to disambiguate
|
|
|
|
# between group and project milestones.
|
2016-01-14 18:37:52 +05:30
|
|
|
def find_object(project, id)
|
2018-05-09 12:01:36 +05:30
|
|
|
return unless project.is_a?(Project)
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
find_milestone_with_finder(project, id: id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_object_from_link(project, iid)
|
2018-05-09 12:01:36 +05:30
|
|
|
return unless project.is_a?(Project)
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
find_milestone_with_finder(project, iid: iid)
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def references_in(text, pattern = Milestone.reference_pattern)
|
|
|
|
# We'll handle here the references that follow the `reference_pattern`.
|
|
|
|
# Other patterns (for example, the link pattern) are handled by the
|
|
|
|
# default implementation.
|
|
|
|
return super(text, pattern) if pattern != Milestone.reference_pattern
|
|
|
|
|
|
|
|
text.gsub(pattern) do |match|
|
2017-08-17 22:00:37 +05:30
|
|
|
milestone = find_milestone($~[:project], $~[:namespace], $~[:milestone_iid], $~[:milestone_name])
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
if milestone
|
2017-09-10 17:25:29 +05:30
|
|
|
yield match, milestone.id, $~[:project], $~[:namespace], $~
|
2016-06-02 11:05:42 +05:30
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def find_milestone(project_ref, namespace_ref, milestone_id, milestone_name)
|
|
|
|
project_path = full_project_path(namespace_ref, project_ref)
|
2018-03-17 18:26:18 +05:30
|
|
|
project = parent_from_ref(project_path)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
return unless project && project.is_a?(Project)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
milestone_params = milestone_params(milestone_id, milestone_name)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
find_milestone_with_finder(project, milestone_params)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def milestone_params(iid, name)
|
|
|
|
if name
|
|
|
|
{ name: name.tr('"', '') }
|
|
|
|
else
|
|
|
|
{ iid: iid.to_i }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def find_milestone_with_finder(project, params)
|
2018-03-17 18:26:18 +05:30
|
|
|
finder_params = { project_ids: [project.id], order: nil, state: 'all' }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
# We don't support IID lookups for group milestones, because IIDs can
|
|
|
|
# clash between group and project milestones.
|
|
|
|
if project.group && !params[:iid]
|
2018-11-08 19:23:39 +05:30
|
|
|
finder_params[:group_ids] = project.group.self_and_ancestors_ids
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
MilestonesFinder.new(finder_params).find_by(params)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def url_for_object(milestone, project)
|
2017-09-10 17:25:29 +05:30
|
|
|
Gitlab::Routing
|
|
|
|
.url_helpers
|
|
|
|
.milestone_url(milestone, only_path: context[:only_path])
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
def object_link_text(object, matches)
|
2017-08-17 22:00:37 +05:30
|
|
|
milestone_link = escape_once(super)
|
2017-09-10 17:25:29 +05:30
|
|
|
reference = object.project&.to_reference(project)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
if reference.present?
|
|
|
|
"#{milestone_link} <i>in #{reference}</i>".html_safe
|
2016-06-02 11:05:42 +05:30
|
|
|
else
|
2017-08-17 22:00:37 +05:30
|
|
|
milestone_link
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def object_link_title(object, matches)
|
2016-09-29 09:46:39 +05:30
|
|
|
nil
|
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|