debian-mirror-gitlab/lib/gitlab/ci/badge/coverage/template.rb

79 lines
2.1 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2021-03-11 19:13:27 +05:30
module Gitlab::Ci
2016-09-13 17:45:13 +05:30
module Badge
module Coverage
##
# Class that represents a coverage badge template.
#
# Template object will be passed to badge.svg.erb template.
#
class Template < Badge::Template
STATUS_COLOR = {
good: '#4c1',
acceptable: '#a3c51c',
medium: '#dfb317',
low: '#e05d44',
unknown: '#9f9f9f'
2017-08-17 22:00:37 +05:30
}.freeze
2021-11-18 22:05:49 +05:30
COVERAGE_MAX = 100
COVERAGE_MIN = 0
MIN_GOOD_DEFAULT = 95
MIN_ACCEPTABLE_DEFAULT = 90
MIN_MEDIUM_DEFAULT = 75
2016-09-13 17:45:13 +05:30
def initialize(badge)
@status = badge.status
2021-11-18 22:05:49 +05:30
@min_good = badge.customization.dig(:min_good)
@min_acceptable = badge.customization.dig(:min_acceptable)
@min_medium = badge.customization.dig(:min_medium)
2022-07-16 23:28:13 +05:30
super
2016-09-13 17:45:13 +05:30
end
def value_text
2018-03-17 18:26:18 +05:30
@status ? ("%.2f%%" % @status) : 'unknown'
2016-09-13 17:45:13 +05:30
end
def value_width
2018-03-17 18:26:18 +05:30
@status ? 54 : 58
2016-09-13 17:45:13 +05:30
end
2021-11-18 22:05:49 +05:30
def min_good_value
if @min_good && @min_good.between?(3, COVERAGE_MAX)
@min_good
else
MIN_GOOD_DEFAULT
end
end
def min_acceptable_value
if @min_acceptable && @min_acceptable.between?(2, min_good_value - 1)
@min_acceptable
else
[MIN_ACCEPTABLE_DEFAULT, (min_good_value - 1)].min
end
end
def min_medium_value
if @min_medium && @min_medium.between?(1, min_acceptable_value - 1)
@min_medium
else
[MIN_MEDIUM_DEFAULT, (min_acceptable_value - 1)].min
end
end
2016-09-13 17:45:13 +05:30
def value_color
case @status
2021-11-18 22:05:49 +05:30
when min_good_value..COVERAGE_MAX then STATUS_COLOR[:good]
when min_acceptable_value..min_good_value then STATUS_COLOR[:acceptable]
when min_medium_value..min_acceptable_value then STATUS_COLOR[:medium]
when COVERAGE_MIN..min_medium_value then STATUS_COLOR[:low]
2016-09-13 17:45:13 +05:30
else
STATUS_COLOR[:unknown]
end
end
end
end
end
end