Commit f3de46e6 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Refactor badge template and metadata classes

parent 9f0b46c0
module Gitlab
module Badge
class Base
def key_text
def entity
raise NotImplementedError
end
def value_text
def status
raise NotImplementedError
end
......
......@@ -9,9 +9,9 @@ module Gitlab
include ActionView::Helpers::AssetTagHelper
include ActionView::Helpers::UrlHelper
def initialize(project, ref)
@project = project
@ref = ref
def initialize(badge)
@project = badge.project
@ref = badge.ref
end
def to_html
......
......@@ -5,14 +5,19 @@ module Gitlab
# Build status badge
#
class Status < Badge::Base
delegate :key_text, :value_text, to: :template
attr_reader :project, :ref
def initialize(project, ref)
@project = project
@ref = ref
@sha = @project.commit(@ref).try(:sha)
end
def entity
'build'
end
def status
@project.pipelines
.where(sha: @sha, ref: @ref)
......@@ -20,11 +25,11 @@ module Gitlab
end
def metadata
@metadata ||= Build::Metadata.new(@project, @ref)
@metadata ||= Build::Metadata.new(self)
end
def template
@template ||= Build::Template.new(status)
@template ||= Build::Template.new(self)
end
end
end
......
......@@ -17,16 +17,17 @@ module Gitlab
unknown: '#9f9f9f'
}
def initialize(status)
@status = status
def initialize(badge)
@entity = badge.entity
@status = badge.status
end
def key_text
'build'
@entity.to_s
end
def value_text
@status
@status.to_s
end
def key_width
......@@ -42,8 +43,7 @@ module Gitlab
end
def value_color
STATUS_COLOR[@status.to_sym] ||
STATUS_COLOR[:unknown]
STATUS_COLOR[@status.to_sym] || STATUS_COLOR[:unknown]
end
def key_text_anchor
......
require 'spec_helper'
describe Gitlab::Badge::Build::Metadata do
let(:project) { create(:project) }
let(:branch) { 'master' }
let(:badge) { described_class.new(project, branch) }
let(:badge) { double(project: create(:project), ref: 'feature') }
let(:metadata) { described_class.new(badge) }
describe '#to_html' do
let(:html) { Nokogiri::HTML.parse(badge.to_html) }
let(:html) { Nokogiri::HTML.parse(metadata.to_html) }
let(:a_href) { html.at('a') }
it 'points to link' do
expect(a_href[:href]).to eq badge.link_url
expect(a_href[:href]).to eq metadata.link_url
end
it 'contains clickable image' do
......@@ -19,19 +18,21 @@ describe Gitlab::Badge::Build::Metadata do
end
describe '#to_markdown' do
subject { badge.to_markdown }
subject { metadata.to_markdown }
it { is_expected.to include badge.image_url }
it { is_expected.to include badge.link_url }
it { is_expected.to include metadata.image_url }
it { is_expected.to include metadata.link_url }
end
describe '#image_url' do
subject { badge.image_url }
it { is_expected.to include "badges/#{branch}/build.svg" }
it 'returns valid url' do
expect(metadata.image_url).to include 'badges/feature/build.svg'
end
end
describe '#link_url' do
subject { badge.link_url }
it { is_expected.to include "commits/#{branch}" }
it 'returns valid link' do
expect(metadata.link_url).to include 'commits/feature'
end
end
end
......@@ -6,6 +6,18 @@ describe Gitlab::Badge::Build::Status do
let(:branch) { 'master' }
let(:badge) { described_class.new(project, branch) }
describe '#entity' do
it 'always says build' do
expect(badge.entity).to eq 'build'
end
end
describe '#template' do
it 'returns badge template' do
expect(badge.template.key_text).to eq 'build'
end
end
describe '#metadata' do
it 'returns badge metadata' do
expect(badge.metadata.image_url)
......@@ -13,12 +25,6 @@ describe Gitlab::Badge::Build::Status do
end
end
describe '#key_text' do
it 'always says build' do
expect(badge.key_text).to eq 'build'
end
end
context 'build exists' do
let!(:build) { create_build(project, sha, branch) }
......@@ -30,12 +36,6 @@ describe Gitlab::Badge::Build::Status do
expect(badge.status).to eq 'success'
end
end
describe '#value_text' do
it 'returns correct value text' do
expect(badge.value_text).to eq 'success'
end
end
end
context 'build failed' do
......@@ -46,12 +46,6 @@ describe Gitlab::Badge::Build::Status do
expect(badge.status).to eq 'failed'
end
end
describe '#value_text' do
it 'has correct value text' do
expect(badge.value_text).to eq 'failed'
end
end
end
context 'when outdated pipeline for given ref exists' do
......@@ -87,12 +81,6 @@ describe Gitlab::Badge::Build::Status do
expect(badge.status).to eq 'unknown'
end
end
describe '#value_text' do
it 'has correct value text' do
expect(badge.value_text).to eq 'unknown'
end
end
end
def create_build(project, sha, branch)
......
require 'spec_helper'
describe Gitlab::Badge::Build::Template do
let(:status) { 'success' }
let(:template) { described_class.new(status) }
let(:badge) { double(entity: 'build', status: 'success') }
let(:template) { described_class.new(badge) }
describe '#key_text' do
it 'is always says build' do
......@@ -34,15 +34,15 @@ describe Gitlab::Badge::Build::Template do
describe '#value_color' do
context 'when status is success' do
let(:status) { 'success' }
it 'has expected color' do
expect(template.value_color).to eq '#4c1'
end
end
context 'when status is failed' do
let(:status) { 'failed' }
before do
allow(badge).to receive(:status).and_return('failed')
end
it 'has expected color' do
expect(template.value_color).to eq '#e05d44'
......@@ -50,7 +50,9 @@ describe Gitlab::Badge::Build::Template do
end
context 'when status is running' do
let(:status) { 'running' }
before do
allow(badge).to receive(:status).and_return('running')
end
it 'has expected color' do
expect(template.value_color).to eq '#dfb317'
......@@ -58,7 +60,9 @@ describe Gitlab::Badge::Build::Template do
end
context 'when status is unknown' do
let(:status) { 'unknown' }
before do
allow(badge).to receive(:status).and_return('unknown')
end
it 'has expected color' do
expect(template.value_color).to eq '#9f9f9f'
......@@ -66,7 +70,9 @@ describe Gitlab::Badge::Build::Template do
end
context 'when status does not match any known statuses' do
let(:status) { 'invalid status' }
before do
allow(badge).to receive(:status).and_return('invalid')
end
it 'has expected color' do
expect(template.value_color).to eq '#9f9f9f'
......
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