Welcome to mirror list, hosted at ThFree Co, Russian Federation.

build_infos_finder.rb « packages « finders « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 92ad5888eb9ddd4dad9f594f8567bfcc5158b85f (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
# frozen_string_literal: true

module Packages
  class BuildInfosFinder
    MAX_PAGE_SIZE = 100

    def initialize(package, params)
      @package = package
      @params = params
    end

    def execute
      build_infos = @package.build_infos.without_empty_pipelines
      build_infos = apply_order(build_infos)
      build_infos = apply_limit(build_infos)
      apply_cursor(build_infos)
    end

    private

    def apply_order(build_infos)
      order_direction = :desc
      order_direction = :asc if last

      build_infos.order_by_pipeline_id(order_direction)
    end

    def apply_limit(build_infos)
      limit = [first, last, max_page_size, MAX_PAGE_SIZE].compact.min
      limit += 1 if support_next_page
      build_infos.limit(limit)
    end

    def apply_cursor(build_infos)
      if before
        build_infos.with_pipeline_id_greater_than(before)
      elsif after
        build_infos.with_pipeline_id_less_than(after)
      else
        build_infos
      end
    end

    def first
      @params[:first]
    end

    def last
      @params[:last]
    end

    def max_page_size
      @params[:max_page_size]
    end

    def before
      @params[:before]
    end

    def after
      @params[:after]
    end

    def support_next_page
      @params[:support_next_page]
    end
  end
end