Commit f3de46e6 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Refactor badge template and metadata classes

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