Class: Gitlab::Ci::Badge::Coverage::Template

Inherits:
Template
  • Object
show all
Defined in:
lib/gitlab/ci/badge/coverage/template.rb

Overview

Class that represents a coverage badge template.

Template object will be passed to badge.svg.erb template.

Constant Summary collapse

STATUS_COLOR =
{
  good: '#4c1',
  acceptable: '#a3c51c',
  medium: '#dfb317',
  low: '#e05d44',
  unknown: '#9f9f9f'
}.freeze
COVERAGE_MAX =
100
COVERAGE_MIN =
0
MIN_GOOD_DEFAULT =
95
MIN_ACCEPTABLE_DEFAULT =
90
MIN_MEDIUM_DEFAULT =
75

Constants inherited from Template

Template::DEFAULT_KEY_WIDTH, Template::MAX_KEY_TEXT_SIZE, Template::MAX_KEY_WIDTH

Instance Method Summary collapse

Methods inherited from Template

#key_color, #key_text, #key_text_anchor, #key_width, #value_text_anchor, #width

Constructor Details

#initialize(badge) ⇒ Template

Returns a new instance of Template.



25
26
27
28
29
30
31
# File 'lib/gitlab/ci/badge/coverage/template.rb', line 25

def initialize(badge)
  @status = badge.status
  @min_good = badge.customization.dig(:min_good)
  @min_acceptable = badge.customization.dig(:min_acceptable)
  @min_medium = badge.customization.dig(:min_medium)
  super
end

Instance Method Details

#min_acceptable_valueObject



49
50
51
52
53
54
55
# File 'lib/gitlab/ci/badge/coverage/template.rb', line 49

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

#min_good_valueObject



41
42
43
44
45
46
47
# File 'lib/gitlab/ci/badge/coverage/template.rb', line 41

def min_good_value
  if @min_good && @min_good.between?(3, COVERAGE_MAX)
    @min_good
  else
    MIN_GOOD_DEFAULT
  end
end

#min_medium_valueObject



57
58
59
60
61
62
63
# File 'lib/gitlab/ci/badge/coverage/template.rb', line 57

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

#value_colorObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/gitlab/ci/badge/coverage/template.rb', line 65

def value_color
  case @status
  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]
  else
    STATUS_COLOR[:unknown]
  end
end

#value_textObject



33
34
35
# File 'lib/gitlab/ci/badge/coverage/template.rb', line 33

def value_text
  @status ? ("%.2f%%" % @status) : 'unknown'
end

#value_widthObject



37
38
39
# File 'lib/gitlab/ci/badge/coverage/template.rb', line 37

def value_width
  @status ? 54 : 58
end