summaryrefslogtreecommitdiff
path: root/app/services/projects/blame_service.rb
blob: 57b913b04e604736b63334323abdb6757a677b87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true

# Service class to correctly initialize Gitlab::Blame and Kaminari pagination
# objects
module Projects
  class BlameService
    PER_PAGE = 1000

    def initialize(blob, commit, params)
      @blob = blob
      @commit = commit
      @page = extract_page(params)
      @pagination_enabled = pagination_state(params)
    end

    attr_reader :page

    def blame
      Gitlab::Blame.new(blob, commit, range: blame_range)
    end

    def pagination
      return unless pagination_enabled

      Kaminari.paginate_array([], total_count: blob_lines_count, limit: per_page)
        .tap { |pagination| pagination.max_paginates_per(per_page) }
        .page(page)
    end

    private

    attr_reader :blob, :commit, :pagination_enabled

    def blame_range
      return unless pagination_enabled

      first_line = (page - 1) * per_page + 1
      last_line = (first_line + per_page).to_i - 1

      first_line..last_line
    end

    def extract_page(params)
      page = params.fetch(:page, 1).to_i

      return 1 if page < 1 || overlimit?(page)

      page
    end

    def per_page
      PER_PAGE
    end

    def pagination_state(params)
      return false if Gitlab::Utils.to_boolean(params[:no_pagination], default: false)

      Feature.enabled?(:blame_page_pagination, commit.project)
    end

    def overlimit?(page)
      page * per_page >= blob_lines_count + per_page
    end

    def blob_lines_count
      @blob_lines_count ||= blob.data.lines.count
    end
  end
end