Commit f4e21be9 authored by Dmitry Gruzd's avatar Dmitry Gruzd Committed by Nick Thomas

Fix code search pagination on a custom branch

When we use non default code search with advanced global search enabled
we have a broken pagination. This change moves default_branch? condition
into the ProjectService to account for that difference.
parent 0a06be7d
---
title: Fix code search pagination on a custom branch
merge_request: 25984
author:
type: fixed
......@@ -8,16 +8,26 @@ module EE
override :execute
def execute
return super unless use_elasticsearch?
return super unless use_elasticsearch? && default_branch?
::Gitlab::Elastic::ProjectSearchResults.new(
current_user,
params[:search],
project.id,
params[:repository_ref]
repository_ref
)
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
project
end
......
......@@ -44,23 +44,14 @@ module Gitlab
def blobs(page: 1, per_page: 20)
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(
query,
page: (page || 1).to_i,
per: per_page
)
else
Kaminari.paginate_array(
Gitlab::FileFinder.new(project, repository_ref).find(query)
)
end
end
end
def wiki_blobs(page: 1, per_page: 20)
......
......@@ -126,18 +126,10 @@ describe Gitlab::Elastic::ProjectSearchResults, :elastic do
subject(:blobs) { result.objects('blobs') }
it 'uses FileFinder instead of ES search' 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
it 'always returns zero results' do
expect_any_instance_of(Gitlab::FileFinder).to receive(:find).never
is_expected.to be_empty
expect(blobs).to be_empty
end
end
......
......@@ -17,6 +17,23 @@ describe Search::ProjectService do
let(:service) { described_class.new(scope, user, params) }
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
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