2018-09-14 01:42:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2012-03-15 19:14:39 -04:00
|
|
|
class SearchController < ApplicationController
|
2017-12-11 09:21:06 -05:00
|
|
|
include ControllerWithCrossProjectAccessCheck
|
2014-01-18 07:16:46 -05:00
|
|
|
include SearchHelper
|
2017-08-23 12:53:29 -04:00
|
|
|
include RendersCommits
|
2014-01-18 07:16:46 -05:00
|
|
|
|
2019-06-26 19:19:59 -04:00
|
|
|
around_action :allow_gitaly_ref_name_caching
|
|
|
|
|
2017-12-11 09:21:06 -05:00
|
|
|
skip_before_action :authenticate_user!
|
|
|
|
requires_cross_project_access if: -> do
|
|
|
|
search_term_present = params[:search].present? || params[:term].present?
|
|
|
|
search_term_present && !params[:project_id].present?
|
|
|
|
end
|
|
|
|
|
2015-05-01 04:39:11 -04:00
|
|
|
layout 'search'
|
2015-04-30 13:06:18 -04:00
|
|
|
|
2012-03-15 19:14:39 -04:00
|
|
|
def show
|
2017-03-31 09:03:55 -04:00
|
|
|
@project = search_service.project
|
|
|
|
@group = search_service.group
|
2015-04-28 05:48:42 -04:00
|
|
|
|
2016-11-07 13:36:58 -05:00
|
|
|
return if params[:search].blank?
|
2016-09-09 07:08:49 -04:00
|
|
|
|
2016-04-19 03:52:15 -04:00
|
|
|
@search_term = params[:search]
|
|
|
|
|
2017-03-31 09:03:55 -04:00
|
|
|
@scope = search_service.scope
|
|
|
|
@show_snippets = search_service.show_snippets?
|
|
|
|
@search_results = search_service.search_results
|
|
|
|
@search_objects = search_service.search_objects
|
2017-01-10 23:20:32 -05:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
render_commits if @scope == 'commits'
|
2018-08-15 15:18:45 -04:00
|
|
|
eager_load_user_status if @scope == 'users'
|
2017-08-23 12:53:29 -04:00
|
|
|
|
2019-07-29 05:58:58 -04:00
|
|
|
increment_navbar_searches_counter
|
|
|
|
|
2017-01-10 23:20:32 -05:00
|
|
|
check_single_commit_result
|
2013-10-23 16:27:40 -04:00
|
|
|
end
|
2014-01-18 07:16:46 -05:00
|
|
|
|
2019-07-15 13:59:57 -04:00
|
|
|
def count
|
|
|
|
params.require([:search, :scope])
|
|
|
|
|
|
|
|
scope = search_service.scope
|
|
|
|
count = search_service.search_results.formatted_count(scope)
|
|
|
|
|
|
|
|
render json: { count: count }
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2014-01-18 07:16:46 -05:00
|
|
|
def autocomplete
|
|
|
|
term = params[:term]
|
2015-04-10 12:39:10 -04:00
|
|
|
|
|
|
|
if params[:project_id].present?
|
|
|
|
@project = Project.find_by(id: params[:project_id])
|
|
|
|
@project = nil unless can?(current_user, :read_project, @project)
|
|
|
|
end
|
|
|
|
|
2014-01-18 07:16:46 -05:00
|
|
|
@ref = params[:project_ref] if params[:project_ref].present?
|
|
|
|
|
|
|
|
render json: search_autocomplete_opts(term).to_json
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-01-10 23:20:32 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
def render_commits
|
|
|
|
@search_objects = prepare_commits_for_rendering(@search_objects)
|
|
|
|
end
|
|
|
|
|
2018-08-15 15:18:45 -04:00
|
|
|
def eager_load_user_status
|
2018-12-19 09:24:46 -05:00
|
|
|
return if Feature.disabled?(:users_search, default_enabled: true)
|
|
|
|
|
2018-08-15 15:18:45 -04:00
|
|
|
@search_objects = @search_objects.eager_load(:status) # rubocop:disable CodeReuse/ActiveRecord
|
|
|
|
end
|
|
|
|
|
2017-01-10 23:20:32 -05:00
|
|
|
def check_single_commit_result
|
|
|
|
if @search_results.single_commit_result?
|
|
|
|
only_commit = @search_results.objects('commits').first
|
|
|
|
query = params[:search].strip.downcase
|
|
|
|
found_by_commit_sha = Commit.valid_hash?(query) && only_commit.sha.start_with?(query)
|
|
|
|
|
2017-06-29 13:06:35 -04:00
|
|
|
redirect_to project_commit_path(@project, only_commit) if found_by_commit_sha
|
2017-01-10 23:20:32 -05:00
|
|
|
end
|
|
|
|
end
|
2019-07-29 05:58:58 -04:00
|
|
|
|
|
|
|
def increment_navbar_searches_counter
|
|
|
|
return if params[:nav_source] != 'navbar'
|
|
|
|
|
|
|
|
Gitlab::UsageDataCounters::SearchCounter.increment_navbar_searches_count
|
|
|
|
end
|
2012-03-15 19:14:39 -04:00
|
|
|
end
|