From f3af06526ad64e653183e4ffd0ea3bbdef5b1bda Mon Sep 17 00:00:00 2001 From: Sean McGivern Date: Wed, 14 Feb 2018 10:15:49 +0000 Subject: Merge branch '42934-search-api-fix' into 'master' Add information about projects when searching in wiki_blobs, blobs, commits scopes Closes #42934 See merge request gitlab-org/gitlab-ce!17095 --- app/models/commit.rb | 4 ++++ doc/api/README.md | 1 + doc/api/search.md | 9 ++++++--- lib/api/entities.rb | 2 ++ lib/api/search.rb | 12 ++++-------- lib/gitlab/file_finder.rb | 5 +++-- lib/gitlab/project_search_results.rb | 5 +++-- lib/gitlab/search_results.rb | 3 ++- spec/fixtures/api/schemas/public_api/v4/blobs.json | 3 ++- spec/fixtures/api/schemas/public_api/v4/commit/detail.json | 7 ++++--- spec/fixtures/api/schemas/public_api/v4/commits_details.json | 4 ++++ spec/requests/api/search_spec.rb | 4 ++-- 12 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 spec/fixtures/api/schemas/public_api/v4/commits_details.json diff --git a/app/models/commit.rb b/app/models/commit.rb index 2d2d89af030..8c960389652 100644 --- a/app/models/commit.rb +++ b/app/models/commit.rb @@ -116,6 +116,10 @@ class Commit raw.id end + def project_id + project.id + end + def ==(other) other.is_a?(self.class) && raw == other.raw end diff --git a/doc/api/README.md b/doc/api/README.md index f226716c3b5..88710eae4fe 100644 --- a/doc/api/README.md +++ b/doc/api/README.md @@ -49,6 +49,7 @@ following locations: - [Repositories](repositories.md) - [Repository Files](repository_files.md) - [Runners](runners.md) +- [Search](search.md) - [Services](services.md) - [Settings](settings.md) - [Sidekiq metrics](sidekiq_metrics.md) diff --git a/doc/api/search.md b/doc/api/search.md index 1fba9c3fbb8..d441b556186 100644 --- a/doc/api/search.md +++ b/doc/api/search.md @@ -737,7 +737,8 @@ Example response: "filename": "home.md", "id": null, "ref": "master", - "startline": 5 + "startline": 5, + "project_id": 6 } ] ``` @@ -767,7 +768,8 @@ Example response: "authored_date": "2013-02-18T22:02:54.000Z", "committer_name": "angus croll", "committer_email": "anguscroll@gmail.com", - "committed_date": "2013-02-18T22:02:54.000Z" + "committed_date": "2013-02-18T22:02:54.000Z", + "project_id": 6 } ] ``` @@ -789,7 +791,8 @@ Example response: "filename": "README.md", "id": null, "ref": "master", - "startline": 46 + "startline": 46, + "project_id": 6 } ] ``` diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 7838de13c56..1608af97d38 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -274,6 +274,7 @@ module API expose :stats, using: Entities::CommitStats, if: :stats expose :status expose :last_pipeline, using: 'API::Entities::PipelineBasic' + expose :project_id end class Branch < Grape::Entity @@ -1172,6 +1173,7 @@ module API expose :id expose :ref expose :startline + expose :project_id end end end 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 diff --git a/lib/gitlab/file_finder.rb b/lib/gitlab/file_finder.rb index 10ffc345bd5..8c082c0c336 100644 --- a/lib/gitlab/file_finder.rb +++ b/lib/gitlab/file_finder.rb @@ -28,7 +28,7 @@ module Gitlab def find_by_content(query) results = repository.search_files_by_content(query, ref).first(BATCH_SIZE) - results.map { |result| Gitlab::ProjectSearchResults.parse_search_result(result) } + results.map { |result| Gitlab::ProjectSearchResults.parse_search_result(result, project) } end def find_by_filename(query, except: []) @@ -45,7 +45,8 @@ module Gitlab basename: File.basename(blob.path), ref: ref, startline: 1, - data: blob.data + data: blob.data, + project: project ) end end diff --git a/lib/gitlab/project_search_results.rb b/lib/gitlab/project_search_results.rb index 9e2fa07a205..cf0935dbd9a 100644 --- a/lib/gitlab/project_search_results.rb +++ b/lib/gitlab/project_search_results.rb @@ -41,7 +41,7 @@ module Gitlab @commits_count ||= commits.count end - def self.parse_search_result(result) + def self.parse_search_result(result, project = nil) ref = nil filename = nil basename = nil @@ -66,7 +66,8 @@ module Gitlab basename: basename, ref: ref, startline: startline, - data: data + data: data, + project_id: project ? project.id : nil ) end diff --git a/lib/gitlab/search_results.rb b/lib/gitlab/search_results.rb index 5ad219179f3..5a5ae7f19d4 100644 --- a/lib/gitlab/search_results.rb +++ b/lib/gitlab/search_results.rb @@ -1,7 +1,7 @@ module Gitlab class SearchResults class FoundBlob - attr_reader :id, :filename, :basename, :ref, :startline, :data + attr_reader :id, :filename, :basename, :ref, :startline, :data, :project_id def initialize(opts = {}) @id = opts.fetch(:id, nil) @@ -11,6 +11,7 @@ module Gitlab @startline = opts.fetch(:startline, nil) @data = opts.fetch(:data, nil) @per_page = opts.fetch(:per_page, 20) + @project_id = opts.fetch(:project_id, nil) end def path diff --git a/spec/fixtures/api/schemas/public_api/v4/blobs.json b/spec/fixtures/api/schemas/public_api/v4/blobs.json index 9cb1eae3762..a812815838f 100644 --- a/spec/fixtures/api/schemas/public_api/v4/blobs.json +++ b/spec/fixtures/api/schemas/public_api/v4/blobs.json @@ -7,11 +7,12 @@ "data": { "type": "string" }, "filename": { "type": ["string"] }, "id": { "type": ["string", "null"] }, + "project_id": { "type": "integer" }, "ref": { "type": "string" }, "startline": { "type": "integer" } }, "required": [ - "basename", "data", "filename", "id", "ref", "startline" + "basename", "data", "filename", "id", "ref", "startline", "project_id" ], "additionalProperties": false } diff --git a/spec/fixtures/api/schemas/public_api/v4/commit/detail.json b/spec/fixtures/api/schemas/public_api/v4/commit/detail.json index 88a3cad62f6..477e776a804 100644 --- a/spec/fixtures/api/schemas/public_api/v4/commit/detail.json +++ b/spec/fixtures/api/schemas/public_api/v4/commit/detail.json @@ -4,9 +4,9 @@ { "$ref": "basic.json" }, { "required" : [ - "stats", "status", - "last_pipeline" + "last_pipeline", + "project_id" ], "properties": { "stats": { "$ref": "../commit_stats.json" }, @@ -16,7 +16,8 @@ { "type": "null" }, { "$ref": "../pipeline/basic.json" } ] - } + }, + "project_id": { "type": "integer" } } } ] diff --git a/spec/fixtures/api/schemas/public_api/v4/commits_details.json b/spec/fixtures/api/schemas/public_api/v4/commits_details.json new file mode 100644 index 00000000000..1f5b1ad86ef --- /dev/null +++ b/spec/fixtures/api/schemas/public_api/v4/commits_details.json @@ -0,0 +1,4 @@ +{ + "type": "array", + "items": { "$ref": "commit/detail.json" } +} diff --git a/spec/requests/api/search_spec.rb b/spec/requests/api/search_spec.rb index ddda5752f0c..9052a18c60b 100644 --- a/spec/requests/api/search_spec.rb +++ b/spec/requests/api/search_spec.rb @@ -295,7 +295,7 @@ describe API::Search do get api("/projects/#{repo_project.id}/-/search", user), scope: 'commits', search: '498214de67004b1da3d820901307bed2a68a8ef6' end - it_behaves_like 'response is correct', schema: 'public_api/v4/commits' + it_behaves_like 'response is correct', schema: 'public_api/v4/commits_details' end context 'for commits scope with project path as id' do @@ -303,7 +303,7 @@ describe API::Search do get api("/projects/#{CGI.escape(repo_project.full_path)}/-/search", user), scope: 'commits', search: '498214de67004b1da3d820901307bed2a68a8ef6' end - it_behaves_like 'response is correct', schema: 'public_api/v4/commits' + it_behaves_like 'response is correct', schema: 'public_api/v4/commits_details' end context 'for blobs scope' do -- cgit v1.2.1