Commit 7cced600 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Introduce latest_status and add a few tests

Feedback:
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/7333#note_20003268
parent 3ce6ba7d
...@@ -98,6 +98,10 @@ module Ci ...@@ -98,6 +98,10 @@ module Ci
end.where(id: max_id.group(:ref, :sha)) end.where(id: max_id.group(:ref, :sha))
end end
def self.latest_status(ref = nil)
latest(ref).status
end
def self.latest_successful_for(ref) def self.latest_successful_for(ref)
success.latest(ref).first success.latest(ref).first
end end
......
...@@ -230,7 +230,7 @@ class Commit ...@@ -230,7 +230,7 @@ class Commit
return @statuses[ref] if @statuses.key?(ref) return @statuses[ref] if @statuses.key?(ref)
@statuses[ref] = pipelines.latest(ref).status @statuses[ref] = pipelines.latest_status(ref)
end end
def revert_branch_name def revert_branch_name
......
...@@ -5,7 +5,7 @@ module Ci ...@@ -5,7 +5,7 @@ module Ci
sha = opts[:sha] || ref_sha(project, ref) sha = opts[:sha] || ref_sha(project, ref)
pipelines = project.pipelines.where(sha: sha) pipelines = project.pipelines.where(sha: sha)
image_name = image_for_status(pipelines.latest(ref).status) image_name = image_for_status(pipelines.latest_status(ref))
image_path = Rails.root.join('public/ci', image_name) image_path = Rails.root.join('public/ci', image_name)
OpenStruct.new(path: image_path, name: image_name) OpenStruct.new(path: image_path, name: image_name)
......
...@@ -381,6 +381,63 @@ describe Ci::Pipeline, models: true do ...@@ -381,6 +381,63 @@ describe Ci::Pipeline, models: true do
end end
end end
shared_context 'with some empty pipelines' do
before do
create_pipeline(:canceled, 'ref', 'A')
create_pipeline(:success, 'ref', 'A')
create_pipeline(:failed, 'ref', 'B')
create_pipeline(:skipped, 'feature', 'C')
end
def create_pipeline(status, ref, sha)
create(:ci_empty_pipeline, status: status, ref: ref, sha: sha)
end
end
describe '.latest' do
include_context 'with some empty pipelines'
context 'when no ref is specified' do
let(:pipelines) { Ci::Pipeline.latest.all }
it 'returns the latest pipeline for the same ref and different sha' do
expect(pipelines.map(&:sha)).to contain_exactly('A', 'B', 'C')
expect(pipelines.map(&:status)).
to contain_exactly('success', 'failed', 'skipped')
end
end
context 'when ref is specified' do
let(:pipelines) { Ci::Pipeline.latest('ref').all }
it 'returns the latest pipeline for ref and different sha' do
expect(pipelines.map(&:sha)).to contain_exactly('A', 'B')
expect(pipelines.map(&:status)).
to contain_exactly('success', 'failed')
end
end
end
describe '.latest_status' do
include_context 'with some empty pipelines'
context 'when no ref is specified' do
let(:latest_status) { Ci::Pipeline.latest_status }
it 'returns the latest status for the same ref and different sha' do
expect(latest_status).to eq(Ci::Pipeline.latest.status)
end
end
context 'when ref is specified' do
let(:latest_status) { Ci::Pipeline.latest_status('ref') }
it 'returns the latest status for ref and different sha' do
expect(latest_status).to eq(Ci::Pipeline.latest_status('ref'))
end
end
end
describe '#status' do describe '#status' do
let!(:build) { create(:ci_build, :created, pipeline: pipeline, name: 'test') } let!(:build) { create(:ci_build, :created, pipeline: pipeline, name: 'test') }
......
...@@ -224,7 +224,7 @@ eos ...@@ -224,7 +224,7 @@ eos
end end
it 'gives compound status from latest pipelines' do it 'gives compound status from latest pipelines' do
expect(commit.status).to eq(Ci::Pipeline.latest.status) expect(commit.status).to eq(Ci::Pipeline.latest_status)
end end
end end
...@@ -251,7 +251,7 @@ eos ...@@ -251,7 +251,7 @@ eos
end end
it 'gives compound status from latest pipelines if ref is nil' do it 'gives compound status from latest pipelines if ref is nil' do
expect(commit.status(nil)).to eq(Ci::Pipeline.latest.status) expect(commit.status(nil)).to eq(Ci::Pipeline.latest_status)
end end
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