debian-mirror-gitlab/lib/gitlab/ci/reports/codequality_reports.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

61 lines
1.8 KiB
Ruby
Raw Normal View History

2021-02-22 17:27:13 +05:30
# frozen_string_literal: true
module Gitlab
module Ci
module Reports
class CodequalityReports
attr_reader :degradations, :error_message
2022-04-04 11:22:00 +05:30
SEVERITY_PRIORITIES = %w(blocker critical major minor info unknown).map.with_index.to_h.freeze # { "blocker" => 0, "critical" => 1 ... }
2021-02-22 17:27:13 +05:30
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-04-29 21:17:54 +05:30
def sort_degradations!
@degradations = @degradations.sort_by do |_fingerprint, degradation|
2022-04-04 11:22:00 +05:30
severity = degradation.dig(:severity)&.downcase
SEVERITY_PRIORITIES[severity] || SEVERITY_PRIORITIES['unknown']
2021-04-29 21:17:54 +05:30
end.to_h
end
2021-02-22 17:27:13 +05:30
def valid_degradation?(degradation)
2021-04-29 21:17:54 +05:30
JSONSchemer.schema(Pathname.new(CODECLIMATE_SCHEMA_PATH)).valid?(degradation)
rescue StandardError => _
2021-02-22 17:27:13 +05:30
false
end
2023-04-23 21:23:45 +05:30
def code_quality_report_summary
report_degradations = @degradations.presence
return if report_degradations.nil?
summary = ::Gitlab::Ci::Reports::CodequalityReports::SEVERITY_PRIORITIES.keys.index_with(0)
report_degradations.each_value do |degradation|
summary[degradation[:severity]] += 1
end
summary['count'] = summary.values.sum
summary
end
2021-02-22 17:27:13 +05:30
end
end
end
end