diff options
author | Grzegorz Bizon <grzegorz@gitlab.com> | 2016-12-23 12:28:54 +0000 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-12-23 10:50:49 -0200 |
commit | 8dd85ca8347b00229364c2f6ea842934c09057b4 (patch) | |
tree | dd69ccd5d2d700a5e8bd95ec8f4d980b2aa0a71e | |
parent | 2f0d7871c258085b910d6e47c319a38528d7a8d1 (diff) | |
download | gitlab-ce-8dd85ca8347b00229364c2f6ea842934c09057b4.tar.gz |
Merge branch 'fix-latest-pipeine-ordering' into 'master'
Fix code that selects latest pipelines
Closes #25993 and #26031
See merge request !8286
-rw-r--r-- | app/models/ci/pipeline.rb | 7 | ||||
-rw-r--r-- | app/models/project.rb | 2 | ||||
-rw-r--r-- | changelogs/unreleased/fix-latest-pipeine-ordering.yml | 4 | ||||
-rw-r--r-- | spec/models/ci/pipeline_spec.rb | 14 |
4 files changed, 13 insertions, 14 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index f2f6453b3b9..6894a5763ff 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -93,11 +93,8 @@ module Ci .select("max(#{quoted_table_name}.id)") .group(:ref, :sha) - if ref - where(id: max_id, ref: ref) - else - where(id: max_id) - end + relation = ref ? where(ref: ref) : self + relation.where(id: max_id).order(id: :desc) end def self.latest_status(ref = nil) diff --git a/app/models/project.rb b/app/models/project.rb index 26fa20f856d..72fdd4514c4 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -418,7 +418,7 @@ class Project < ActiveRecord::Base repository.commit(ref) end - # ref can't be HEAD, can only be branch/tag name or SHA + # ref can't be HEAD or SHA, can only be branch/tag name def latest_successful_builds_for(ref = default_branch) latest_pipeline = pipelines.latest_successful_for(ref) diff --git a/changelogs/unreleased/fix-latest-pipeine-ordering.yml b/changelogs/unreleased/fix-latest-pipeine-ordering.yml new file mode 100644 index 00000000000..3dbd1ba036a --- /dev/null +++ b/changelogs/unreleased/fix-latest-pipeine-ordering.yml @@ -0,0 +1,4 @@ +--- +title: Fix finding the latest pipeline +merge_request: 8286 +author: diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index dc377d15f15..b28da6daabf 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -424,20 +424,18 @@ describe Ci::Pipeline, models: true do context 'when no ref is specified' do let(:pipelines) { described_class.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') + it 'gives the latest pipelines for the same ref and different sha in reverse chronological order' do + expect(pipelines.map(&:sha)).to eq(%w[C B A]) + expect(pipelines.map(&:status)).to eq(%w[skipped failed success]) end end context 'when ref is specified' do let(:pipelines) { described_class.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') + it 'gives the latest pipelines for ref and different sha in reverse chronological order' do + expect(pipelines.map(&:sha)).to eq(%w[B A]) + expect(pipelines.map(&:status)).to eq(%w[failed success]) end end end |