2015-04-26 12:48:37 +05:30
|
|
|
module Gitlab
|
|
|
|
class SearchResults
|
2017-08-17 22:00:37 +05:30
|
|
|
class FoundBlob
|
|
|
|
attr_reader :id, :filename, :basename, :ref, :startline, :data
|
|
|
|
|
|
|
|
def initialize(opts = {})
|
|
|
|
@id = opts.fetch(:id, nil)
|
|
|
|
@filename = opts.fetch(:filename, nil)
|
|
|
|
@basename = opts.fetch(:basename, nil)
|
|
|
|
@ref = opts.fetch(:ref, nil)
|
|
|
|
@startline = opts.fetch(:startline, nil)
|
|
|
|
@data = opts.fetch(:data, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def path
|
|
|
|
filename
|
|
|
|
end
|
|
|
|
|
|
|
|
def no_highlighting?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
attr_reader :current_user, :query
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
# Limit search results by passed projects
|
2015-04-26 12:48:37 +05:30
|
|
|
# It allows us to search only for projects user has access to
|
2016-06-02 11:05:42 +05:30
|
|
|
attr_reader :limit_projects
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def initialize(current_user, limit_projects, query)
|
|
|
|
@current_user = current_user
|
|
|
|
@limit_projects = limit_projects || Project.all
|
2015-04-26 12:48:37 +05:30
|
|
|
@query = Shellwords.shellescape(query) if query.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def objects(scope, page = nil)
|
|
|
|
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)
|
2015-09-25 12:07:36 +05:30
|
|
|
when 'milestones'
|
|
|
|
milestones.page(page).per(per_page)
|
2015-04-26 12:48:37 +05:30
|
|
|
else
|
|
|
|
Kaminari.paginate_array([]).page(page).per(per_page)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
def milestones_count
|
|
|
|
@milestones_count ||= milestones.count
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def single_commit_result?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def projects
|
2016-06-02 11:05:42 +05:30
|
|
|
limit_projects.search(query)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def issues
|
2017-01-15 13:20:01 +05:30
|
|
|
issues = IssuesFinder.new(current_user).execute.where(project_id: project_ids_relation)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
issues =
|
|
|
|
if query =~ /#(\d+)\z/
|
|
|
|
issues.where(iid: $1)
|
|
|
|
else
|
|
|
|
issues.full_search(query)
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
issues.order('updated_at DESC')
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
def milestones
|
2016-06-02 11:05:42 +05:30
|
|
|
milestones = Milestone.where(project_id: project_ids_relation)
|
2015-09-25 12:07:36 +05:30
|
|
|
milestones = milestones.search(query)
|
|
|
|
milestones.order('updated_at DESC')
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def merge_requests
|
2017-01-15 13:20:01 +05:30
|
|
|
merge_requests = MergeRequestsFinder.new(current_user).execute.in_projects(project_ids_relation)
|
2017-08-17 22:00:37 +05:30
|
|
|
merge_requests =
|
|
|
|
if query =~ /[#!](\d+)\z/
|
|
|
|
merge_requests.where(iid: $1)
|
|
|
|
else
|
|
|
|
merge_requests.full_search(query)
|
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
merge_requests.order('updated_at DESC')
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def default_scope
|
|
|
|
'projects'
|
|
|
|
end
|
|
|
|
|
|
|
|
def per_page
|
|
|
|
20
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
def project_ids_relation
|
|
|
|
limit_projects.select(:id).reorder(nil)
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|