summaryrefslogtreecommitdiff
path: root/app/finders/branches_finder.rb
blob: a62d47071d49721f60887f29242af05679cd485f (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
# frozen_string_literal: true

class BranchesFinder < GitRefsFinder
  def initialize(repository, params = {})
    super(repository, params)
  end

  def execute(gitaly_pagination: false)
    if gitaly_pagination && names.blank? && search.blank?
      repository.branches_sorted_by(sort, pagination_params)
    else
      branches = repository.branches_sorted_by(sort)
      branches = by_search(branches)
      by_names(branches)
    end
  end

  def total
    repository.branch_count
  end

  private

  def names
    @params[:names].presence
  end

  def per_page
    @params[:per_page].presence
  end

  def page_token
    "#{Gitlab::Git::BRANCH_REF_PREFIX}#{@params[:page_token]}" if @params[:page_token]
  end

  def pagination_params
    { limit: per_page, page_token: page_token }
  end

  def by_names(branches)
    return branches unless names

    branch_names = names.to_set
    branches.select do |branch|
      branch_names.include?(branch.name)
    end
  end
end