Commit a9b6d3eb authored by Alexandru Croitor's avatar Alexandru Croitor

Exclude project namespaces from namespace searches

When searching namespces by path or name we are not expecting
to get ProjectNamespaces in results yet, so we need to filter
those out for now.
parent e31ec46d
......@@ -125,7 +125,7 @@ class Namespace < ApplicationRecord
scope :user_namespaces, -> { where(type: [nil, Namespaces::UserNamespace.sti_name]) }
# TODO: this can be simplified with `type != 'Project'` when working on issue
# https://gitlab.com/gitlab-org/gitlab/-/issues/341070
scope :without_project_namespaces, -> { where("type IS DISTINCT FROM ?", Namespaces::ProjectNamespace.sti_name) }
scope :without_project_namespaces, -> { where(Namespace.arel_table[:type].is_distinct_from(Namespaces::ProjectNamespace.sti_name)) }
scope :sort_by_type, -> { order(Gitlab::Database.nulls_first_order(:type)) }
scope :include_route, -> { includes(:route) }
scope :by_parent, -> (parent) { where(parent_id: parent) }
......@@ -192,9 +192,9 @@ class Namespace < ApplicationRecord
# Returns an ActiveRecord::Relation.
def search(query, include_parents: false)
if include_parents
where(id: Route.for_routable_type(Namespace.name).fuzzy_search(query, [Route.arel_table[:path], Route.arel_table[:name]]).select(:source_id))
without_project_namespaces.where(id: Route.for_routable_type(Namespace.name).fuzzy_search(query, [Route.arel_table[:path], Route.arel_table[:name]]).select(:source_id))
else
fuzzy_search(query, [:path, :name])
without_project_namespaces.fuzzy_search(query, [:path, :name])
end
end
......
......@@ -559,6 +559,25 @@ RSpec.describe Namespace do
it 'returns namespaces with a matching route path regardless of the casing' do
expect(described_class.search('PARENT-PATH/NEW-PATH', include_parents: true)).to eq([second_group])
end
context 'with project namespaces' do
let_it_be(:project) { create(:project, namespace: parent_group, path: 'some-new-path') }
let_it_be(:project_namespace) { create(:project_namespace, project: project) }
it 'does not return project namespace' do
search_result = described_class.search('path')
expect(search_result).not_to include(project_namespace)
expect(search_result).to match_array([first_group, parent_group, second_group])
end
it 'does not return project namespace when including parents' do
search_result = described_class.search('path', include_parents: true)
expect(search_result).not_to include(project_namespace)
expect(search_result).to match_array([first_group, parent_group, second_group])
end
end
end
describe '.with_statistics' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment