2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-31 09:59:59 -05: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,
|
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,
|
|
|
|
snippet_blobs: Entities::Snippet
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
def search(additional_params = {})
|
|
|
|
search_params = {
|
|
|
|
scope: params[:scope],
|
|
|
|
search: params[:search],
|
|
|
|
snippets: snippets?,
|
|
|
|
page: params[:page],
|
2018-02-06 11:53:42 -05:00
|
|
|
per_page: params[:per_page]
|
2018-01-31 09:59:59 -05:00
|
|
|
}.merge(additional_params)
|
|
|
|
|
|
|
|
results = SearchService.new(current_user, search_params).search_objects
|
|
|
|
|
|
|
|
process_results(results)
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_results(results)
|
2018-12-02 16:47:33 -05:00
|
|
|
paginate(results)
|
2018-01-31 09:59:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def snippets?
|
|
|
|
%w(snippet_blobs snippet_titles).include?(params[:scope]).to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def entity
|
|
|
|
SCOPE_ENTITY[params[:scope].to_sym]
|
|
|
|
end
|
|
|
|
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'
|
2018-02-07 08:20:18 -05:00
|
|
|
requires :scope,
|
|
|
|
type: String,
|
|
|
|
desc: 'The scope of search, available scopes:
|
|
|
|
projects, issues, merge_requests, milestones, snippet_titles, snippet_blobs',
|
2018-01-31 09:59:59 -05:00
|
|
|
values: %w(projects issues merge_requests milestones snippet_titles snippet_blobs)
|
|
|
|
use :pagination
|
|
|
|
end
|
|
|
|
get do
|
|
|
|
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'
|
2018-02-07 08:20:18 -05:00
|
|
|
requires :scope,
|
|
|
|
type: String,
|
|
|
|
desc: 'The scope of search, available scopes:
|
|
|
|
projects, issues, merge_requests, milestones',
|
2018-01-31 09:59:59 -05:00
|
|
|
values: %w(projects issues merge_requests milestones)
|
|
|
|
use :pagination
|
|
|
|
end
|
2018-03-14 11:49:21 -04:00
|
|
|
get ':id/(-/)search' do
|
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'
|
2018-02-07 08:20:18 -05:00
|
|
|
requires :scope,
|
|
|
|
type: String,
|
|
|
|
desc: 'The scope of search, available scopes:
|
|
|
|
issues, merge_requests, milestones, notes, wiki_blobs, commits, blobs',
|
2018-01-31 09:59:59 -05:00
|
|
|
values: %w(issues merge_requests milestones notes wiki_blobs commits blobs)
|
|
|
|
use :pagination
|
|
|
|
end
|
2018-03-14 11:49:21 -04:00
|
|
|
get ':id/(-/)search' do
|
2018-02-13 07:41:35 -05:00
|
|
|
present search(project_id: user_project.id), with: entity
|
2018-01-31 09:59:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|