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 ...@@ -16,12 +16,15 @@ module Ci
end end
def index def index
@projects = Ci::Project.all
if current_user if current_user
@projects = ProjectListBuilder.new.execute(current_user, params[:search]) @projects = @projects.where(gitlab_id: current_user.authorized_projects.pluck(:id))
@projects = @projects.page(params[:page]).per(40)
@total_count = @projects.size
end end
@projects = @projects.includes(:last_commit).order('ci_commits.created_at DESC')
@projects = @projects.page(params[:page]).per(40)
respond_to do |format| respond_to do |format|
format.json do format.json do
pager_json("ci/projects/index", @total_count) pager_json("ci/projects/index", @total_count)
......
...@@ -41,6 +41,7 @@ module Ci ...@@ -41,6 +41,7 @@ module Ci
has_many :events, dependent: :destroy, class_name: 'Ci::Event' has_many :events, dependent: :destroy, class_name: 'Ci::Event'
has_many :variables, dependent: :destroy, class_name: 'Ci::Variable' has_many :variables, dependent: :destroy, class_name: 'Ci::Variable'
has_many :triggers, dependent: :destroy, class_name: 'Ci::Trigger' 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 # Project services
has_many :services, dependent: :destroy, class_name: 'Ci::Service' has_many :services, dependent: :destroy, class_name: 'Ci::Service'
......
- if project.gitlab_ci_project - last_commit = project.last_commit
- ci_project = project.gitlab_ci_project %tr
- last_commit = ci_project.last_commit
%tr
%td %td
= link_to [:ci, ci_project] do = link_to [:ci, project] do
= ci_project.name = project.name
%td %td
- if last_commit - if last_commit
= ci_status_with_icon(last_commit.status) = ci_status_with_icon(last_commit.status)
= commit_link(last_commit) = commit_link(last_commit)
&middot; &middot;
- if ci_project.last_commit_date - if project.last_commit_date
= time_ago_in_words ci_project.last_commit_date = time_ago_in_words project.last_commit_date
ago ago
- else - else
No builds yet No builds yet
%td %td
- if ci_project.public - if project.public
%i.fa.fa-globe %i.fa.fa-globe
Public Public
- else - else
%i.fa.fa-lock %i.fa.fa-lock
Private Private
%td %td
= ci_project.commits.count = 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