Commit 9f70abf1 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Avoid mixing builds from different pipelines:

So we no longer join anything, just find the latest pipeline
and load builds from there to avoid mixing builds.

Thanks Kamil for the help and tests.
parent a9f3f3c8
...@@ -15,9 +15,6 @@ module Ci ...@@ -15,9 +15,6 @@ module Ci
scope :with_artifacts, ->() { where.not(artifacts_file: nil) } scope :with_artifacts, ->() { where.not(artifacts_file: nil) }
scope :with_expired_artifacts, ->() { with_artifacts.where('artifacts_expire_at < ?', Time.now) } scope :with_expired_artifacts, ->() { with_artifacts.where('artifacts_expire_at < ?', Time.now) }
scope :last_month, ->() { where('created_at > ?', Date.today - 1.month) } scope :last_month, ->() { where('created_at > ?', Date.today - 1.month) }
scope :latest_successful_with_artifacts, ->() do
with_artifacts.success.order(id: :desc)
end
scope :manual_actions, ->() { where(when: :manual) } scope :manual_actions, ->() { where(when: :manual) }
mount_uploader :artifacts_file, ArtifactUploader mount_uploader :artifacts_file, ArtifactUploader
......
...@@ -22,7 +22,7 @@ module Ci ...@@ -22,7 +22,7 @@ module Ci
# ref can't be HEAD or SHA, can only be branch/tag name # ref can't be HEAD or SHA, can only be branch/tag name
scope :latest_successful_for, ->(ref = default_branch) do scope :latest_successful_for, ->(ref = default_branch) do
where(ref: ref).success.order(id: :desc) where(ref: ref).success.order(id: :desc).limit(1)
end end
def self.truncate_sha(sha) def self.truncate_sha(sha)
......
...@@ -431,8 +431,13 @@ class Project < ActiveRecord::Base ...@@ -431,8 +431,13 @@ class Project < ActiveRecord::Base
# ref can't be HEAD, can only be branch/tag name or SHA # ref can't be HEAD, can only be branch/tag name or SHA
def latest_successful_builds_for(ref = default_branch) def latest_successful_builds_for(ref = default_branch)
builds.joins(:pipeline).merge(pipelines.latest_successful_for(ref)). latest_successful_pipeline = pipelines.latest_successful_for(ref).first
latest_successful_with_artifacts
if latest_successful_pipeline
latest_successful_pipeline.builds.with_artifacts.latest
else
builds.none
end
end end
def merge_base_commit(first_commit_id, second_commit_id) def merge_base_commit(first_commit_id, second_commit_id)
......
...@@ -1148,34 +1148,43 @@ describe Project, models: true do ...@@ -1148,34 +1148,43 @@ describe Project, models: true do
end end
end end
context 'with multiple pipelines and builds' do context 'with many builds' do
shared_examples 'latest successful one' do before do
@pipeline1 = create_pipeline
@pipeline2 = create_pipeline
@build1_p2 = create_build(@pipeline2, 'test')
@build1_p1 = create_build(@pipeline1, 'test')
@build2_p1 = create_build(@pipeline1, 'test2')
@build2_p2 = create_build(@pipeline2, 'test2')
end
it 'gives the latest build from latest pipeline' do it 'gives the latest build from latest pipeline' do
latest_build = project.latest_successful_builds_for.first latest_builds = project.latest_successful_builds_for
expect(latest_build).to eq(build) expect(latest_builds).to contain_exactly(@build2_p2, @build1_p2)
end
end end
context 'with all succeeded pipeline' do def create_pipeline
before do
old_pipelines = Array.new(3).map do
create(:ci_pipeline, project: project, create(:ci_pipeline, project: project,
sha: project.commit.sha, sha: project.commit.sha,
ref: project.default_branch, ref: project.default_branch,
status: 'success') status: 'success')
end end
# should not give this old build for the latest pipeline def create_build(pipe, name = 'test')
create(:ci_build, :success, :artifacts, pipeline: pipeline) create(:ci_build, :success, :artifacts,
build pipeline: pipe,
name: name)
old_pipelines.reverse_each do |pipe|
create(:ci_build, :success, :artifacts, pipeline: pipe)
end end
end end
it_behaves_like 'latest successful one' context 'with multiple pipelines and builds' do
shared_examples 'latest successful one' do
it 'gives the latest build from latest pipeline' do
latest_build = project.latest_successful_builds_for.first
expect(latest_build).to eq(build)
end
end end
context 'with some pending pipeline' do context 'with some pending pipeline' 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