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