diff options
-rw-r--r-- | app/models/ci/pipeline.rb | 16 | ||||
-rw-r--r-- | app/models/commit.rb | 8 | ||||
-rw-r--r-- | app/services/ci/image_for_build_service.rb | 11 | ||||
-rw-r--r-- | spec/models/commit_spec.rb | 8 |
4 files changed, 20 insertions, 23 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index cd7d8fd3af7..e566503bb18 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -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) diff --git a/app/models/commit.rb b/app/models/commit.rb index 2134ba2d75f..b588b93b158 100644 --- a/app/models/commit.rb +++ b/app/models/commit.rb @@ -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 diff --git a/app/services/ci/image_for_build_service.rb b/app/services/ci/image_for_build_service.rb index 026a727a8f9..d5a07ef630b 100644 --- a/app/services/ci/image_for_build_service.rb +++ b/app/services/ci/image_for_build_service.rb @@ -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 diff --git a/spec/models/commit_spec.rb b/spec/models/commit_spec.rb index ca277601970..21590cd4ff1 100644 --- a/spec/models/commit_spec.rb +++ b/spec/models/commit_spec.rb @@ -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 |