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

136 lines
4.2 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
class Search < Grape::API
include PaginationParams
before { authenticate! }
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
2020-06-23 00:09:42 +05:30
SCOPE_PRELOAD_METHOD = {
merge_requests: :with_api_entity_associations,
projects: :with_api_entity_associations,
issues: :with_api_entity_associations,
milestones: :with_api_entity_associations
}.freeze
2018-03-17 18:26:18 +05:30
def search(additional_params = {})
search_params = {
scope: params[:scope],
search: params[:search],
snippets: snippets?,
page: params[:page],
per_page: params[:per_page]
}.merge(additional_params)
2020-06-23 00:09:42 +05:30
results = SearchService.new(current_user, search_params).search_objects(preload_method)
Gitlab::UsageDataCounters::SearchCounter.count(:all_searches)
2018-03-17 18:26:18 +05:30
2019-02-15 15:39:39 +05:30
paginate(results)
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
SCOPE_PRELOAD_METHOD[params[:scope].to_sym]
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
def check_users_search_allowed!
if params[:scope].to_sym == :users && Feature.disabled?(:users_search, default_enabled: true)
render_api_error!({ error: _("Scope not supported with disabled 'users_search' feature!") }, 400)
end
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
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
check_users_search_allowed!
2018-03-17 18:26:18 +05:30
present search, with: entity
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
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
check_users_search_allowed!
2018-03-17 18:26:18 +05:30
present search(group_id: user_group.id), with: entity
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
requires :id, type: String, desc: 'The ID of a project'
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'
2018-03-17 18:26:18 +05:30
use :pagination
end
2018-05-09 12:01:36 +05:30
get ':id/(-/)search' do
2019-07-07 11:18:12 +05:30
check_users_search_allowed!
2019-09-04 21:01:54 +05:30
present search({ project_id: user_project.id, repository_ref: params[:ref] }), with: entity
2018-03-17 18:26:18 +05:30
end
end
end
end
2019-12-04 20:38:33 +05:30
API::Search.prepend_if_ee('EE::API::Search')