Commit 0c4fa861 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Calculate build status only in build badge class

parent 9ae1ecf8
......@@ -5,8 +5,16 @@ module Gitlab
#
class Build
def initialize(project, ref)
@project, @ref = project, ref
@image = ::Ci::ImageForBuildService.new.execute(project, ref: ref)
@project = project
@ref = ref
end
def status
sha = @project.commit(@ref).try(:sha)
@project.pipelines
.where(sha: sha, ref: @ref)
.status || 'unknown'
end
def metadata
......@@ -18,11 +26,9 @@ module Gitlab
end
def data
File.read(@image[:path])
end
def to_s
@image[:name].sub(/\.svg$/, '')
File.read(
Rails.root.join('public/ci', 'build-' + status + '.svg')
)
end
end
end
......
......@@ -24,9 +24,10 @@ describe Gitlab::Badge::Build do
context 'build success' do
before { build.success! }
describe '#to_s' do
subject { badge.to_s }
it { is_expected.to eq 'build-success' }
describe '#status' do
it 'is successful' do
expect(badge.status).to eq 'success'
end
end
describe '#data' do
......@@ -41,9 +42,10 @@ describe Gitlab::Badge::Build do
context 'build failed' do
before { build.drop! }
describe '#to_s' do
subject { badge.to_s }
it { is_expected.to eq 'build-failed' }
describe '#status' do
it 'failed' do
expect(badge.status).to eq 'failed'
end
end
describe '#data' do
......@@ -57,9 +59,10 @@ describe Gitlab::Badge::Build do
end
context 'build does not exist' do
describe '#to_s' do
subject { badge.to_s }
it { is_expected.to eq 'build-unknown' }
describe '#status' do
it 'is unknown' do
expect(badge.status).to eq 'unknown'
end
end
describe '#data' do
......@@ -81,7 +84,7 @@ describe Gitlab::Badge::Build do
end
it 'does not take outdated pipeline into account' do
expect(badge.to_s).to eq 'build-success'
expect(badge.status).to eq 'success'
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