2020-11-23 10:09:37 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
module Reports
|
|
|
|
class CodequalityReports
|
|
|
|
attr_reader :degradations, :error_message
|
|
|
|
|
2021-03-29 11:09:30 -04:00
|
|
|
SEVERITY_PRIORITIES = %w(blocker critical major minor info).map.with_index.to_h.freeze # { "blocker" => 0, "critical" => 1 ... }
|
2020-11-23 10:09:37 -05:00
|
|
|
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
|
|
|
|
|
2021-03-29 11:09:30 -04:00
|
|
|
def sort_degradations!
|
|
|
|
@degradations = @degradations.sort_by do |_fingerprint, degradation|
|
|
|
|
SEVERITY_PRIORITIES[degradation.dig(:severity)]
|
|
|
|
end.to_h
|
|
|
|
end
|
|
|
|
|
2020-11-23 10:09:37 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def valid_degradation?(degradation)
|
2021-03-23 14:09:05 -04:00
|
|
|
JSONSchemer.schema(Pathname.new(CODECLIMATE_SCHEMA_PATH)).valid?(degradation)
|
|
|
|
rescue StandardError => _
|
2020-11-23 10:09:37 -05:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|