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 ...@@ -20,8 +20,6 @@ module API
snippet_blobs: Entities::Snippet snippet_blobs: Entities::Snippet
}.freeze }.freeze
ELASTICSEARCH_SCOPES = %w(wiki_blobs blobs commits).freeze
def search(additional_params = {}) def search(additional_params = {})
search_params = { search_params = {
scope: params[:scope], scope: params[:scope],
...@@ -37,12 +35,6 @@ module API ...@@ -37,12 +35,6 @@ module API
end end
def process_results(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
paginate(results) paginate(results)
end end
...@@ -54,14 +46,10 @@ module API ...@@ -54,14 +46,10 @@ module API
SCOPE_ENTITY[params[:scope].to_sym] SCOPE_ENTITY[params[:scope].to_sym]
end end
def check_elasticsearch_scope! def verify_search_scope!
if ELASTICSEARCH_SCOPES.include?(params[:scope]) && !elasticsearch? # In EE we have additional validation requirements for searches.
render_api_error!({ error: 'Scope not supported without Elasticsearch!' }, 400) # Defining this method here as a noop allows us to easily extend it in
end # EE, without having to modify this file directly.
end
def elasticsearch?
Gitlab::CurrentSettings.elasticsearch_search?
end end
end end
...@@ -73,15 +61,12 @@ module API ...@@ -73,15 +61,12 @@ module API
requires :search, type: String, desc: 'The expression it should be searched for' requires :search, type: String, desc: 'The expression it should be searched for'
requires :scope, requires :scope,
type: String, type: String,
desc: 'The scope of search, available scopes: desc: 'The scope of the search',
projects, issues, merge_requests, milestones, snippet_titles, snippet_blobs, values: Helpers::SearchHelpers.global_search_scopes
if Elasticsearch enabled: wiki_blobs, blobs, commits',
values: %w(projects issues merge_requests milestones snippet_titles snippet_blobs
wiki_blobs blobs commits)
use :pagination use :pagination
end end
get do get do
check_elasticsearch_scope! verify_search_scope!
present search, with: entity present search, with: entity
end end
...@@ -96,14 +81,12 @@ module API ...@@ -96,14 +81,12 @@ module API
requires :search, type: String, desc: 'The expression it should be searched for' requires :search, type: String, desc: 'The expression it should be searched for'
requires :scope, requires :scope,
type: String, type: String,
desc: 'The scope of search, available scopes: desc: 'The scope of the search',
projects, issues, merge_requests, milestones, values: Helpers::SearchHelpers.group_search_scopes
if Elasticsearch enabled: wiki_blobs, blobs, commits',
values: %w(projects issues merge_requests milestones wiki_blobs blobs commits)
use :pagination use :pagination
end end
get ':id/(-/)search' do get ':id/(-/)search' do
check_elasticsearch_scope! verify_search_scope!
present search(group_id: user_group.id), with: entity present search(group_id: user_group.id), with: entity
end end
...@@ -118,9 +101,8 @@ module API ...@@ -118,9 +101,8 @@ module API
requires :search, type: String, desc: 'The expression it should be searched for' requires :search, type: String, desc: 'The expression it should be searched for'
requires :scope, requires :scope,
type: String, type: String,
desc: 'The scope of search, available scopes: desc: 'The scope of the search',
issues, merge_requests, milestones, notes, wiki_blobs, commits, blobs', values: Helpers::SearchHelpers.project_search_scopes
values: %w(issues merge_requests milestones notes wiki_blobs commits blobs)
use :pagination use :pagination
end end
get ':id/(-/)search' do get ':id/(-/)search' do
...@@ -129,3 +111,5 @@ module API ...@@ -129,3 +111,5 @@ module API
end end
end 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