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
32
33
# File 'lib/gitlab/ci/badge/coverage/template.rb', line 25

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

Instance Method Details

#min_acceptable_valueObject


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

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


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

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

#min_medium_valueObject


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

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


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

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


35
36
37
# File 'lib/gitlab/ci/badge/coverage/template.rb', line 35

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

#value_widthObject


39
40
41
# File 'lib/gitlab/ci/badge/coverage/template.rb', line 39

def value_width
  @status ? 54 : 58
end