Commit f51a8eac authored by Sean McGivern's avatar Sean McGivern Committed by Jose Ivan Vargas

Merge branch 'sidebar-cache-updates' into 'master'

Only update the sidebar count caches when needed

See merge request !13878
parent 531e89f2
...@@ -269,6 +269,16 @@ class Issue < ActiveRecord::Base ...@@ -269,6 +269,16 @@ class Issue < ActiveRecord::Base
end end
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 private
# Returns `true` if the given User can read the current Issue. # Returns `true` if the given User can read the current Issue.
......
...@@ -941,4 +941,27 @@ class MergeRequest < ActiveRecord::Base ...@@ -941,4 +941,27 @@ class MergeRequest < ActiveRecord::Base
true true
end 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 end
---
title: Only update the sidebar count caches when needed
merge_request:
author:
type: other
...@@ -755,4 +755,40 @@ describe Issue do ...@@ -755,4 +755,40 @@ describe Issue do
end end
end 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 end
...@@ -1694,4 +1694,25 @@ describe MergeRequest do ...@@ -1694,4 +1694,25 @@ describe MergeRequest do
expect(subject.ref_fetched?).to be_falsey expect(subject.ref_fetched?).to be_falsey
end end
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 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