Commit 721f2d37 authored by Lin Jen-Shin's avatar Lin Jen-Shin

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.
parent b20c7846
...@@ -89,13 +89,23 @@ module Ci ...@@ -89,13 +89,23 @@ module Ci
end end
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 # 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) def self.latest_successful_for(ref)
latest_for(ref).success.first where(ref: ref).order(id: :desc).success.first
end end
def self.truncate_sha(sha) def self.truncate_sha(sha)
......
...@@ -234,13 +234,7 @@ class Commit ...@@ -234,13 +234,7 @@ class Commit
return @statuses[ref] if @statuses.key?(ref) return @statuses[ref] if @statuses.key?(ref)
latest_pipeline = if ref @statuses[ref] = pipelines.latest_for(ref).status
pipelines.latest_for(ref)
else
pipelines.latest
end.first
@statuses[ref] = latest_pipeline.try(:status)
end end
def revert_branch_name def revert_branch_name
......
...@@ -3,18 +3,11 @@ module Ci ...@@ -3,18 +3,11 @@ module Ci
def execute(project, opts) def execute(project, opts)
ref = opts[:ref] ref = opts[:ref]
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)
latest_pipeline = if ref image_name = image_for_status(pipelines.latest_for(ref).status)
pipelines.latest_for(ref)
else
pipelines.latest
end.first
image_name = image_for_status(latest_pipeline.try(:status))
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)
end end
......
...@@ -216,8 +216,8 @@ eos ...@@ -216,8 +216,8 @@ eos
end end
end end
it 'gives the status from latest pipeline' do it 'gives compound status' do
expect(commit.status).to eq(Ci::Pipeline.latest.first.status) expect(commit.status).to eq(Ci::Pipeline.latest.status)
end end
end end
...@@ -243,8 +243,8 @@ eos ...@@ -243,8 +243,8 @@ eos
expect(commit.status('fix')).to eq(pipeline_from_fix.status) expect(commit.status('fix')).to eq(pipeline_from_fix.status)
end end
it 'gives status from latest pipeline for whatever branch' do it 'gives compound status if ref is nil' do
expect(commit.status(nil)).to eq(Ci::Pipeline.latest.first.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