diff options
author | Grzegorz Bizon <grzegorz@gitlab.com> | 2016-12-23 12:28:54 +0000 |
---|---|---|
committer | Grzegorz Bizon <grzegorz@gitlab.com> | 2016-12-23 12:28:54 +0000 |
commit | 5c0f25410cb2b94642d03361565372ceb5e14c00 (patch) | |
tree | 358099a4b0bae1ba9460f271d5d99027fbaa6137 | |
parent | 0b95f765656442f389fd4d5f45131be6608d0829 (diff) | |
parent | 332b85d2df466118b62b6599bd18036d691d04ed (diff) | |
download | gitlab-ce-5c0f25410cb2b94642d03361565372ceb5e14c00.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 |