Skip to content
Snippets Groups Projects
Verified Commit a1224cc4 authored by Pedro Pombeiro's avatar Pedro Pombeiro
Browse files

Apply suggestion

parent c24ec26a
No related branches found
No related tags found
No related merge requests found
......@@ -114,16 +114,35 @@ def can_edit?
end
def last_pipeline
return unless object.builds_enabled? && pipeline = object.pipeline_status
return unless pipeline.status.present?
{
details_path: project_pipeline_path(object, pipeline),
group: pipeline.status,
icon: "status_#{pipeline.status}",
label: pipeline.status,
text: pipeline.status
}
return unless project? && can?(request.current_user, :read_build, object)
BatchLoader.for(object).batch do |projects, loader|
# rubocop: disable CodeReuse/ActiveRecord
# This will load the project_feature association for all the projects for the line after next line
ActiveRecord::Associations::Preloader.new.preload(projects, :project_feature)
builds_enabled_projects = projects.select(&:builds_enabled?)
# Prefill nil for all the projects
projects.each { |project| loader.call(project, nil) }
# This is to load last_pipeline for all the builds-enabled projects in one SQL
Ci::Pipeline # The DISTINCT select statement will work with the below ORDER statement to return the last pipeline
.select("DISTINCT ON (project_id) *")
.where(project_id: builds_enabled_projects.map(&:id)).ci_sources
.order(:project_id, id: :desc)
# `each` is used instead of `find_each` because `find_each` will overwrite the above ORDER statement that we
# need for the last pipelines query to work.
.each do |pipeline|
project = projects.find { |project| project.id == pipeline.project_id }
loader.call(project, {
details_path: project_pipeline_path(project, pipeline),
group: pipeline.status,
icon: "status_#{pipeline.status}",
label: pipeline.status,
text: pipeline.status
})
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment