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

daily_build_group_report_results_finder.rb « testing « ci « finders « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 70d9e55dc470bb4113c4d258ecf45f597daadead (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true

# DailyBuildGroupReportResultsFinder
#
# Used to filter DailyBuildGroupReportResults by set of params
#
# Arguments:
#   current_user
#   params:
#     project: integer
#     group: integer
#     coverage: boolean
#     ref_path: string
#     start_date: date
#     end_date: date
#     sort: boolean
#     limit: integer

module Ci
  module Testing
    class DailyBuildGroupReportResultsFinder
      include Gitlab::Allowable

      MAX_ITEMS = 1_000

      attr_reader :params, :current_user

      def initialize(params: {}, current_user: nil)
        @params = params
        @current_user = current_user
      end

      def execute
        return Ci::DailyBuildGroupReportResult.none unless query_allowed?

        collection = Ci::DailyBuildGroupReportResult.by_projects(params[:project])
        collection = filter_report_results(collection)
        collection
      end

      private

      def query_allowed?
        can?(current_user, :read_build_report_results, params[:project])
      end

      def filter_report_results(collection)
        collection = by_coverage(collection)
        collection = by_ref_path(collection)
        collection = by_dates(collection)

        collection = sort(collection)
        collection = limit_by(collection)
        collection
      end

      def by_coverage(items)
        params[:coverage].present? ? items.with_coverage : items
      end

      def by_ref_path(items)
        params[:ref_path].present? ? items.by_ref_path(params[:ref_path]) : items.with_default_branch
      end

      def by_dates(items)
        params[:start_date].present? && params[:end_date].present? ? items.by_dates(params[:start_date], params[:end_date]) : items
      end

      def sort(items)
        params[:sort].present? ? items.ordered_by_date_and_group_name : items
      end

      # rubocop: disable CodeReuse/ActiveRecord
      def limit_by(items)
        items.limit(limit)
      end
      # rubocop: enable CodeReuse/ActiveRecord

      def limit
        return MAX_ITEMS unless params[:limit].present?

        [params[:limit].to_i, MAX_ITEMS].min
      end
    end
  end
end

Ci::Testing::DailyBuildGroupReportResultsFinder.prepend_if_ee('::EE::Ci::Testing::DailyBuildGroupReportResultsFinder')