summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2016-08-11 11:16:14 +0200
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2016-08-15 14:39:46 +0200
commitf3de46e6b0d4cc61e00c884753a8c9eec66f66c4 (patch)
treec94491b355df3a161a7a805f2d7794ece07384e6
parent9f0b46c05aef9d0352bfaa5e42e34143227de8ff (diff)
downloadgitlab-ce-f3de46e6b0d4cc61e00c884753a8c9eec66f66c4.tar.gz
Refactor badge template and metadata classes
-rw-r--r--lib/gitlab/badge/base.rb4
-rw-r--r--lib/gitlab/badge/build/metadata.rb6
-rw-r--r--lib/gitlab/badge/build/status.rb11
-rw-r--r--lib/gitlab/badge/build/template.rb12
-rw-r--r--spec/lib/gitlab/badge/build/metadata_spec.rb25
-rw-r--r--spec/lib/gitlab/badge/build/status_spec.rb36
-rw-r--r--spec/lib/gitlab/badge/build/template_spec.rb22
7 files changed, 58 insertions, 58 deletions
diff --git a/lib/gitlab/badge/base.rb b/lib/gitlab/badge/base.rb
index 229e7b5aa57..909fa24fa90 100644
--- a/lib/gitlab/badge/base.rb
+++ b/lib/gitlab/badge/base.rb
@@ -1,11 +1,11 @@
module Gitlab
module Badge
class Base
- def key_text
+ def entity
raise NotImplementedError
end
- def value_text
+ def status
raise NotImplementedError
end
diff --git a/lib/gitlab/badge/build/metadata.rb b/lib/gitlab/badge/build/metadata.rb
index fbe10b948c4..52a10b19298 100644
--- a/lib/gitlab/badge/build/metadata.rb
+++ b/lib/gitlab/badge/build/metadata.rb
@@ -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
diff --git a/lib/gitlab/badge/build/status.rb b/lib/gitlab/badge/build/status.rb
index a72e284d513..50aa45e5406 100644
--- a/lib/gitlab/badge/build/status.rb
+++ b/lib/gitlab/badge/build/status.rb
@@ -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
diff --git a/lib/gitlab/badge/build/template.rb b/lib/gitlab/badge/build/template.rb
index 779569d0cd7..f52589ff736 100644
--- a/lib/gitlab/badge/build/template.rb
+++ b/lib/gitlab/badge/build/template.rb
@@ -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
diff --git a/spec/lib/gitlab/badge/build/metadata_spec.rb b/spec/lib/gitlab/badge/build/metadata_spec.rb
index ad5388215c2..4044a82d16c 100644
--- a/spec/lib/gitlab/badge/build/metadata_spec.rb
+++ b/spec/lib/gitlab/badge/build/metadata_spec.rb
@@ -1,16 +1,15 @@
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
diff --git a/spec/lib/gitlab/badge/build/status_spec.rb b/spec/lib/gitlab/badge/build/status_spec.rb
index fa5bc068918..38eebb2a176 100644
--- a/spec/lib/gitlab/badge/build/status_spec.rb
+++ b/spec/lib/gitlab/badge/build/status_spec.rb
@@ -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)
diff --git a/spec/lib/gitlab/badge/build/template_spec.rb b/spec/lib/gitlab/badge/build/template_spec.rb
index 86dead3c54e..a7e21fb8bb1 100644
--- a/spec/lib/gitlab/badge/build/template_spec.rb
+++ b/spec/lib/gitlab/badge/build/template_spec.rb
@@ -1,8 +1,8 @@
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'