diff options
author | Alex Hanselka <alex@gitlab.com> | 2018-12-11 00:42:50 +0300 |
---|---|---|
committer | Alex Hanselka <alex@gitlab.com> | 2018-12-11 00:42:50 +0300 |
commit | f77888fe0857c9f4a47133f434c741a510946767 (patch) | |
tree | b227b60894474ba2c9a8864b66e4df65763b35b8 /lib/banzai/filter/milestone_reference_filter.rb | |
parent | d2120ff1e705799752e7d9704cae3f1896d8e186 (diff) | |
parent | 2d7dc668506a0576e231fbe290c89e47cf088300 (diff) |
Merge branch '11-6-stable-prepare-rc5' into '11-6-stable'
Prepare 11.6 RC5 release
See merge request gitlab-org/gitlab-ce!23704
Diffstat (limited to 'lib/banzai/filter/milestone_reference_filter.rb')
-rw-r--r-- | lib/banzai/filter/milestone_reference_filter.rb | 66 |
1 files changed, 50 insertions, 16 deletions
diff --git a/lib/banzai/filter/milestone_reference_filter.rb b/lib/banzai/filter/milestone_reference_filter.rb index 328c8c1803b..c70c3f0c04e 100644 --- a/lib/banzai/filter/milestone_reference_filter.rb +++ b/lib/banzai/filter/milestone_reference_filter.rb @@ -4,6 +4,8 @@ module Banzai module Filter # HTML filter that replaces milestone references with links. class MilestoneReferenceFilter < AbstractReferenceFilter + include Gitlab::Utils::StrongMemoize + self.reference_type = :milestone def self.object_class @@ -13,16 +15,34 @@ module Banzai # 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. - def find_object(project, id) - return unless project.is_a?(Project) + def find_object(parent, id) + return unless valid_context?(parent) - find_milestone_with_finder(project, id: id) + find_milestone_with_finder(parent, id: id) end - def find_object_from_link(project, iid) - return unless project.is_a?(Project) + def find_object_from_link(parent, iid) + return unless valid_context?(parent) + + find_milestone_with_finder(parent, iid: iid) + end + + def valid_context?(parent) + strong_memoize(:valid_context) do + group_context?(parent) || project_context?(parent) + end + end + + def group_context?(parent) + strong_memoize(:group_context) do + parent.is_a?(Group) + end + end - find_milestone_with_finder(project, iid: iid) + def project_context?(parent) + strong_memoize(:project_context) do + parent.is_a?(Project) + end end def references_in(text, pattern = Milestone.reference_pattern) @@ -44,13 +64,15 @@ module Banzai def find_milestone(project_ref, namespace_ref, milestone_id, milestone_name) project_path = full_project_path(namespace_ref, project_ref) - project = parent_from_ref(project_path) - return unless project && project.is_a?(Project) + # Returns group if project is not found by path + parent = parent_from_ref(project_path) + + return unless parent milestone_params = milestone_params(milestone_id, milestone_name) - find_milestone_with_finder(project, milestone_params) + find_milestone_with_finder(parent, milestone_params) end def milestone_params(iid, name) @@ -61,16 +83,28 @@ module Banzai end end - def find_milestone_with_finder(project, params) - finder_params = { project_ids: [project.id], order: nil, state: 'all' } + def find_milestone_with_finder(parent, params) + finder_params = milestone_finder_params(parent, params[:iid].present?) + + MilestonesFinder.new(finder_params).find_by(params) + end - # We don't support IID lookups for group milestones, because IIDs can - # clash between group and project milestones. - if project.group && !params[:iid] - finder_params[:group_ids] = project.group.self_and_ancestors_ids + def milestone_finder_params(parent, find_by_iid) + { order: nil, state: 'all' }.tap do |params| + params[:project_ids] = parent.id if project_context?(parent) + + # We don't support IID lookups because IIDs can clash between + # group/project milestones and group/subgroup milestones. + params[:group_ids] = self_and_ancestors_ids(parent) unless find_by_iid end + end - MilestonesFinder.new(finder_params).find_by(params) + def self_and_ancestors_ids(parent) + if group_context?(parent) + parent.self_and_ancestors_ids + elsif project_context?(parent) + parent.group&.self_and_ancestors_ids + end end def url_for_object(milestone, project) |