2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
class SearchController < ApplicationController
|
2018-03-27 19:54:05 +05:30
|
|
|
include ControllerWithCrossProjectAccessCheck
|
2014-09-02 18:07:02 +05:30
|
|
|
include SearchHelper
|
2018-03-17 18:26:18 +05:30
|
|
|
include RendersCommits
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
before_action :override_snippet_scope, only: :show
|
2019-09-30 21:07:59 +05:30
|
|
|
around_action :allow_gitaly_ref_name_caching
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
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-09-11 14:41:01 +05:30
|
|
|
layout 'search'
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def show
|
2017-08-17 22:00:37 +05:30
|
|
|
@project = search_service.project
|
|
|
|
@group = search_service.group
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
return if params[:search].blank?
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
return unless search_term_valid?
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
@search_term = params[:search]
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
@scope = search_service.scope
|
|
|
|
@show_snippets = search_service.show_snippets?
|
|
|
|
@search_results = search_service.search_results
|
|
|
|
@search_objects = search_service.search_objects
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
render_commits if @scope == 'commits'
|
2019-07-07 11:18:12 +05:30
|
|
|
eager_load_user_status if @scope == 'users'
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
increment_navbar_searches_counter
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
check_single_commit_result
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def count
|
|
|
|
params.require([:search, :scope])
|
|
|
|
|
|
|
|
scope = search_service.scope
|
|
|
|
count = search_service.search_results.formatted_count(scope)
|
|
|
|
|
|
|
|
render json: { count: count }
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2014-09-02 18:07:02 +05:30
|
|
|
def autocomplete
|
|
|
|
term = params[:term]
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
if params[:project_id].present?
|
|
|
|
@project = Project.find_by(id: params[:project_id])
|
|
|
|
@project = nil unless can?(current_user, :read_project, @project)
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
@ref = params[:project_ref] if params[:project_ref].present?
|
|
|
|
|
|
|
|
render json: search_autocomplete_opts(term).to_json
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
def search_term_valid?
|
|
|
|
unless search_service.valid_query_length?
|
|
|
|
flash[:alert] = t('errors.messages.search_chars_too_long', count: SearchService::SEARCH_CHAR_LIMIT)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
unless search_service.valid_terms_count?
|
|
|
|
flash[:alert] = t('errors.messages.search_terms_too_long', count: SearchService::SEARCH_TERM_LIMIT)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def render_commits
|
|
|
|
@search_objects = prepare_commits_for_rendering(@search_objects)
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def eager_load_user_status
|
|
|
|
return if Feature.disabled?(:users_search, default_enabled: true)
|
|
|
|
|
|
|
|
@search_objects = @search_objects.eager_load(:status) # rubocop:disable CodeReuse/ActiveRecord
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
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-09-10 17:25:29 +05:30
|
|
|
redirect_to project_commit_path(@project, only_commit) if found_by_commit_sha
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
def increment_navbar_searches_counter
|
|
|
|
return if params[:nav_source] != 'navbar'
|
|
|
|
|
|
|
|
Gitlab::UsageDataCounters::SearchCounter.increment_navbar_searches_count
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
# Disallow web snippet_blobs search as we migrate snippet
|
|
|
|
# from database-backed storage to git repository-based,
|
|
|
|
# and searching across multiple git repositories is not feasible.
|
|
|
|
#
|
|
|
|
# TODO: after 13.0 refactor this into Search::SnippetService
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/issues/208882
|
|
|
|
def override_snippet_scope
|
|
|
|
params[:scope] = 'snippet_titles' if params[:snippets] == 'true'
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|