Still use compound pipeline status, but group by

ref and sha so that it would show latest pipeline
if ref and sha are both specified, otherwise still
the same as before.
This commit is contained in:
Lin Jen-Shin 2016-11-21 22:19:16 +08:00
parent b20c7846ff
commit 721f2d3788
4 changed files with 20 additions and 23 deletions

View File

@ -89,13 +89,23 @@ module Ci
end
end
scope :latest, -> { order(id: :desc) }
scope :latest, -> do
max_id = unscope(:select).select("max(#{quoted_table_name}.id)")
where(id: max_id.group(:ref, :sha))
end
# ref can't be HEAD or SHA, can only be branch/tag name
scope :latest_for, ->(ref) { where(ref: ref).latest }
scope :latest_for, ->(ref) do
if ref
where(ref: ref)
else
self
end.latest
end
def self.latest_successful_for(ref)
latest_for(ref).success.first
where(ref: ref).order(id: :desc).success.first
end
def self.truncate_sha(sha)

View File

@ -234,13 +234,7 @@ class Commit
return @statuses[ref] if @statuses.key?(ref)
latest_pipeline = if ref
pipelines.latest_for(ref)
else
pipelines.latest
end.first
@statuses[ref] = latest_pipeline.try(:status)
@statuses[ref] = pipelines.latest_for(ref).status
end
def revert_branch_name

View File

@ -3,18 +3,11 @@ module Ci
def execute(project, opts)
ref = opts[:ref]
sha = opts[:sha] || ref_sha(project, ref)
pipelines = project.pipelines.where(sha: sha)
latest_pipeline = if ref
pipelines.latest_for(ref)
else
pipelines.latest
end.first
image_name = image_for_status(latest_pipeline.try(:status))
image_name = image_for_status(pipelines.latest_for(ref).status)
image_path = Rails.root.join('public/ci', image_name)
OpenStruct.new(path: image_path, name: image_name)
end

View File

@ -216,8 +216,8 @@ eos
end
end
it 'gives the status from latest pipeline' do
expect(commit.status).to eq(Ci::Pipeline.latest.first.status)
it 'gives compound status' do
expect(commit.status).to eq(Ci::Pipeline.latest.status)
end
end
@ -243,8 +243,8 @@ eos
expect(commit.status('fix')).to eq(pipeline_from_fix.status)
end
it 'gives status from latest pipeline for whatever branch' do
expect(commit.status(nil)).to eq(Ci::Pipeline.latest.first.status)
it 'gives compound status if ref is nil' do
expect(commit.status(nil)).to eq(Ci::Pipeline.latest.status)
end
end
end