summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzegorz@gitlab.com>2016-12-23 12:28:54 +0000
committerMarin Jankovski <marin@gitlab.com>2016-12-27 16:48:10 +0000
commitbdcdf454be9e26c5e72104b810869aa67e0baf28 (patch)
tree06534512fa63efd9094d5bf6ad4dde83d7706f10
parent2974330726eb5473309899c952b059fa46062ff3 (diff)
downloadgitlab-ce-bdcdf454be9e26c5e72104b810869aa67e0baf28.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.rb7
-rw-r--r--app/models/project.rb2
-rw-r--r--changelogs/unreleased/fix-latest-pipeine-ordering.yml4
-rw-r--r--spec/models/ci/pipeline_spec.rb14
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