Commit af86b8c2 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Latest success pipelines (rather than builds)

parent 85409a5a
...@@ -18,6 +18,14 @@ module Ci ...@@ -18,6 +18,14 @@ module Ci
after_touch :update_state after_touch :update_state
after_save :keep_around_commits after_save :keep_around_commits
scope :latest, -> do
max_id = unscope(:select).
select("max(#{table_name}.id)").
group(:ref)
where(id: max_id)
end
def self.truncate_sha(sha) def self.truncate_sha(sha)
sha[0...8] sha[0...8]
end end
......
...@@ -18,8 +18,8 @@ class CommitStatus < ActiveRecord::Base ...@@ -18,8 +18,8 @@ class CommitStatus < ActiveRecord::Base
scope :latest, -> do scope :latest, -> do
max_id = unscope(:select). max_id = unscope(:select).
select("max(#{table_name}.id)"). select("max(#{table_name}.id)").
group(:name, :commit_id) group(:name, :commit_id)
where(id: max_id) where(id: max_id)
end end
......
...@@ -430,12 +430,9 @@ class Project < ActiveRecord::Base ...@@ -430,12 +430,9 @@ class Project < ActiveRecord::Base
end end
def latest_success_builds_for(ref = 'HEAD') def latest_success_builds_for(ref = 'HEAD')
builds_for(ref).success.latest
end
def builds_for(ref = 'HEAD')
Ci::Build.joins(:pipeline). Ci::Build.joins(:pipeline).
merge(Ci::Pipeline.where(ref: ref, project: self)) merge(pipelines.where(ref: ref).success.latest).
with_artifacts
end end
def merge_base_commit(first_commit_id, second_commit_id) def merge_base_commit(first_commit_id, second_commit_id)
......
...@@ -6,7 +6,8 @@ describe Ci::Build, models: true do ...@@ -6,7 +6,8 @@ describe Ci::Build, models: true do
let(:pipeline) do let(:pipeline) do
create(:ci_pipeline, project: project, create(:ci_pipeline, project: project,
sha: project.commit.id, sha: project.commit.id,
ref: 'fix') ref: 'fix',
status: 'success')
end end
let(:build) { create(:ci_build, pipeline: pipeline) } let(:build) { create(:ci_build, pipeline: pipeline) }
...@@ -694,20 +695,38 @@ describe Ci::Build, models: true do ...@@ -694,20 +695,38 @@ describe Ci::Build, models: true do
end end
describe 'Project#latest_success_builds_for' do describe 'Project#latest_success_builds_for' do
let(:build) do
create(:ci_build, :artifacts, :success, pipeline: pipeline)
end
before do before do
build.update(status: 'success') build
end end
it 'returns builds from ref' do context 'with succeed pipeline' do
builds = project.latest_success_builds_for('fix') it 'returns builds from ref' do
builds = project.latest_success_builds_for('fix')
expect(builds).to contain_exactly(build)
end
it 'returns empty relation if the build cannot be found' do
builds = project.latest_success_builds_for('TAIL').all
expect(builds).to contain_exactly(build) expect(builds).to be_empty
end
end end
it 'returns empty relation if the build cannot be found' do context 'with pending pipeline' do
builds = project.latest_success_builds_for('TAIL').all before do
pipeline.update(status: 'pending')
end
expect(builds).to be_empty it 'returns empty relation' do
builds = project.latest_success_builds_for('fix').all
expect(builds).to be_empty
end
end end
end end
end end
...@@ -25,7 +25,7 @@ shared_examples 'artifacts from ref with 404' do ...@@ -25,7 +25,7 @@ shared_examples 'artifacts from ref with 404' do
context 'has no such build' do context 'has no such build' do
before do before do
get path_from_ref(pipeline.sha, 'NOBUILD') get path_from_ref(pipeline.ref, 'NOBUILD')
end end
it('gives 404') { verify } it('gives 404') { verify }
...@@ -33,6 +33,11 @@ shared_examples 'artifacts from ref with 404' do ...@@ -33,6 +33,11 @@ shared_examples 'artifacts from ref with 404' do
end end
shared_examples 'artifacts from ref successfully' do shared_examples 'artifacts from ref successfully' do
def create_new_pipeline(status)
new_pipeline = create(:ci_pipeline, status: 'success')
create(:ci_build, status, :artifacts, pipeline: new_pipeline)
end
context 'with regular branch' do context 'with regular branch' do
before do before do
pipeline.update(ref: 'master', pipeline.update(ref: 'master',
...@@ -59,10 +64,10 @@ shared_examples 'artifacts from ref successfully' do ...@@ -59,10 +64,10 @@ shared_examples 'artifacts from ref successfully' do
it('gives the file') { verify } it('gives the file') { verify }
end end
context 'with latest build' do context 'with latest pipeline' do
before do before do
3.times do # creating some old builds 3.times do # creating some old pipelines
create(:ci_build, :success, :artifacts, pipeline: pipeline) create_new_pipeline(:success)
end end
end end
...@@ -73,10 +78,10 @@ shared_examples 'artifacts from ref successfully' do ...@@ -73,10 +78,10 @@ shared_examples 'artifacts from ref successfully' do
it('gives the file') { verify } it('gives the file') { verify }
end end
context 'with success build' do context 'with success pipeline' do
before do before do
build # make sure build was old, but still the latest success one build # make sure pipeline was old, but still the latest success one
create(:ci_build, :pending, :artifacts, pipeline: pipeline) create_new_pipeline(:pending)
end end
before do before do
......
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