Commit 35a5c083 authored by Yorick Peterse's avatar Yorick Peterse

Refactor API::Search for EE

This refactors API::Search so that Enterprise Edition can more easily
extend its functionality, without having to modify the file directly.
parent e82c6d05
# frozen_string_literal: true
module EE
module API
module Helpers
module SearchHelpers
extend ActiveSupport::Concern
class_methods do
extend ::Gitlab::Utils::Override
override :global_search_scopes
def global_search_scopes
['wiki_blobs', 'blobs', 'commits', *super]
end
override :group_search_scopes
def group_search_scopes
['wiki_blobs', 'blobs', 'commits', *super]
end
end
end
end
end
end
# frozen_string_literal: true
module EE
module API
module Search
extend ActiveSupport::Concern
prepended do
helpers do
extend ::Gitlab::Utils::Override
ELASTICSEARCH_SCOPES = %w(wiki_blobs blobs commits).freeze
override :verify_search_scope!
def verify_search_scope!
if ELASTICSEARCH_SCOPES.include?(params[:scope]) && !elasticsearch?
render_api_error!({ error: 'Scope not supported without Elasticsearch!' }, 400)
end
end
def elasticsearch?
::Gitlab::CurrentSettings.elasticsearch_search?
end
override :process_results
def process_results(results)
return [] if results.empty?
if results.is_a?(::Elasticsearch::Model::Response::Response)
return paginate(results).map { |blob| ::Gitlab::Elastic::SearchResults.parse_search_result(blob) }
end
super
end
end
end
end
end
end
# frozen_string_literal: true
module API
module Helpers
module SearchHelpers
def self.global_search_scopes
# This is a separate method so that EE can redefine it.
%w(projects issues merge_requests milestones snippet_titles snippet_blobs)
end
def self.group_search_scopes
# This is a separate method so that EE can redefine it.
%w(projects issues merge_requests milestones)
end
def self.project_search_scopes
# This is a separate method so that EE can redefine it.
%w(issues merge_requests milestones notes wiki_blobs commits blobs)
end
end
end
end
API::Helpers::SearchHelpers.prepend(EE::API::Helpers::SearchHelpers)
......@@ -20,8 +20,6 @@ module API
snippet_blobs: Entities::Snippet
}.freeze
ELASTICSEARCH_SCOPES = %w(wiki_blobs blobs commits).freeze
def search(additional_params = {})
search_params = {
scope: params[:scope],
......@@ -37,12 +35,6 @@ module API
end
def process_results(results)
return [] if results.empty?
if results.is_a?(Elasticsearch::Model::Response::Response)
return paginate(results).map { |blob| Gitlab::Elastic::SearchResults.parse_search_result(blob) }
end
paginate(results)
end
......@@ -54,14 +46,10 @@ module API
SCOPE_ENTITY[params[:scope].to_sym]
end
def check_elasticsearch_scope!
if ELASTICSEARCH_SCOPES.include?(params[:scope]) && !elasticsearch?
render_api_error!({ error: 'Scope not supported without Elasticsearch!' }, 400)
end
end
def elasticsearch?
Gitlab::CurrentSettings.elasticsearch_search?
def verify_search_scope!
# In EE we have additional validation requirements for searches.
# Defining this method here as a noop allows us to easily extend it in
# EE, without having to modify this file directly.
end
end
......@@ -73,15 +61,12 @@ module API
requires :search, type: String, desc: 'The expression it should be searched for'
requires :scope,
type: String,
desc: 'The scope of search, available scopes:
projects, issues, merge_requests, milestones, snippet_titles, snippet_blobs,
if Elasticsearch enabled: wiki_blobs, blobs, commits',
values: %w(projects issues merge_requests milestones snippet_titles snippet_blobs
wiki_blobs blobs commits)
desc: 'The scope of the search',
values: Helpers::SearchHelpers.global_search_scopes
use :pagination
end
get do
check_elasticsearch_scope!
verify_search_scope!
present search, with: entity
end
......@@ -96,14 +81,12 @@ module API
requires :search, type: String, desc: 'The expression it should be searched for'
requires :scope,
type: String,
desc: 'The scope of search, available scopes:
projects, issues, merge_requests, milestones,
if Elasticsearch enabled: wiki_blobs, blobs, commits',
values: %w(projects issues merge_requests milestones wiki_blobs blobs commits)
desc: 'The scope of the search',
values: Helpers::SearchHelpers.group_search_scopes
use :pagination
end
get ':id/(-/)search' do
check_elasticsearch_scope!
verify_search_scope!
present search(group_id: user_group.id), with: entity
end
......@@ -118,9 +101,8 @@ module API
requires :search, type: String, desc: 'The expression it should be searched for'
requires :scope,
type: String,
desc: 'The scope of search, available scopes:
issues, merge_requests, milestones, notes, wiki_blobs, commits, blobs',
values: %w(issues merge_requests milestones notes wiki_blobs commits blobs)
desc: 'The scope of the search',
values: Helpers::SearchHelpers.project_search_scopes
use :pagination
end
get ':id/(-/)search' do
......@@ -129,3 +111,5 @@ module API
end
end
end
API::Search.prepend(EE::API::Search)
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