summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/reports/codequality_reports.rb
blob: 060a1e2399b9435424691339b602ad000c7095b5 (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
# frozen_string_literal: true

module Gitlab
  module Ci
    module Reports
      class CodequalityReports
        attr_reader :degradations, :error_message

        CODECLIMATE_SCHEMA_PATH = Rails.root.join('app', 'validators', 'json_schemas', 'codeclimate.json').to_s

        def initialize
          @degradations = {}.with_indifferent_access
          @error_message = nil
        end

        def add_degradation(degradation)
          valid_degradation?(degradation) && @degradations[degradation.dig('fingerprint')] = degradation
        end

        def set_error_message(error)
          @error_message = error
        end

        def degradations_count
          @degradations.size
        end

        def all_degradations
          @degradations.values
        end

        private

        def valid_degradation?(degradation)
          JSON::Validator.validate!(CODECLIMATE_SCHEMA_PATH, degradation)
        rescue JSON::Schema::ValidationError => e
          set_error_message("Invalid degradation format: #{e.message}")
          false
        end
      end
    end
  end
end