Commit 7535be32 authored by Matthias Käppler's avatar Matthias Käppler

Merge branch 'mo-refactor-badge-template' into 'master'

Refactor badge template

See merge request gitlab-org/gitlab!63224
parents 9fcfe629 11017bf8
...@@ -24,26 +24,10 @@ module Gitlab::Ci ...@@ -24,26 +24,10 @@ module Gitlab::Ci
@key_width = badge.customization.dig(:key_width) @key_width = badge.customization.dig(:key_width)
end end
def key_text
if @key_text && @key_text.size <= MAX_KEY_TEXT_SIZE
@key_text
else
@entity.to_s
end
end
def value_text def value_text
@status ? ("%.2f%%" % @status) : 'unknown' @status ? ("%.2f%%" % @status) : 'unknown'
end end
def key_width
if @key_width && @key_width.between?(1, MAX_KEY_WIDTH)
@key_width
else
62
end
end
def value_width def value_width
@status ? 54 : 58 @status ? 54 : 58
end end
......
...@@ -28,26 +28,10 @@ module Gitlab::Ci ...@@ -28,26 +28,10 @@ module Gitlab::Ci
@key_width = badge.customization.dig(:key_width) @key_width = badge.customization.dig(:key_width)
end end
def key_text
if @key_text && @key_text.size <= MAX_KEY_TEXT_SIZE
@key_text
else
@entity.to_s
end
end
def value_text def value_text
STATUS_RENAME[@status.to_s] || @status.to_s STATUS_RENAME[@status.to_s] || @status.to_s
end end
def key_width
if @key_width && @key_width.between?(1, MAX_KEY_WIDTH)
@key_width
else
62
end
end
def value_width def value_width
54 54
end end
......
...@@ -8,6 +8,7 @@ module Gitlab::Ci ...@@ -8,6 +8,7 @@ module Gitlab::Ci
class Template class Template
MAX_KEY_TEXT_SIZE = 64 MAX_KEY_TEXT_SIZE = 64
MAX_KEY_WIDTH = 512 MAX_KEY_WIDTH = 512
DEFAULT_KEY_WIDTH = 62
def initialize(badge) def initialize(badge)
@entity = badge.entity @entity = badge.entity
...@@ -15,7 +16,11 @@ module Gitlab::Ci ...@@ -15,7 +16,11 @@ module Gitlab::Ci
end end
def key_text def key_text
raise NotImplementedError if @key_text && @key_text.size <= MAX_KEY_TEXT_SIZE
@key_text
else
@entity.to_s
end
end end
def value_text def value_text
...@@ -23,7 +28,11 @@ module Gitlab::Ci ...@@ -23,7 +28,11 @@ module Gitlab::Ci
end end
def key_width def key_width
raise NotImplementedError if @key_width && @key_width.between?(1, MAX_KEY_WIDTH)
@key_width
else
DEFAULT_KEY_WIDTH
end
end end
def value_width def value_width
......
...@@ -6,31 +6,7 @@ RSpec.describe Gitlab::Ci::Badge::Coverage::Template do ...@@ -6,31 +6,7 @@ RSpec.describe Gitlab::Ci::Badge::Coverage::Template do
let(:badge) { double(entity: 'coverage', status: 90.00, customization: {}) } let(:badge) { double(entity: 'coverage', status: 90.00, customization: {}) }
let(:template) { described_class.new(badge) } let(:template) { described_class.new(badge) }
describe '#key_text' do it_behaves_like 'a badge template', 'coverage'
it 'says coverage by default' do
expect(template.key_text).to eq 'coverage'
end
context 'when custom key_text is defined' do
before do
allow(badge).to receive(:customization).and_return({ key_text: "custom text" })
end
it 'returns custom value' do
expect(template.key_text).to eq "custom text"
end
context 'when its size is larger than the max allowed value' do
before do
allow(badge).to receive(:customization).and_return({ key_text: 't' * 65 })
end
it 'returns default value' do
expect(template.key_text).to eq 'coverage'
end
end
end
end
describe '#value_text' do describe '#value_text' do
context 'when coverage is known' do context 'when coverage is known' do
...@@ -60,32 +36,6 @@ RSpec.describe Gitlab::Ci::Badge::Coverage::Template do ...@@ -60,32 +36,6 @@ RSpec.describe Gitlab::Ci::Badge::Coverage::Template do
end end
end end
describe '#key_width' do
it 'is fixed by default' do
expect(template.key_width).to eq 62
end
context 'when custom key_width is defined' do
before do
allow(badge).to receive(:customization).and_return({ key_width: 101 })
end
it 'returns custom value' do
expect(template.key_width).to eq 101
end
context 'when it is larger than the max allowed value' do
before do
allow(badge).to receive(:customization).and_return({ key_width: 513 })
end
it 'returns default value' do
expect(template.key_width).to eq 62
end
end
end
end
describe '#value_width' do describe '#value_width' do
context 'when coverage is known' do context 'when coverage is known' do
it 'is narrower when coverage is known' do it 'is narrower when coverage is known' do
......
...@@ -6,31 +6,7 @@ RSpec.describe Gitlab::Ci::Badge::Pipeline::Template do ...@@ -6,31 +6,7 @@ RSpec.describe Gitlab::Ci::Badge::Pipeline::Template do
let(:badge) { double(entity: 'pipeline', status: 'success', customization: {}) } let(:badge) { double(entity: 'pipeline', status: 'success', customization: {}) }
let(:template) { described_class.new(badge) } let(:template) { described_class.new(badge) }
describe '#key_text' do it_behaves_like 'a badge template', 'pipeline'
it 'says pipeline by default' do
expect(template.key_text).to eq 'pipeline'
end
context 'when custom key_text is defined' do
before do
allow(badge).to receive(:customization).and_return({ key_text: 'custom text' })
end
it 'returns custom value' do
expect(template.key_text).to eq 'custom text'
end
context 'when its size is larger than the max allowed value' do
before do
allow(badge).to receive(:customization).and_return({ key_text: 't' * 65 })
end
it 'returns default value' do
expect(template.key_text).to eq 'pipeline'
end
end
end
end
describe '#value_text' do describe '#value_text' do
it 'is status value' do it 'is status value' do
...@@ -38,32 +14,6 @@ RSpec.describe Gitlab::Ci::Badge::Pipeline::Template do ...@@ -38,32 +14,6 @@ RSpec.describe Gitlab::Ci::Badge::Pipeline::Template do
end end
end end
describe '#key_width' do
it 'is fixed by default' do
expect(template.key_width).to eq 62
end
context 'when custom key_width is defined' do
before do
allow(badge).to receive(:customization).and_return({ key_width: 101 })
end
it 'returns custom value' do
expect(template.key_width).to eq 101
end
context 'when it is larger than the max allowed value' do
before do
allow(badge).to receive(:customization).and_return({ key_width: 513 })
end
it 'returns default value' do
expect(template.key_width).to eq 62
end
end
end
end
describe 'widths and text anchors' do describe 'widths and text anchors' do
it 'has fixed width and text anchors' do it 'has fixed width and text anchors' do
expect(template.width).to eq 116 expect(template.width).to eq 116
......
# frozen_string_literal: true
RSpec.shared_examples 'a badge template' do |badge_type|
describe '#key_text' do
it "says #{badge_type} by default" do
expect(template.key_text).to eq(badge_type)
end
context 'when custom key_text is defined' do
before do
allow(badge).to receive(:customization).and_return({ key_text: "custom text" })
end
it 'returns custom value' do
expect(template.key_text).to eq("custom text")
end
context 'when its size is larger than the max allowed value' do
before do
allow(badge).to receive(:customization).and_return({ key_text: 't' * (::Gitlab::Ci::Badge::Template::MAX_KEY_TEXT_SIZE + 1) } )
end
it 'returns default value' do
expect(template.key_text).to eq(badge_type)
end
end
end
end
describe '#key_width' do
let_it_be(:default_key_width) { ::Gitlab::Ci::Badge::Template::DEFAULT_KEY_WIDTH }
it 'is fixed by default' do
expect(template.key_width).to eq(default_key_width)
end
context 'when custom key_width is defined' do
before do
allow(badge).to receive(:customization).and_return({ key_width: 101 })
end
it 'returns custom value' do
expect(template.key_width).to eq(101)
end
context 'when it is larger than the max allowed value' do
before do
allow(badge).to receive(:customization).and_return({ key_width: ::Gitlab::Ci::Badge::Template::MAX_KEY_WIDTH + 1 })
end
it 'returns default value' do
expect(template.key_width).to eq(default_key_width)
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment