From 7381944565701f2a8db5d58d5bc3c7e52e7f60bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarka=20Kadlecova=CC=81?= Date: Wed, 31 Jan 2018 15:59:59 +0100 Subject: Support search in API --- lib/api/search.rb | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 lib/api/search.rb (limited to 'lib/api/search.rb') diff --git a/lib/api/search.rb b/lib/api/search.rb new file mode 100644 index 00000000000..31121b3ee2d --- /dev/null +++ b/lib/api/search.rb @@ -0,0 +1,110 @@ +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::Commit, + 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], + per_page: params[:per_page], + without_counts: false + }.merge(additional_params) + + results = SearchService.new(current_user, search_params).search_objects + + process_results(results) + end + + def process_results(results) + case params[:scope] + when 'wiki_blobs' + paginate(results).map { |blob| Gitlab::ProjectSearchResults.parse_search_result(blob) } + when 'blobs' + paginate(results).map { |blob| blob[1] } + else + paginate(results) + end + 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' + requires :scope, type: String, desc: 'The scope of search, available scopes: + projects, issues, merge_requests, milestones, snippet_titles, snippet_blobs', + values: %w(projects issues merge_requests milestones snippet_titles snippet_blobs) + use :pagination + end + get do + present search, with: entity + end + end + + resource :groups, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do + 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, desc: 'The scope of search, available scopes: + projects, issues, merge_requests, milestones', + values: %w(projects issues merge_requests milestones) + use :pagination + end + get ':id/-/search' do + find_group!(params[:id]) + + present search(group_id: params[:id]), with: entity + end + end + + resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do + 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, desc: 'The scope of search, available scopes: + issues, merge_requests, milestones, notes, wiki_blobs, commits, blobs', + values: %w(issues merge_requests milestones notes wiki_blobs commits blobs) + use :pagination + end + get ':id/-/search' do + find_project!(params[:id]) + + present search(project_id: params[:id]), with: entity + end + end + end +end -- cgit v1.2.1 From 1b2400b529628da08e406e2391ef37c0b05f889e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarka=20Kadlecov=C3=A1?= Date: Tue, 6 Feb 2018 17:53:42 +0100 Subject: Return only limited pagination headers for search API endpoints --- lib/api/search.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/api/search.rb') diff --git a/lib/api/search.rb b/lib/api/search.rb index 31121b3ee2d..3912c66657e 100644 --- a/lib/api/search.rb +++ b/lib/api/search.rb @@ -24,8 +24,7 @@ module API search: params[:search], snippets: snippets?, page: params[:page], - per_page: params[:per_page], - without_counts: false + per_page: params[:per_page] }.merge(additional_params) results = SearchService.new(current_user, search_params).search_objects -- cgit v1.2.1 From 86e98c832a4eeaac616daef4ef9ddebeb7191fdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarka=20Kadlecov=C3=A1?= Date: Wed, 7 Feb 2018 14:20:18 +0100 Subject: Small code/doc changes --- lib/api/search.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'lib/api/search.rb') diff --git a/lib/api/search.rb b/lib/api/search.rb index 3912c66657e..9f08fd96a3b 100644 --- a/lib/api/search.rb +++ b/lib/api/search.rb @@ -58,8 +58,10 @@ module API end params do requires :search, type: String, desc: 'The expression it should be searched for' - requires :scope, type: String, desc: 'The scope of search, available scopes: - projects, issues, merge_requests, milestones, snippet_titles, snippet_blobs', + requires :scope, + type: String, + desc: 'The scope of search, available scopes: + projects, issues, merge_requests, milestones, snippet_titles, snippet_blobs', values: %w(projects issues merge_requests milestones snippet_titles snippet_blobs) use :pagination end @@ -75,8 +77,10 @@ module API 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, desc: 'The scope of search, available scopes: - projects, issues, merge_requests, milestones', + requires :scope, + type: String, + desc: 'The scope of search, available scopes: + projects, issues, merge_requests, milestones', values: %w(projects issues merge_requests milestones) use :pagination end @@ -94,8 +98,10 @@ module API 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, desc: 'The scope of search, available scopes: - issues, merge_requests, milestones, notes, wiki_blobs, commits, blobs', + requires :scope, + type: String, + desc: 'The scope of search, available scopes: + issues, merge_requests, milestones, notes, wiki_blobs, commits, blobs', values: %w(issues merge_requests milestones notes wiki_blobs commits blobs) use :pagination end -- cgit v1.2.1 From 68ff219c4ec9088005ab872e141912f35ecc59f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarka=20Kadlecov=C3=A1?= Date: Fri, 9 Feb 2018 15:38:52 +0100 Subject: API - fix searching in group/project specified by path --- lib/api/search.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/api/search.rb') diff --git a/lib/api/search.rb b/lib/api/search.rb index 9f08fd96a3b..b9982e03bb3 100644 --- a/lib/api/search.rb +++ b/lib/api/search.rb @@ -85,9 +85,9 @@ module API use :pagination end get ':id/-/search' do - find_group!(params[:id]) + group = find_group!(params[:id]) - present search(group_id: params[:id]), with: entity + present search(group_id: group.id), with: entity end end @@ -106,9 +106,9 @@ module API use :pagination end get ':id/-/search' do - find_project!(params[:id]) + project = find_project!(params[:id]) - present search(project_id: params[:id]), with: entity + present search(project_id: project.id), with: entity end end end -- cgit v1.2.1 From b0b4ae1875529cd7ca786bd5eccd49be9a40a038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarka=20Kadlecov=C3=A1?= Date: Tue, 13 Feb 2018 13:41:35 +0100 Subject: API - Include project in commits&blobs search results --- lib/api/search.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'lib/api/search.rb') diff --git a/lib/api/search.rb b/lib/api/search.rb index b9982e03bb3..3556ad98c52 100644 --- a/lib/api/search.rb +++ b/lib/api/search.rb @@ -11,7 +11,7 @@ module API projects: Entities::BasicProjectDetails, milestones: Entities::Milestone, notes: Entities::Note, - commits: Entities::Commit, + commits: Entities::CommitDetail, blobs: Entities::Blob, wiki_blobs: Entities::Blob, snippet_titles: Entities::Snippet, @@ -35,7 +35,7 @@ module API def process_results(results) case params[:scope] when 'wiki_blobs' - paginate(results).map { |blob| Gitlab::ProjectSearchResults.parse_search_result(blob) } + paginate(results).map { |blob| Gitlab::ProjectSearchResults.parse_search_result(blob, user_project) } when 'blobs' paginate(results).map { |blob| blob[1] } else @@ -85,9 +85,7 @@ module API use :pagination end get ':id/-/search' do - group = find_group!(params[:id]) - - present search(group_id: group.id), with: entity + present search(group_id: user_group.id), with: entity end end @@ -106,9 +104,7 @@ module API use :pagination end get ':id/-/search' do - project = find_project!(params[:id]) - - present search(project_id: project.id), with: entity + present search(project_id: user_project.id), with: entity end end end -- cgit v1.2.1