summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-09-23 15:30:24 +0200
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-09-23 15:30:24 +0200
commit31b15e3dce2817d8cd6d70cda97c6b9dbf5c7f8a (patch)
tree704788a66dc2ba75baba94c28a7f012aa70f32f9
parent0731a7af319e4c19e98a43b7355fa26f5e8bd6f5 (diff)
downloadgitlab-ce-31b15e3dce2817d8cd6d70cda97c6b9dbf5c7f8a.tar.gz
Simplify CI projects query
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
-rw-r--r--app/controllers/ci/projects_controller.rb9
-rw-r--r--app/models/ci/project.rb1
-rw-r--r--app/views/ci/projects/_project.html.haml50
-rw-r--r--lib/ci/project_list_builder.rb21
4 files changed, 31 insertions, 50 deletions
diff --git a/app/controllers/ci/projects_controller.rb b/app/controllers/ci/projects_controller.rb
index e480c0be907..6b7545e5447 100644
--- a/app/controllers/ci/projects_controller.rb
+++ b/app/controllers/ci/projects_controller.rb
@@ -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)
diff --git a/app/models/ci/project.rb b/app/models/ci/project.rb
index 37fbcc287bb..a52e28615f7 100644
--- a/app/models/ci/project.rb
+++ b/app/models/ci/project.rb
@@ -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'
diff --git a/app/views/ci/projects/_project.html.haml b/app/views/ci/projects/_project.html.haml
index e0ea02cf1af..58022de9bc1 100644
--- a/app/views/ci/projects/_project.html.haml
+++ b/app/views/ci/projects/_project.html.haml
@@ -1,26 +1,24 @@
-- 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
diff --git a/lib/ci/project_list_builder.rb b/lib/ci/project_list_builder.rb
deleted file mode 100644
index da26f9a9f47..00000000000
--- a/lib/ci/project_list_builder.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-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