Commit 20037e61 authored by Lin Jen-Shin's avatar Lin Jen-Shin

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.
parent 3c89a788
...@@ -16,7 +16,13 @@ class CommitStatus < ActiveRecord::Base ...@@ -16,7 +16,13 @@ class CommitStatus < ActiveRecord::Base
alias_attribute :author, :user 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 :retried, -> { where.not(id: latest) }
scope :ordered, -> { order(:name) } scope :ordered, -> { order(:name) }
scope :ignored, -> { where(allow_failure: true, status: [:failed, :canceled]) } scope :ignored, -> { where(allow_failure: true, status: [:failed, :canceled]) }
......
...@@ -429,6 +429,14 @@ class Project < ActiveRecord::Base ...@@ -429,6 +429,14 @@ class Project < ActiveRecord::Base
repository.commit(id) repository.commit(id)
end 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) def merge_base_commit(first_commit_id, second_commit_id)
sha = repository.merge_base(first_commit_id, second_commit_id) sha = repository.merge_base(first_commit_id, second_commit_id)
repository.commit(sha) if sha repository.commit(sha) if sha
......
...@@ -673,7 +673,7 @@ describe Ci::Build, models: true do ...@@ -673,7 +673,7 @@ describe Ci::Build, models: true do
context 'when build is running' do context 'when build is running' do
before { build.run! } before { build.run! }
it 'should return false' do it 'returns false' do
expect(build.retryable?).to be false expect(build.retryable?).to be false
end end
end end
...@@ -681,9 +681,17 @@ describe Ci::Build, models: true do ...@@ -681,9 +681,17 @@ describe Ci::Build, models: true do
context 'when build is finished' do context 'when build is finished' do
before { build.success! } before { build.success! }
it 'should return true' do it 'returns true' do
expect(build.retryable?).to be true expect(build.retryable?).to be true
end end
end 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 end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment