From 1e7116b34d229fcefab5147d6f90b1c24513afe8 Mon Sep 17 00:00:00 2001
From: Zeger-Jan van de Weg <zegerjan@gitlab.com>
Date: Tue, 29 Mar 2016 16:57:24 +0200
Subject: [PATCH] Exclude projects pending deletion from all results

---
 CHANGELOG                                     |  1 +
 app/models/project.rb                         |  2 ++
 app/services/system_hooks_service.rb          | 22 ++++++++++---------
 app/workers/project_destroy_worker.rb         |  2 +-
 ...52_add_index_on_pending_delete_projects.rb |  6 +++++
 db/schema.rb                                  |  3 ++-
 spec/models/project_spec.rb                   |  9 ++++++++
 7 files changed, 33 insertions(+), 12 deletions(-)
 create mode 100644 db/migrate/20160329144452_add_index_on_pending_delete_projects.rb

diff --git a/CHANGELOG b/CHANGELOG
index f95a243b0a1..5f73570650b 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -10,6 +10,7 @@ v 8.7.0 (unreleased)
   - Fix raw/rendered diff producing different results on merge requests !3450
   - Add links to CI setup documentation from project settings and builds pages
   - Handle nil descriptions in Slack issue messages (Stan Hu)
+  - Add default scope to projects to exclude projects pending deletion
   - Implement 'Groups View' as an option for dashboard preferences !3379 (Elias W.)
   - Implement 'TODOs View' as an option for dashboard preferences !3379 (Elias W.)
   - Gracefully handle notes on deleted commits in merge requests (Stan Hu)
diff --git a/app/models/project.rb b/app/models/project.rb
index 2285063ab50..f208965086d 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -206,6 +206,8 @@ class Project < ActiveRecord::Base
   mount_uploader :avatar, AvatarUploader
 
   # Scopes
+  default_scope { where(pending_delete: false) }
+
   scope :sorted_by_activity, -> { reorder(last_activity_at: :desc) }
   scope :sorted_by_stars, -> { reorder('projects.star_count DESC') }
   scope :sorted_by_names, -> { joins(:namespace).reorder('namespaces.name ASC, projects.name ASC') }
diff --git a/app/services/system_hooks_service.rb b/app/services/system_hooks_service.rb
index ea2b26ccb52..f0615ec7420 100644
--- a/app/services/system_hooks_service.rb
+++ b/app/services/system_hooks_service.rb
@@ -95,17 +95,19 @@ class SystemHooksService
   end
 
   def project_member_data(model)
+    project = model.project || Project.unscoped.find(model.source_id)
+
     {
-      project_name: model.project.name,
-      project_path: model.project.path,
-      project_path_with_namespace: model.project.path_with_namespace,
-      project_id: model.project.id,
-      user_username: model.user.username,
-      user_name: model.user.name,
-      user_email: model.user.email,
-      user_id: model.user.id,
-      access_level: model.human_access,
-      project_visibility: Project.visibility_levels.key(model.project.visibility_level_field).downcase
+      project_name:                 project.name,
+      project_path:                 project.path,
+      project_path_with_namespace:  project.path_with_namespace,
+      project_id:                   project.id,
+      user_username:                model.user.username,
+      user_name:                    model.user.name,
+      user_email:                   model.user.email,
+      user_id:                      model.user.id,
+      access_level:                 model.human_access,
+      project_visibility:           Project.visibility_levels.key(project.visibility_level_field).downcase
     }
   end
 
diff --git a/app/workers/project_destroy_worker.rb b/app/workers/project_destroy_worker.rb
index d06e4480292..b51c6a266c9 100644
--- a/app/workers/project_destroy_worker.rb
+++ b/app/workers/project_destroy_worker.rb
@@ -5,7 +5,7 @@ class ProjectDestroyWorker
 
   def perform(project_id, user_id, params)
     begin
-      project = Project.find(project_id)
+      project = Project.unscoped.find(project_id)
     rescue ActiveRecord::RecordNotFound
       return
     end
diff --git a/db/migrate/20160329144452_add_index_on_pending_delete_projects.rb b/db/migrate/20160329144452_add_index_on_pending_delete_projects.rb
new file mode 100644
index 00000000000..275554e736e
--- /dev/null
+++ b/db/migrate/20160329144452_add_index_on_pending_delete_projects.rb
@@ -0,0 +1,6 @@
+class AddIndexOnPendingDeleteProjects < ActiveRecord::Migration
+  def change
+    add_index :projects, :pending_delete
+  end
+end
+
diff --git a/db/schema.rb b/db/schema.rb
index 90eaff1a4dd..e63e22ce864 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -418,7 +418,7 @@ ActiveRecord::Schema.define(version: 20160331133914) do
     t.integer  "iid"
     t.integer  "updated_by_id"
     t.integer  "moved_to_id"
-    t.boolean  "confidential",              default: false
+    t.boolean  "confidential",  default: false
     t.datetime "deleted_at"
   end
 
@@ -745,6 +745,7 @@ ActiveRecord::Schema.define(version: 20160331133914) do
   add_index "projects", ["namespace_id"], name: "index_projects_on_namespace_id", using: :btree
   add_index "projects", ["path"], name: "index_projects_on_path", using: :btree
   add_index "projects", ["path"], name: "index_projects_on_path_trigram", using: :gin, opclasses: {"path"=>"gin_trgm_ops"}
+  add_index "projects", ["pending_delete"], name: "index_projects_on_pending_delete", using: :btree
   add_index "projects", ["runners_token"], name: "index_projects_on_runners_token", using: :btree
   add_index "projects", ["star_count"], name: "index_projects_on_star_count", using: :btree
   add_index "projects", ["visibility_level"], name: "index_projects_on_visibility_level", using: :btree
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index 55f1c665b86..f29c389e094 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -104,6 +104,15 @@ describe Project, models: true do
     end
   end
 
+  describe 'default_scope' do
+    it 'excludes projects pending deletion from the results' do
+      project = create(:empty_project)
+      create(:empty_project, pending_delete: true)
+
+      expect(Project.all).to eq [project]
+    end
+  end
+
   describe 'project token' do
     it 'should set an random token if none provided' do
       project = FactoryGirl.create :empty_project, runners_token: ''
-- 
GitLab