debian-mirror-gitlab/lib/api/search.rb

193 lines
6.5 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module API
2021-01-03 14:25:43 +05:30
class Search < ::API::Base
2018-03-17 18:26:18 +05:30
include PaginationParams
2022-03-02 08:16:31 +05:30
before do
authenticate!
2022-05-07 20:08:51 +05:30
check_rate_limit!(:search_rate_limit, scope: [current_user])
2022-03-02 08:16:31 +05:30
end
2018-03-17 18:26:18 +05:30
2021-01-29 00:20:46 +05:30
feature_category :global_search
2022-07-16 23:28:13 +05:30
urgency :low
2021-01-29 00:20:46 +05:30
2022-01-26 12:08:38 +05:30
rescue_from ActiveRecord::QueryCanceled do |e|
render_api_error!({ error: 'Request timed out' }, 408)
end
2018-03-17 18:26:18 +05:30
helpers do
SCOPE_ENTITY = {
merge_requests: Entities::MergeRequestBasic,
issues: Entities::IssueBasic,
projects: Entities::BasicProjectDetails,
milestones: Entities::Milestone,
notes: Entities::Note,
commits: Entities::CommitDetail,
blobs: Entities::Blob,
wiki_blobs: Entities::Blob,
snippet_titles: Entities::Snippet,
2019-07-07 11:18:12 +05:30
users: Entities::UserBasic
2018-03-17 18:26:18 +05:30
}.freeze
2021-04-29 21:17:54 +05:30
def scope_preload_method
{
merge_requests: :with_api_entity_associations,
projects: :with_api_entity_associations,
issues: :with_api_entity_associations,
milestones: :with_api_entity_associations,
commits: :with_api_commit_entity_associations
}.freeze
end
2020-06-23 00:09:42 +05:30
2022-03-02 08:16:31 +05:30
def search_service(additional_params = {})
2018-03-17 18:26:18 +05:30
search_params = {
scope: params[:scope],
search: params[:search],
2020-11-24 15:15:51 +05:30
state: params[:state],
2021-01-03 14:25:43 +05:30
confidential: params[:confidential],
2018-03-17 18:26:18 +05:30
snippets: snippets?,
2021-01-29 00:20:46 +05:30
basic_search: params[:basic_search],
2018-03-17 18:26:18 +05:30
page: params[:page],
2021-01-29 00:20:46 +05:30
per_page: params[:per_page],
order_by: params[:order_by],
sort: params[:sort]
2018-03-17 18:26:18 +05:30
}.merge(additional_params)
2022-03-02 08:16:31 +05:30
SearchService.new(current_user, search_params)
end
def search(additional_params = {})
2022-08-27 11:52:29 +05:30
@search_duration_s = Benchmark.realtime do
@results = search_service(additional_params).search_objects(preload_method)
end
2022-11-25 23:54:43 +05:30
set_global_search_log_information(additional_params)
2020-06-23 00:09:42 +05:30
2022-10-11 01:57:18 +05:30
Gitlab::Metrics::GlobalSearchSlis.record_apdex(
elapsed: @search_duration_s,
2023-01-13 00:05:48 +05:30
search_type: search_type(additional_params),
search_level: search_service(additional_params).level,
2022-10-11 01:57:18 +05:30
search_scope: search_scope
)
2020-06-23 00:09:42 +05:30
Gitlab::UsageDataCounters::SearchCounter.count(:all_searches)
2018-03-17 18:26:18 +05:30
2022-08-27 11:52:29 +05:30
paginate(@results)
2022-10-11 01:57:18 +05:30
ensure
# If we raise an error somewhere in the @search_duration_s benchmark block, we will end up here
# with a 200 status code, but an empty @search_duration_s.
Gitlab::Metrics::GlobalSearchSlis.record_error_rate(
error: @search_duration_s.nil? || (status < 200 || status >= 400),
2023-01-13 00:05:48 +05:30
search_type: search_type(additional_params),
2022-10-11 01:57:18 +05:30
search_level: search_service(additional_params).level,
search_scope: search_scope
)
2018-03-17 18:26:18 +05:30
end
def snippets?
2020-05-24 23:13:21 +05:30
%w(snippet_titles).include?(params[:scope]).to_s
2018-03-17 18:26:18 +05:30
end
def entity
SCOPE_ENTITY[params[:scope].to_sym]
end
2019-07-07 11:18:12 +05:30
2020-06-23 00:09:42 +05:30
def preload_method
2021-04-29 21:17:54 +05:30
scope_preload_method[params[:scope].to_sym]
2020-06-23 00:09:42 +05:30
end
2020-03-13 15:44:24 +05:30
def verify_search_scope!(resource:)
2019-07-07 11:18:12 +05:30
# 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
2022-08-27 11:52:29 +05:30
2022-11-25 23:54:43 +05:30
def search_type(additional_params = {})
2022-08-27 11:52:29 +05:30
'basic'
end
def search_scope
params[:scope]
end
2022-11-25 23:54:43 +05:30
def set_global_search_log_information(additional_params)
2022-08-27 11:52:29 +05:30
Gitlab::Instrumentation::GlobalSearchApi.set_information(
2022-11-25 23:54:43 +05:30
type: search_type(additional_params),
level: search_service(additional_params).level,
2022-08-27 11:52:29 +05:30
scope: search_scope,
search_duration_s: @search_duration_s
)
end
2018-03-17 18:26:18 +05:30
end
resource :search do
desc 'Search on GitLab' do
detail 'This feature was introduced in GitLab 10.5.'
end
params do
requires :search, type: String, desc: 'The expression it should be searched for'
requires :scope,
type: String,
2019-07-07 11:18:12 +05:30
desc: 'The scope of the search',
values: Helpers::SearchHelpers.global_search_scopes
2020-11-24 15:15:51 +05:30
optional :state, type: String, desc: 'Filter results by state', values: Helpers::SearchHelpers.search_states
2021-01-03 14:25:43 +05:30
optional :confidential, type: Boolean, desc: 'Filter results by confidentiality'
2018-03-17 18:26:18 +05:30
use :pagination
end
get do
2020-03-13 15:44:24 +05:30
verify_search_scope!(resource: nil)
2019-07-07 11:18:12 +05:30
2022-09-01 20:07:04 +05:30
present search, with: entity, current_user: current_user
2018-03-17 18:26:18 +05:30
end
end
2019-02-15 15:39:39 +05:30
resource :groups, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
2018-03-17 18:26:18 +05:30
desc 'Search on GitLab' do
detail 'This feature was introduced in GitLab 10.5.'
end
params do
requires :id, type: String, desc: 'The ID of a group'
requires :search, type: String, desc: 'The expression it should be searched for'
requires :scope,
type: String,
2019-07-07 11:18:12 +05:30
desc: 'The scope of the search',
values: Helpers::SearchHelpers.group_search_scopes
2020-11-24 15:15:51 +05:30
optional :state, type: String, desc: 'Filter results by state', values: Helpers::SearchHelpers.search_states
2021-01-03 14:25:43 +05:30
optional :confidential, type: Boolean, desc: 'Filter results by confidentiality'
2018-03-17 18:26:18 +05:30
use :pagination
end
2018-05-09 12:01:36 +05:30
get ':id/(-/)search' do
2020-03-13 15:44:24 +05:30
verify_search_scope!(resource: user_group)
2019-07-07 11:18:12 +05:30
2022-09-01 20:07:04 +05:30
present search(group_id: user_group.id), with: entity, current_user: current_user
2018-03-17 18:26:18 +05:30
end
end
2019-02-15 15:39:39 +05:30
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
2018-03-17 18:26:18 +05:30
desc 'Search on GitLab' do
detail 'This feature was introduced in GitLab 10.5.'
end
params do
2023-01-13 00:05:48 +05:30
requires :id, types: [String, Integer], desc: 'The ID or URL-encoded path of the project'
2018-03-17 18:26:18 +05:30
requires :search, type: String, desc: 'The expression it should be searched for'
requires :scope,
type: String,
2019-07-07 11:18:12 +05:30
desc: 'The scope of the search',
values: Helpers::SearchHelpers.project_search_scopes
2019-09-04 21:01:54 +05:30
optional :ref, type: String, desc: 'The name of a repository branch or tag. If not given, the default branch is used'
2020-11-24 15:15:51 +05:30
optional :state, type: String, desc: 'Filter results by state', values: Helpers::SearchHelpers.search_states
2021-01-03 14:25:43 +05:30
optional :confidential, type: Boolean, desc: 'Filter results by confidentiality'
2018-03-17 18:26:18 +05:30
use :pagination
end
2018-05-09 12:01:36 +05:30
get ':id/(-/)search' do
2022-09-01 20:07:04 +05:30
present search({ project_id: user_project.id, repository_ref: params[:ref] }), with: entity, current_user: current_user
2018-03-17 18:26:18 +05:30
end
end
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
API::Search.prepend_mod_with('API::Search')