BigW Consortium Gitlab

search_results.rb 2.71 KB
Newer Older
1 2
module Gitlab
  class SearchResults
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
    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

24
    attr_reader :current_user, :query
25

26
    # Limit search results by passed projects
27
    # It allows us to search only for projects user has access to
28
    attr_reader :limit_projects
29

30 31
    def initialize(current_user, limit_projects, query)
      @current_user = current_user
32
      @limit_projects = limit_projects || Project.all
33 34 35
      @query = Shellwords.shellescape(query) if query.present?
    end

Dmitriy Zaporozhets committed
36
    def objects(scope, page = nil)
37 38 39 40 41 42 43
      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)
44 45
      when 'milestones'
        milestones.page(page).per(per_page)
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
      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

63 64 65 66
    def milestones_count
      @milestones_count ||= milestones.count
    end

67 68 69 70
    def single_commit_result?
      false
    end

71 72 73
    private

    def projects
74
      limit_projects.search(query)
75 76 77
    end

    def issues
78
      issues = IssuesFinder.new(current_user).execute.where(project_id: project_ids_relation)
79

Douwe Maan committed
80 81 82 83 84 85
      issues =
        if query =~ /#(\d+)\z/
          issues.where(iid: $1)
        else
          issues.full_search(query)
        end
86

87
      issues.order('updated_at DESC')
88 89
    end

90
    def milestones
91
      milestones = Milestone.where(project_id: project_ids_relation)
92 93 94 95
      milestones = milestones.search(query)
      milestones.order('updated_at DESC')
    end

96
    def merge_requests
97
      merge_requests = MergeRequestsFinder.new(current_user).execute.in_projects(project_ids_relation)
Douwe Maan committed
98 99 100 101 102 103
      merge_requests =
        if query =~ /[#!](\d+)\z/
          merge_requests.where(iid: $1)
        else
          merge_requests.full_search(query)
        end
104
      merge_requests.order('updated_at DESC')
105 106 107 108 109 110 111 112 113
    end

    def default_scope
      'projects'
    end

    def per_page
      20
    end
114 115

    def project_ids_relation
116
      limit_projects.select(:id).reorder(nil)
117
    end
118 119
  end
end