summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLin Jen-Shin <godfat@godfat.org>2016-07-07 21:03:21 +0800
committerLin Jen-Shin <godfat@godfat.org>2016-07-07 21:03:21 +0800
commit20037e61122a688366060f9427506962048e77ed (patch)
tree70d9a4fb4e060341624db895577518fe108da21e
parent3c89a788c795fba2b050a0af0d8261e302d8cded (diff)
downloadgitlab-ce-20037e61122a688366060f9427506962048e77ed.tar.gz
Introduce Project#builds_for(build_name, ref = 'HEAD'):
So that we could find the particular builds according to build_name and ref. It would be used to find the latest build artifacts from a particular branch or tag.
-rw-r--r--app/models/commit_status.rb8
-rw-r--r--app/models/project.rb8
-rw-r--r--spec/models/build_spec.rb12
3 files changed, 25 insertions, 3 deletions
diff --git a/app/models/commit_status.rb b/app/models/commit_status.rb
index e437e3417a8..6828705dbc8 100644
--- a/app/models/commit_status.rb
+++ b/app/models/commit_status.rb
@@ -16,7 +16,13 @@ class CommitStatus < ActiveRecord::Base
alias_attribute :author, :user
- scope :latest, -> { where(id: unscope(:select).select('max(id)').group(:name, :commit_id)) }
+ scope :latest, -> do
+ id = unscope(:select).
+ select("max(#{table_name}.id)").
+ group(:name, :commit_id)
+
+ where(id: id)
+ end
scope :retried, -> { where.not(id: latest) }
scope :ordered, -> { order(:name) }
scope :ignored, -> { where(allow_failure: true, status: [:failed, :canceled]) }
diff --git a/app/models/project.rb b/app/models/project.rb
index 029026a4e56..293dbd52359 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -429,6 +429,14 @@ class Project < ActiveRecord::Base
repository.commit(id)
end
+ def builds_for(build_name, ref = 'HEAD')
+ sha = commit(ref).sha
+
+ builds.joins(:pipeline).
+ merge(Ci::Pipeline.where(sha: sha)).
+ where(name: build_name)
+ end
+
def merge_base_commit(first_commit_id, second_commit_id)
sha = repository.merge_base(first_commit_id, second_commit_id)
repository.commit(sha) if sha
diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb
index e8171788872..8e3c9672fd5 100644
--- a/spec/models/build_spec.rb
+++ b/spec/models/build_spec.rb
@@ -673,7 +673,7 @@ describe Ci::Build, models: true do
context 'when build is running' do
before { build.run! }
- it 'should return false' do
+ it 'returns false' do
expect(build.retryable?).to be false
end
end
@@ -681,9 +681,17 @@ describe Ci::Build, models: true do
context 'when build is finished' do
before { build.success! }
- it 'should return true' do
+ it 'returns true' do
expect(build.retryable?).to be true
end
end
end
+
+ describe 'Project#builds_for' do
+ it 'returns builds from ref and build name' do
+ latest_build = project.builds_for(build.name, 'HEAD').latest.first
+
+ expect(latest_build.id).to eq(build.id)
+ end
+ end
end