Commit 31b15e3d authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Simplify CI projects query

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 0731a7af
......@@ -16,12 +16,15 @@ module Ci
end
def index
@projects = Ci::Project.all
if current_user
@projects = ProjectListBuilder.new.execute(current_user, params[:search])
@projects = @projects.page(params[:page]).per(40)
@total_count = @projects.size
@projects = @projects.where(gitlab_id: current_user.authorized_projects.pluck(:id))
end
@projects = @projects.includes(:last_commit).order('ci_commits.created_at DESC')
@projects = @projects.page(params[:page]).per(40)
respond_to do |format|
format.json do
pager_json("ci/projects/index", @total_count)
......
......@@ -41,6 +41,7 @@ module Ci
has_many :events, dependent: :destroy, class_name: 'Ci::Event'
has_many :variables, dependent: :destroy, class_name: 'Ci::Variable'
has_many :triggers, dependent: :destroy, class_name: 'Ci::Trigger'
has_one :last_commit, -> { order 'ci_commits.created_at DESC' }, class_name: 'Ci::Commit'
# Project services
has_many :services, dependent: :destroy, class_name: 'Ci::Service'
......
- if project.gitlab_ci_project
- ci_project = project.gitlab_ci_project
- last_commit = ci_project.last_commit
%tr
%td
= link_to [:ci, ci_project] do
= ci_project.name
%td
- if last_commit
= ci_status_with_icon(last_commit.status)
= commit_link(last_commit)
&middot;
- if ci_project.last_commit_date
= time_ago_in_words ci_project.last_commit_date
ago
- else
No builds yet
%td
- if ci_project.public
%i.fa.fa-globe
Public
- else
%i.fa.fa-lock
Private
%td
= ci_project.commits.count
- last_commit = project.last_commit
%tr
%td
= link_to [:ci, project] do
= project.name
%td
- if last_commit
= ci_status_with_icon(last_commit.status)
= commit_link(last_commit)
&middot;
- if project.last_commit_date
= time_ago_in_words project.last_commit_date
ago
- else
No builds yet
%td
- if project.public
%i.fa.fa-globe
Public
- else
%i.fa.fa-lock
Private
%td
= project.commits.count
module Ci
class ProjectListBuilder
def execute(current_user, search = nil)
projects = current_user.authorized_projects
projects = projects.search(search) if search
projects.
joins("LEFT JOIN ci_projects ON projects.id = ci_projects.gitlab_id
LEFT JOIN #{last_commit_subquery} AS last_commit ON #{Ci::Project.table_name}.id = last_commit.project_id").
reorder("ci_projects.id is NULL ASC,
CASE WHEN last_commit.committed_at IS NULL THEN 1 ELSE 0 END,
last_commit.committed_at DESC")
end
private
def last_commit_subquery
"(SELECT project_id, MAX(committed_at) committed_at FROM #{Ci::Commit.table_name} GROUP BY project_id)"
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