BigW Consortium Gitlab

Search results libraries added

Gitlab::SearchResults and Gitlab::ProjectSearchResults are libraries we are going to use to get search results based on query, enitity type and pagination. It will allow us to get only issues from project #23 where title or description includes 'foo'. Ex: search_results = Gitlab::ProjectSearchResults.new(project.id, 'foo', 'issues') search_results.objects => # [<Issues #23>, <Issues #34>] search_results.issues_count => 2 search_results.total_count => 12 (it includes results from comments and merge requests too) Signed-off-by: 's avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent b3b50bb8
module Gitlab
class ProjectSearchResults < SearchResults
attr_reader :project, :repository_ref
def initialize(project_id, query, scope = nil, page = nil, repository_ref = nil)
@project = Project.find(project_id)
@repository_ref = repository_ref
@page = page
@query = Shellwords.shellescape(query) if query.present?
@scope = scope
unless %w(blobs notes issues merge_requests).include?(@scope)
@scope = default_scope
end
end
def objects
case scope
when 'notes'
notes.page(page).per(per_page)
when 'blobs'
Kaminari.paginate_array(blobs).page(page).per(per_page)
else
super
end
end
def total_count
@total_count ||= issues_count + merge_requests_count + blobs_count + notes_count
end
def blobs_count
@blobs_count ||= blobs.count
end
def notes_count
@notes_count ||= notes.count
end
private
def blobs
if project.empty_repo?
[]
else
project.repository.search_files(query, repository_ref)
end
end
def notes
Note.where(project_id: limit_project_ids).search(query).order('updated_at DESC')
end
def default_scope
'blobs'
end
def limit_project_ids
[project.id]
end
end
end
module Gitlab
class SearchResults
attr_reader :scope, :objects, :query, :page
# Limit search results by passed project ids
# It allows us to search only for projects user has access to
attr_reader :limit_project_ids
def initialize(limit_project_ids, query, scope = nil, page = nil)
@limit_project_ids = limit_project_ids || Project.all
@page = page
@query = Shellwords.shellescape(query) if query.present?
@scope = scope
unless %w(projects issues merge_requests).include?(@scope)
@scope = default_scope
end
end
def objects
case scope
when 'projects'
projects.page(page).per(per_page)
when 'issues'
issues.page(page).per(per_page)
when 'merge_requests'
merge_requests.page(page).per(per_page)
else
Kaminari.paginate_array([]).page(page).per(per_page)
end
end
def total_count
@total_count ||= projects_count + issues_count + merge_requests_count
end
def projects_count
@projects_count ||= projects.count
end
def issues_count
@issues_count ||= issues.count
end
def merge_requests_count
@merge_requests_count ||= merge_requests.count
end
def empty?
total_count.zero?
end
private
def projects
Project.where(id: limit_project_ids).search(query)
end
def issues
Issue.where(project_id: limit_project_ids).search(query).order('updated_at DESC')
end
def merge_requests
MergeRequest.in_projects(limit_project_ids).search(query).order('updated_at DESC')
end
def default_scope
'projects'
end
def per_page
20
end
end
end
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