Commit a7e6e2b4 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'ee-42934-search-api-fix' into 'master'

EE port of: API - Include project in commits&blobs search results

See merge request gitlab-org/gitlab-ee!4515
parents 052487b7 f138f82d
......@@ -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
......
......@@ -54,6 +54,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)
......
......@@ -302,7 +302,8 @@ Example response:
"filename": "home.md",
"id": null,
"ref": "master",
"startline": 5
"startline": 5,
"project_id": 6
}
]
```
......@@ -334,7 +335,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
}
]
```
......@@ -358,7 +360,8 @@ Example response:
"filename": "README.md",
"id": null,
"ref": "master",
"startline": 46
"startline": 46,
"project_id": 6
}
]
```
......@@ -603,7 +606,8 @@ Example response:
"filename": "home.md",
"id": null,
"ref": "master",
"startline": 5
"startline": 5,
"project_id": 6
}
]
```
......@@ -635,7 +639,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
}
]
```
......@@ -659,7 +664,8 @@ Example response:
"filename": "README.md",
"id": null,
"ref": "master",
"startline": 46
"startline": 46,
"project_id": 6
}
]
```
......@@ -901,7 +907,8 @@ Example response:
"filename": "home.md",
"id": null,
"ref": "master",
"startline": 5
"startline": 5,
"project_id": 6
}
]
```
......@@ -931,7 +938,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
}
]
```
......@@ -953,7 +961,8 @@ Example response:
"filename": "README.md",
"id": null,
"ref": "master",
"startline": 46
"startline": 46,
"project_id": 6
}
]
```
......
......@@ -77,6 +77,7 @@ module Gitlab
extname = File.extname(filename)
basename = filename.sub(/#{extname}$/, '')
content = result["_source"]["blob"]["content"]
project_id = result["_parent"].to_i
total_lines = content.lines.size
term =
......@@ -113,7 +114,8 @@ module Gitlab
basename: basename,
ref: ref,
startline: from + 1,
data: data.join
data: data.join,
project_id: project_id
)
end
......
......@@ -303,6 +303,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
......@@ -1450,6 +1451,7 @@ module API
expose :id
expose :ref
expose :startline
expose :project_id
end
end
end
......@@ -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,
......@@ -43,7 +43,7 @@ module API
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
......@@ -110,9 +110,7 @@ module API
get ':id/-/search' do
check_elasticsearch_scope!
group = find_group!(params[:id])
present search(group_id: group.id), with: entity
present search(group_id: user_group.id), with: entity
end
end
......@@ -131,9 +129,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
......
......@@ -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
......
......@@ -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
......
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
......
......@@ -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
}
......
......@@ -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" }
}
}
]
......
{
"type": "array",
"items": { "$ref": "commit/detail.json" }
}
......@@ -66,7 +66,7 @@ describe API::Search do
get api(endpoint, user), scope: 'commits', search: 'folder'
end
it_behaves_like 'response is correct', schema: 'public_api/v4/commits', size: 2
it_behaves_like 'response is correct', schema: 'public_api/v4/commits_details', size: 2
end
context 'for blobs scope' do
......@@ -167,7 +167,7 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/snippets'
end
context 'when elasticsearch is enabled' do
context 'when elasticsearch is disabled' do
it_behaves_like 'elasticsearch disabled' do
let(:endpoint) { '/search' }
end
......@@ -391,7 +391,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
......@@ -399,7 +399,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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment