2012-10-09 15:09:46 -04:00
|
|
|
class SearchContext
|
2013-11-06 10:13:21 -05:00
|
|
|
attr_accessor :project_ids, :current_user, :params
|
2012-10-09 15:09:46 -04:00
|
|
|
|
2013-11-06 10:13:21 -05:00
|
|
|
def initialize(project_ids, user, params)
|
|
|
|
@project_ids, @current_user, @params = project_ids, user, params.dup
|
2012-10-09 15:09:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2013-10-31 10:35:06 -04:00
|
|
|
query = params[:search]
|
|
|
|
query = Shellwords.shellescape(query) if query.present?
|
2012-10-09 15:09:46 -04:00
|
|
|
|
|
|
|
return result unless query.present?
|
2013-11-06 10:13:21 -05:00
|
|
|
visibility_levels = @current_user ? [ Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC ] : [ Gitlab::VisibilityLevel::PUBLIC ]
|
|
|
|
result[:projects] = Project.where("projects.id in (?) OR projects.visibility_level in (?)", project_ids, visibility_levels).search(query).limit(20)
|
2013-05-09 04:27:14 -04:00
|
|
|
|
2013-09-22 00:50:18 -04:00
|
|
|
# Search inside single project
|
2013-10-24 11:16:14 -04:00
|
|
|
single_project_search(Project.where(id: project_ids), query)
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def single_project_search(projects, query)
|
2013-05-14 13:11:48 -04:00
|
|
|
project = projects.first if projects.length == 1
|
2013-05-09 04:27:14 -04:00
|
|
|
|
|
|
|
if params[:search_code].present?
|
2013-05-14 13:25:16 -04:00
|
|
|
result[:blobs] = project.repository.search_files(query, params[:repository_ref]) unless project.empty_repo?
|
2013-05-09 04:27:14 -04:00
|
|
|
else
|
2013-08-15 04:54:16 -04:00
|
|
|
result[:merge_requests] = MergeRequest.in_projects(project_ids).search(query).order('updated_at DESC').limit(20)
|
|
|
|
result[:issues] = Issue.where(project_id: project_ids).search(query).order('updated_at DESC').limit(20)
|
2013-05-09 04:27:14 -04:00
|
|
|
result[:wiki_pages] = []
|
2013-04-14 13:07:11 -04:00
|
|
|
end
|
2012-10-09 15:09:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def result
|
|
|
|
@result ||= {
|
|
|
|
projects: [],
|
|
|
|
merge_requests: [],
|
2012-10-16 15:05:11 -04:00
|
|
|
issues: [],
|
2013-04-14 13:07:11 -04:00
|
|
|
wiki_pages: [],
|
2013-05-09 04:27:14 -04:00
|
|
|
blobs: []
|
2012-10-09 15:09:46 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|