2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-31 09:59:59 -05:00
|
|
|
module API
|
2020-10-14 20:08:42 -04:00
|
|
|
class Search < ::API::Base
|
2018-01-31 09:59:59 -05:00
|
|
|
include PaginationParams
|
|
|
|
|
2021-12-20 13:13:27 -05:00
|
|
|
before do
|
|
|
|
authenticate!
|
|
|
|
|
2022-03-14 02:07:47 -04:00
|
|
|
check_rate_limit!(:search_rate_limit, scope: [current_user])
|
2021-12-20 13:13:27 -05:00
|
|
|
end
|
2018-01-31 09:59:59 -05:00
|
|
|
|
2020-10-30 14:08:56 -04:00
|
|
|
feature_category :global_search
|
2022-05-02 20:08:25 -04:00
|
|
|
urgency :low
|
2020-10-30 14:08:56 -04:00
|
|
|
|
2021-12-07 07:10:33 -05:00
|
|
|
rescue_from ActiveRecord::QueryCanceled do |e|
|
|
|
|
render_api_error!({ error: 'Request timed out' }, 408)
|
|
|
|
end
|
|
|
|
|
2018-01-31 09:59:59 -05:00
|
|
|
helpers do
|
|
|
|
SCOPE_ENTITY = {
|
|
|
|
merge_requests: Entities::MergeRequestBasic,
|
|
|
|
issues: Entities::IssueBasic,
|
|
|
|
projects: Entities::BasicProjectDetails,
|
|
|
|
milestones: Entities::Milestone,
|
|
|
|
notes: Entities::Note,
|
2018-02-13 07:41:35 -05:00
|
|
|
commits: Entities::CommitDetail,
|
2018-01-31 09:59:59 -05:00
|
|
|
blobs: Entities::Blob,
|
|
|
|
wiki_blobs: Entities::Blob,
|
|
|
|
snippet_titles: Entities::Snippet,
|
2018-09-17 08:54:32 -04:00
|
|
|
users: Entities::UserBasic
|
2018-01-31 09:59:59 -05:00
|
|
|
}.freeze
|
|
|
|
|
2021-04-07 11:09:18 -04:00
|
|
|
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-05-20 02:08:06 -04:00
|
|
|
|
2021-12-20 13:13:27 -05:00
|
|
|
def search_service(additional_params = {})
|
2018-01-31 09:59:59 -05:00
|
|
|
search_params = {
|
|
|
|
scope: params[:scope],
|
|
|
|
search: params[:search],
|
2020-09-14 23:09:24 -04:00
|
|
|
state: params[:state],
|
2020-10-15 08:09:06 -04:00
|
|
|
confidential: params[:confidential],
|
2018-01-31 09:59:59 -05:00
|
|
|
snippets: snippets?,
|
2020-10-23 20:08:35 -04:00
|
|
|
basic_search: params[:basic_search],
|
2018-01-31 09:59:59 -05:00
|
|
|
page: params[:page],
|
2020-11-04 19:09:16 -05:00
|
|
|
per_page: params[:per_page],
|
|
|
|
order_by: params[:order_by],
|
|
|
|
sort: params[:sort]
|
2018-01-31 09:59:59 -05:00
|
|
|
}.merge(additional_params)
|
|
|
|
|
2021-12-20 13:13:27 -05:00
|
|
|
SearchService.new(current_user, search_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def search(additional_params = {})
|
|
|
|
results = search_service(additional_params).search_objects(preload_method)
|
2018-01-31 09:59:59 -05:00
|
|
|
|
2020-05-25 02:08:38 -04:00
|
|
|
Gitlab::UsageDataCounters::SearchCounter.count(:all_searches)
|
|
|
|
|
2018-12-02 16:47:33 -05:00
|
|
|
paginate(results)
|
2018-01-31 09:59:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def snippets?
|
2020-04-28 14:09:35 -04:00
|
|
|
%w(snippet_titles).include?(params[:scope]).to_s
|
2018-01-31 09:59:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def entity
|
|
|
|
SCOPE_ENTITY[params[:scope].to_sym]
|
|
|
|
end
|
2019-02-27 11:51:20 -05:00
|
|
|
|
2020-05-20 02:08:06 -04:00
|
|
|
def preload_method
|
2021-04-07 11:09:18 -04:00
|
|
|
scope_preload_method[params[:scope].to_sym]
|
2020-05-20 02:08:06 -04:00
|
|
|
end
|
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
def verify_search_scope!(resource:)
|
2019-02-27 11:51:20 -05:00
|
|
|
# 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
|
2018-01-31 09:59:59 -05:00
|
|
|
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'
|
2019-02-06 14:55:58 -05:00
|
|
|
requires :scope,
|
|
|
|
type: String,
|
|
|
|
desc: 'The scope of the search',
|
|
|
|
values: Helpers::SearchHelpers.global_search_scopes
|
2020-09-14 23:09:24 -04:00
|
|
|
optional :state, type: String, desc: 'Filter results by state', values: Helpers::SearchHelpers.search_states
|
2020-10-15 08:09:06 -04:00
|
|
|
optional :confidential, type: Boolean, desc: 'Filter results by confidentiality'
|
2018-01-31 09:59:59 -05:00
|
|
|
use :pagination
|
|
|
|
end
|
|
|
|
get do
|
2020-01-28 07:08:44 -05:00
|
|
|
verify_search_scope!(resource: nil)
|
2019-02-27 11:51:20 -05:00
|
|
|
|
2018-01-31 09:59:59 -05:00
|
|
|
present search, with: entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-08 07:18:17 -05:00
|
|
|
resource :groups, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2018-01-31 09:59:59 -05:00
|
|
|
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'
|
2019-02-06 14:55:58 -05:00
|
|
|
requires :scope,
|
|
|
|
type: String,
|
|
|
|
desc: 'The scope of the search',
|
|
|
|
values: Helpers::SearchHelpers.group_search_scopes
|
2020-09-14 23:09:24 -04:00
|
|
|
optional :state, type: String, desc: 'Filter results by state', values: Helpers::SearchHelpers.search_states
|
2020-10-15 08:09:06 -04:00
|
|
|
optional :confidential, type: Boolean, desc: 'Filter results by confidentiality'
|
2018-01-31 09:59:59 -05:00
|
|
|
use :pagination
|
|
|
|
end
|
2018-03-14 11:49:21 -04:00
|
|
|
get ':id/(-/)search' do
|
2020-01-28 07:08:44 -05:00
|
|
|
verify_search_scope!(resource: user_group)
|
2019-02-27 11:51:20 -05:00
|
|
|
|
2018-02-13 07:41:35 -05:00
|
|
|
present search(group_id: user_group.id), with: entity
|
2018-01-31 09:59:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-08 07:18:17 -05:00
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2018-01-31 09:59:59 -05:00
|
|
|
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 project'
|
|
|
|
requires :search, type: String, desc: 'The expression it should be searched for'
|
2019-02-06 14:55:58 -05:00
|
|
|
requires :scope,
|
|
|
|
type: String,
|
|
|
|
desc: 'The scope of the search',
|
|
|
|
values: Helpers::SearchHelpers.project_search_scopes
|
2019-05-17 02:10:08 -04:00
|
|
|
optional :ref, type: String, desc: 'The name of a repository branch or tag. If not given, the default branch is used'
|
2020-09-14 23:09:24 -04:00
|
|
|
optional :state, type: String, desc: 'Filter results by state', values: Helpers::SearchHelpers.search_states
|
2020-10-15 08:09:06 -04:00
|
|
|
optional :confidential, type: Boolean, desc: 'Filter results by confidentiality'
|
2018-01-31 09:59:59 -05:00
|
|
|
use :pagination
|
|
|
|
end
|
2018-03-14 11:49:21 -04:00
|
|
|
get ':id/(-/)search' do
|
2019-05-17 02:10:08 -04:00
|
|
|
present search({ project_id: user_project.id, repository_ref: params[:ref] }), with: entity
|
2018-01-31 09:59:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
API::Search.prepend_mod_with('API::Search')
|