gitlab-org--gitlab-foss/lib/api/search.rb

122 lines
3.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-01-31 14:59:59 +00:00
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,
2018-01-31 14:59:59 +00:00
blobs: Entities::Blob,
wiki_blobs: Entities::Blob,
snippet_titles: Entities::Snippet,
2018-09-17 12:54:32 +00:00
snippet_blobs: Entities::Snippet,
users: Entities::UserBasic
2018-01-31 14:59:59 +00:00
}.freeze
def search(additional_params = {})
search_params = {
scope: params[:scope],
search: params[:search],
snippets: snippets?,
page: params[:page],
per_page: params[:per_page]
2018-01-31 14:59:59 +00:00
}.merge(additional_params)
results = SearchService.new(current_user, search_params).search_objects
process_results(results)
end
def process_results(results)
paginate(results)
2018-01-31 14:59:59 +00:00
end
def snippets?
%w(snippet_blobs snippet_titles).include?(params[:scope]).to_s
end
def entity
SCOPE_ENTITY[params[:scope].to_sym]
end
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
params :scope do |options|
2018-12-19 14:24:46 +00:00
scope_entities =
if Feature.enabled?(:users_search, default_enabled: true)
SCOPE_ENTITY
else
SCOPE_ENTITY.reject { |key, value| key == :users }
end
values = scope_entities.stringify_keys.slice(*options[:values]).keys
requires :scope,
type: String,
desc: 'The scope of the search',
values: values
end
2018-01-31 14:59:59 +00: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'
use :scope, values: Helpers::SearchHelpers.global_search_scopes
2018-01-31 14:59:59 +00:00
use :pagination
end
get do
verify_search_scope!
2018-01-31 14:59:59 +00:00
present search, with: entity
end
end
resource :groups, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
2018-01-31 14:59:59 +00: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'
use :scope, values: Helpers::SearchHelpers.group_search_scopes
2018-01-31 14:59:59 +00:00
use :pagination
end
get ':id/(-/)search' do
verify_search_scope!
present search(group_id: user_group.id), with: entity
2018-01-31 14:59:59 +00:00
end
end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
2018-01-31 14:59:59 +00: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'
use :scope, Helpers::SearchHelpers.project_search_scopes
2018-01-31 14:59:59 +00:00
use :pagination
end
get ':id/(-/)search' do
present search(project_id: user_project.id), with: entity
2018-01-31 14:59:59 +00:00
end
end
end
end