Skip to content

Commit 42496ec

Browse files
committed
Render coverage badge for latest successful pipeline
1 parent ac73de5 commit 42496ec

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

lib/gitlab/badge/coverage/report.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ def initialize(project, ref, job = nil)
1313
@job = job
1414

1515
@pipeline = @project.pipelines
16-
.where(ref: @ref)
17-
.where(sha: @project.commit(@ref).try(:sha))
16+
.latest_successful_for(@ref)
1817
.first
1918
end
2019

spec/lib/gitlab/badge/coverage/report_spec.rb

+40-27
Original file line numberDiff line numberDiff line change
@@ -44,50 +44,63 @@
4444
end
4545
end
4646

47-
context 'pipeline exists' do
48-
let!(:pipeline) do
49-
create(:ci_pipeline, project: project,
50-
sha: project.commit.id,
51-
ref: 'master')
52-
end
47+
context 'when latest successful pipeline exists' do
48+
before do
49+
create_pipeline do |pipeline|
50+
create(:ci_build, :success, pipeline: pipeline, name: 'first', coverage: 40)
51+
create(:ci_build, :success, pipeline: pipeline, coverage: 60)
52+
end
5353

54-
context 'builds exist' do
55-
before do
56-
create(:ci_build, name: 'first', pipeline: pipeline, coverage: 40)
57-
create(:ci_build, pipeline: pipeline, coverage: 60)
54+
create_pipeline do |pipeline|
55+
create(:ci_build, :failed, pipeline: pipeline, coverage: 10)
5856
end
57+
end
5958

60-
context 'particular job specified' do
61-
let(:job_name) { 'first' }
59+
context 'when particular job specified' do
60+
let(:job_name) { 'first' }
6261

63-
it 'returns coverage for the particular job' do
64-
expect(badge.status).to eq 40
65-
end
62+
it 'returns coverage for the particular job' do
63+
expect(badge.status).to eq 40
6664
end
65+
end
6766

68-
context 'particular job not specified' do
69-
let(:job_name) { '' }
67+
context 'when particular job not specified' do
68+
let(:job_name) { '' }
69+
70+
it 'returns arithemetic mean for the pipeline' do
71+
expect(badge.status).to eq 50
72+
end
73+
end
74+
end
7075

71-
it 'returns arithemetic mean for the pipeline' do
72-
expect(badge.status).to eq 50
73-
end
76+
context 'when only failed pipeline exists' do
77+
before do
78+
create_pipeline do |pipeline|
79+
create(:ci_build, :failed, pipeline: pipeline, coverage: 10)
7480
end
7581
end
7682

77-
context 'builds do not exist' do
78-
it_behaves_like 'unknown coverage report'
83+
it_behaves_like 'unknown coverage report'
7984

80-
context 'particular job specified' do
81-
let(:job_name) { 'nonexistent' }
85+
context 'particular job specified' do
86+
let(:job_name) { 'nonexistent' }
8287

83-
it 'retruns nil' do
84-
expect(badge.status).to be_nil
85-
end
88+
it 'retruns nil' do
89+
expect(badge.status).to be_nil
8690
end
8791
end
8892
end
8993

9094
context 'pipeline does not exist' do
9195
it_behaves_like 'unknown coverage report'
9296
end
97+
98+
def create_pipeline
99+
opts = { project: project, sha: project.commit.id, ref: 'master' }
100+
101+
create(:ci_pipeline, opts).tap do |pipeline|
102+
yield pipeline
103+
pipeline.build_updated
104+
end
105+
end
93106
end

0 commit comments

Comments
 (0)