Commit 5e6a52ca authored by Kamil Trzciński's avatar Kamil Trzciński

Improve performance of `Pipeline.newest_first`

parent 2530bc0c
......@@ -194,22 +194,24 @@ module Ci
#
# ref - The name (or names) of the branch(es)/tag(s) to limit the list of
# pipelines to.
def self.newest_first(ref = nil)
# limit - This limits a backlog search, default to 100.
def self.newest_first(ref: nil, limit: 100)
relation = order(id: :desc)
ref ? relation.where(ref: ref) : relation
relation = relation.where(ref: ref) if ref
relation = relation.where(id: relation.limit(limit).select(:id)) if limit
relation
end
def self.latest_status(ref = nil)
newest_first(ref).pluck(:status).first
newest_first(ref: ref).pluck(:status).first
end
def self.latest_successful_for(ref)
newest_first(ref).success.take
newest_first(ref: ref).success.take
end
def self.latest_successful_for_refs(refs)
relation = newest_first(refs).success
relation = newest_first(ref: refs).success
relation.each_with_object({}) do |pipeline, hash|
hash[pipeline.ref] ||= pipeline
......
......@@ -22,7 +22,7 @@ module EE
# The new `reports:` syntax reports
scope :with_security_reports, -> do
where('EXISTS (?)', Ci::Build.select(1).latest.with_security_reports.where('ci_pipelines.id=ci_build.commit_id'))
where('EXISTS (?)', ::Ci::Build.latest.with_security_reports.where('ci_pipelines.id=ci_builds.commit_id').select(1))
end
# This structure describes feature levels
......
......@@ -110,8 +110,8 @@ module EE
end
def latest_pipeline_with_security_reports
pipelines.newest_first(default_branch).with_security_reports.first ||
pipelines.newest_first(default_branch).with_legacy_security_reports.first
pipelines.newest_first(ref: default_branch).with_security_reports.first ||
pipelines.newest_first(ref: default_branch).with_legacy_security_reports.first
end
def environments_for_scope(scope)
......
......@@ -1498,17 +1498,42 @@ describe Project do
describe '#latest_pipeline_with_security_reports' do
let(:project) { create(:project) }
let(:pipeline_1) { create(:ci_pipeline_without_jobs, project: project) }
let(:pipeline_2) { create(:ci_pipeline_without_jobs, project: project) }
let(:pipeline_3) { create(:ci_pipeline_without_jobs, project: project) }
let!(:pipeline_1) { create(:ci_pipeline_without_jobs, project: project) }
let!(:pipeline_2) { create(:ci_pipeline_without_jobs, project: project) }
let!(:pipeline_3) { create(:ci_pipeline_without_jobs, project: project) }
subject { project.latest_pipeline_with_security_reports }
context 'when legacy reports are used' do
before do
create(:ee_ci_build, :legacy_sast, pipeline: pipeline_1)
create(:ee_ci_build, :legacy_sast, pipeline: pipeline_2)
end
it "returns the latest pipeline with security reports" do
expect(project.latest_pipeline_with_legacy_security_reports).to eq(pipeline_2)
is_expected.to eq(pipeline_2)
end
end
context 'when new reports are used' do
before do
create(:ee_ci_build, :sast, pipeline: pipeline_1)
create(:ee_ci_build, :sast, pipeline: pipeline_2)
end
it "returns the latest pipeline with security reports" do
is_expected.to eq(pipeline_2)
end
context 'when legacy used' do
before do
create(:ee_ci_build, :legacy_sast, pipeline: pipeline_3)
end
it "prefers the new reports" do
is_expected.to eq(pipeline_2)
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