BigW Consortium Gitlab

search_controller.rb 1.29 KB
Newer Older
1
class SearchController < ApplicationController
2
  skip_before_action :authenticate_user!
3

4 5
  include SearchHelper

6
  layout 'search'
7

8
  def show
9
    search_service = SearchService.new(current_user, params)
10

11 12
    @project = search_service.project
    @group = search_service.group
13

14
    return if params[:search].blank?
15

16 17
    @search_term = params[:search]

18 19 20 21
    @scope = search_service.scope
    @show_snippets = search_service.show_snippets?
    @search_results = search_service.search_results
    @search_objects = search_service.search_objects
22 23

    check_single_commit_result
24
  end
25 26 27

  def autocomplete
    term = params[:term]
28 29 30 31 32 33

    if params[:project_id].present?
      @project = Project.find_by(id: params[:project_id])
      @project = nil unless can?(current_user, :read_project, @project)
    end

34 35 36 37
    @ref = params[:project_ref] if params[:project_ref].present?

    render json: search_autocomplete_opts(term).to_json
  end
38 39 40 41 42 43 44 45 46

  private

  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)

47
      redirect_to project_commit_path(@project, only_commit) if found_by_commit_sha
48 49
    end
  end
50
end