Skip to content

Commit 7cced60

Browse files
committed
Introduce latest_status and add a few tests
Feedback: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/7333#note_20003268
1 parent 3ce6ba7 commit 7cced60

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed

app/models/ci/pipeline.rb

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ class Pipeline < ActiveRecord::Base
9898
end.where(id: max_id.group(:ref, :sha))
9999
end
100100

101+
def self.latest_status(ref = nil)
102+
latest(ref).status
103+
end
104+
101105
def self.latest_successful_for(ref)
102106
success.latest(ref).first
103107
end

app/models/commit.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def status(ref = nil)
230230

231231
return @statuses[ref] if @statuses.key?(ref)
232232

233-
@statuses[ref] = pipelines.latest(ref).status
233+
@statuses[ref] = pipelines.latest_status(ref)
234234
end
235235

236236
def revert_branch_name
@@ -266,7 +266,7 @@ def merged_merge_request(current_user)
266266
@merged_merge_request_hash ||= Hash.new do |hash, user|
267267
hash[user] = merged_merge_request_no_cache(user)
268268
end
269-
269+
270270
@merged_merge_request_hash[current_user]
271271
end
272272

app/services/ci/image_for_build_service.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def execute(project, opts)
55
sha = opts[:sha] || ref_sha(project, ref)
66
pipelines = project.pipelines.where(sha: sha)
77

8-
image_name = image_for_status(pipelines.latest(ref).status)
8+
image_name = image_for_status(pipelines.latest_status(ref))
99
image_path = Rails.root.join('public/ci', image_name)
1010

1111
OpenStruct.new(path: image_path, name: image_name)

spec/models/ci/pipeline_spec.rb

+57
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,63 @@ def create_build(name, queued_at = current, started_from = 0)
381381
end
382382
end
383383

384+
shared_context 'with some empty pipelines' do
385+
before do
386+
create_pipeline(:canceled, 'ref', 'A')
387+
create_pipeline(:success, 'ref', 'A')
388+
create_pipeline(:failed, 'ref', 'B')
389+
create_pipeline(:skipped, 'feature', 'C')
390+
end
391+
392+
def create_pipeline(status, ref, sha)
393+
create(:ci_empty_pipeline, status: status, ref: ref, sha: sha)
394+
end
395+
end
396+
397+
describe '.latest' do
398+
include_context 'with some empty pipelines'
399+
400+
context 'when no ref is specified' do
401+
let(:pipelines) { Ci::Pipeline.latest.all }
402+
403+
it 'returns the latest pipeline for the same ref and different sha' do
404+
expect(pipelines.map(&:sha)).to contain_exactly('A', 'B', 'C')
405+
expect(pipelines.map(&:status)).
406+
to contain_exactly('success', 'failed', 'skipped')
407+
end
408+
end
409+
410+
context 'when ref is specified' do
411+
let(:pipelines) { Ci::Pipeline.latest('ref').all }
412+
413+
it 'returns the latest pipeline for ref and different sha' do
414+
expect(pipelines.map(&:sha)).to contain_exactly('A', 'B')
415+
expect(pipelines.map(&:status)).
416+
to contain_exactly('success', 'failed')
417+
end
418+
end
419+
end
420+
421+
describe '.latest_status' do
422+
include_context 'with some empty pipelines'
423+
424+
context 'when no ref is specified' do
425+
let(:latest_status) { Ci::Pipeline.latest_status }
426+
427+
it 'returns the latest status for the same ref and different sha' do
428+
expect(latest_status).to eq(Ci::Pipeline.latest.status)
429+
end
430+
end
431+
432+
context 'when ref is specified' do
433+
let(:latest_status) { Ci::Pipeline.latest_status('ref') }
434+
435+
it 'returns the latest status for ref and different sha' do
436+
expect(latest_status).to eq(Ci::Pipeline.latest_status('ref'))
437+
end
438+
end
439+
end
440+
384441
describe '#status' do
385442
let!(:build) { create(:ci_build, :created, pipeline: pipeline, name: 'test') }
386443

spec/models/commit_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@
224224
end
225225

226226
it 'gives compound status from latest pipelines' do
227-
expect(commit.status).to eq(Ci::Pipeline.latest.status)
227+
expect(commit.status).to eq(Ci::Pipeline.latest_status)
228228
end
229229
end
230230

@@ -251,7 +251,7 @@
251251
end
252252

253253
it 'gives compound status from latest pipelines if ref is nil' do
254-
expect(commit.status(nil)).to eq(Ci::Pipeline.latest.status)
254+
expect(commit.status(nil)).to eq(Ci::Pipeline.latest_status)
255255
end
256256
end
257257
end

0 commit comments

Comments
 (0)