summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean McGivern <sean@mcgivern.me.uk>2017-08-31 14:30:03 +0000
committerJose Ivan Vargas <jvargas@gitlab.com>2017-09-08 12:37:01 -0500
commitf51a8eac4e1b0533666b3fdd2cb60ac974c3e913 (patch)
treebfa1263f675d133a3e0cf206e0042eca72d49958
parent531e89f25a2791928e826a560498eb05598efb0a (diff)
downloadgitlab-ce-f51a8eac4e1b0533666b3fdd2cb60ac974c3e913.tar.gz
Merge branch 'sidebar-cache-updates' into 'master'
Only update the sidebar count caches when needed See merge request !13878
-rw-r--r--app/models/issue.rb10
-rw-r--r--app/models/merge_request.rb23
-rw-r--r--changelogs/unreleased/sidebar-cache-updates.yml5
-rw-r--r--spec/models/issue_spec.rb36
-rw-r--r--spec/models/merge_request_spec.rb21
5 files changed, 95 insertions, 0 deletions
diff --git a/app/models/issue.rb b/app/models/issue.rb
index 1c948c8957e..30dc62c6c40 100644
--- a/app/models/issue.rb
+++ b/app/models/issue.rb
@@ -269,6 +269,16 @@ class Issue < ActiveRecord::Base
end
end
+ def update_project_counter_caches?
+ state_changed? || confidential_changed?
+ end
+
+ def update_project_counter_caches
+ return unless update_project_counter_caches?
+
+ Projects::OpenIssuesCountService.new(project).refresh_cache
+ end
+
private
# Returns `true` if the given User can read the current Issue.
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index 9fc526ec3ef..4dd9d5124b5 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -941,4 +941,27 @@ class MergeRequest < ActiveRecord::Base
true
end
+
+ def update_project_counter_caches?
+ state_changed?
+ end
+
+ def update_project_counter_caches
+ return unless update_project_counter_caches?
+
+ Projects::OpenMergeRequestsCountService.new(target_project).refresh_cache
+ end
+
+ private
+
+ def write_ref
+ target_project.repository.with_repo_branch_commit(
+ source_project.repository, source_branch) do |commit|
+ if commit
+ target_project.repository.write_ref(ref_path, commit.sha)
+ else
+ raise Rugged::ReferenceError, 'source repository is empty'
+ end
+ end
+ end
end
diff --git a/changelogs/unreleased/sidebar-cache-updates.yml b/changelogs/unreleased/sidebar-cache-updates.yml
new file mode 100644
index 00000000000..aebe53ba5b2
--- /dev/null
+++ b/changelogs/unreleased/sidebar-cache-updates.yml
@@ -0,0 +1,5 @@
+---
+title: Only update the sidebar count caches when needed
+merge_request:
+author:
+type: other
diff --git a/spec/models/issue_spec.rb b/spec/models/issue_spec.rb
index c055863d298..940d614bdcb 100644
--- a/spec/models/issue_spec.rb
+++ b/spec/models/issue_spec.rb
@@ -755,4 +755,40 @@ describe Issue do
end
end
end
+
+ describe 'removing an issue' do
+ it 'refreshes the number of open issues of the project' do
+ project = subject.project
+
+ expect { subject.destroy }
+ .to change { project.open_issues_count }.from(1).to(0)
+ end
+ end
+
+ describe '.public_only' do
+ it 'only returns public issues' do
+ public_issue = create(:issue)
+ create(:issue, confidential: true)
+
+ expect(described_class.public_only).to eq([public_issue])
+ end
+ end
+
+ describe '#update_project_counter_caches?' do
+ it 'returns true when the state changes' do
+ subject.state = 'closed'
+
+ expect(subject.update_project_counter_caches?).to eq(true)
+ end
+
+ it 'returns true when the confidential flag changes' do
+ subject.confidential = true
+
+ expect(subject.update_project_counter_caches?).to eq(true)
+ end
+
+ it 'returns false when the state or confidential flag did not change' do
+ expect(subject.update_project_counter_caches?).to eq(false)
+ end
+ end
end
diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb
index 5baa7c81ecc..7edceba2bda 100644
--- a/spec/models/merge_request_spec.rb
+++ b/spec/models/merge_request_spec.rb
@@ -1694,4 +1694,25 @@ describe MergeRequest do
expect(subject.ref_fetched?).to be_falsey
end
end
+
+ describe 'removing a merge request' do
+ it 'refreshes the number of open merge requests of the target project' do
+ project = subject.target_project
+
+ expect { subject.destroy }
+ .to change { project.open_merge_requests_count }.from(1).to(0)
+ end
+ end
+
+ describe '#update_project_counter_caches?' do
+ it 'returns true when the state changes' do
+ subject.state = 'closed'
+
+ expect(subject.update_project_counter_caches?).to eq(true)
+ end
+
+ it 'returns false when the state did not change' do
+ expect(subject.update_project_counter_caches?).to eq(false)
+ end
+ end
end