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

post_schema_validator.rb « validator « dashboard « metrics « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 73bfc5a6294143ea9741f11ce74ada33865d61e9 (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
# frozen_string_literal: true

module Gitlab
  module Metrics
    module Dashboard
      module Validator
        class PostSchemaValidator
          def initialize(metric_ids:, project: nil, dashboard_path: nil)
            @metric_ids = metric_ids
            @project = project
            @dashboard_path = dashboard_path
          end

          def validate
            errors = []
            errors << uniq_metric_ids
            errors.compact
          end

          private

          attr_reader :project, :metric_ids, :dashboard_path

          def uniq_metric_ids
            return Validator::Errors::DuplicateMetricIds.new if metric_ids.uniq!

            uniq_metric_ids_across_project if project.present? || dashboard_path.present?
          end

          # rubocop: disable CodeReuse/ActiveRecord
          def uniq_metric_ids_across_project
            return ArgumentError.new(_('Both project and dashboard_path are required')) unless
              dashboard_path.present? && project.present?

            # If PrometheusMetric identifier is not unique across project and dashboard_path,
            # we need to error because we don't know if the user is trying to create a new metric
            # or update an existing one.
            identifier_on_other_dashboard = PrometheusMetric.where(
              project: project,
              identifier: metric_ids
            ).where.not(
              dashboard_path: dashboard_path
            ).exists?

            Validator::Errors::DuplicateMetricIds.new if identifier_on_other_dashboard
          end
          # rubocop: enable CodeReuse/ActiveRecord
        end
      end
    end
  end
end