Commit 175be36f authored by Nick Thomas's avatar Nick Thomas

Merge branch '207623-fix-code-search-pagination' into 'master'

Fix code search pagination on a custom branch

Closes #207623

See merge request gitlab-org/gitlab!25984
parents 3eb665d7 f4e21be9
---
title: Fix code search pagination on a custom branch
merge_request: 25984
author:
type: fixed
...@@ -8,16 +8,26 @@ module EE ...@@ -8,16 +8,26 @@ module EE
override :execute override :execute
def execute def execute
return super unless use_elasticsearch? return super unless use_elasticsearch? && default_branch?
::Gitlab::Elastic::ProjectSearchResults.new( ::Gitlab::Elastic::ProjectSearchResults.new(
current_user, current_user,
params[:search], params[:search],
project.id, project.id,
params[:repository_ref] repository_ref
) )
end end
def repository_ref
params[:repository_ref]
end
def default_branch?
return true if repository_ref.blank?
project.root_ref?(repository_ref)
end
def elasticsearchable_scope def elasticsearchable_scope
project project
end end
......
...@@ -44,23 +44,14 @@ module Gitlab ...@@ -44,23 +44,14 @@ module Gitlab
def blobs(page: 1, per_page: 20) def blobs(page: 1, per_page: 20)
return Kaminari.paginate_array([]) unless Ability.allowed?(@current_user, :download_code, project) return Kaminari.paginate_array([]) unless Ability.allowed?(@current_user, :download_code, project)
return Kaminari.paginate_array([]) if project.empty_repo? || query.blank?
return Kaminari.paginate_array([]) unless root_ref?
if project.empty_repo? || query.blank?
Kaminari.paginate_array([])
else
# We use elastic for default branch only
if root_ref?
project.repository.__elasticsearch__.elastic_search_as_found_blob( project.repository.__elasticsearch__.elastic_search_as_found_blob(
query, query,
page: (page || 1).to_i, page: (page || 1).to_i,
per: per_page per: per_page
) )
else
Kaminari.paginate_array(
Gitlab::FileFinder.new(project, repository_ref).find(query)
)
end
end
end end
def wiki_blobs(page: 1, per_page: 20) def wiki_blobs(page: 1, per_page: 20)
......
...@@ -126,18 +126,10 @@ describe Gitlab::Elastic::ProjectSearchResults, :elastic do ...@@ -126,18 +126,10 @@ describe Gitlab::Elastic::ProjectSearchResults, :elastic do
subject(:blobs) { result.objects('blobs') } subject(:blobs) { result.objects('blobs') }
it 'uses FileFinder instead of ES search' do it 'always returns zero results' do
project.add_reporter(user)
expect_any_instance_of(Gitlab::FileFinder).to receive(:find).with('initial').and_return([])
_ = blobs
end
it 'respects project visibility' do
expect_any_instance_of(Gitlab::FileFinder).to receive(:find).never expect_any_instance_of(Gitlab::FileFinder).to receive(:find).never
is_expected.to be_empty expect(blobs).to be_empty
end end
end end
......
...@@ -17,6 +17,23 @@ describe Search::ProjectService do ...@@ -17,6 +17,23 @@ describe Search::ProjectService do
let(:service) { described_class.new(scope, user, params) } let(:service) { described_class.new(scope, user, params) }
end end
context 'code search' do
let(:user) { scope.owner }
let(:scope) { create(:project) }
let(:service) { described_class.new(scope, user, params) }
let(:params) { { search: '*' } }
describe '#execute' do
subject { service.execute }
it 'returns ordinary results when searching non-default branch' do
expect(service).to receive(:default_branch?).and_return(false)
is_expected.to be_a(::Gitlab::ProjectSearchResults)
end
end
end
context 'visibility', :elastic, :sidekiq_inline do context 'visibility', :elastic, :sidekiq_inline do
include_context 'ProjectPolicyTable context' include_context 'ProjectPolicyTable context'
......
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