From d96c24d81590dd6da237f131d4c074b70e64e030 Mon Sep 17 00:00:00 2001 From: Patrick Bajao Date: Fri, 9 Aug 2019 18:09:06 +0800 Subject: Invalidate branches cache on PostReceive Whenever `PostReceive` is enqueued, `UpdateMergeRequestsWorker` is enqueued and `MergeRequests::RefreshService` is called, it'll check if the source branch of each MR asssociated to the push exists or not via `MergeRequest#source_branch_exists?`. The said method will call `Repository#branch_exists?` which is cached in `Rails.cache`. When the cache contains outdated data and the source branch actually exists, the `MergeRequests#RefreshService` job will close associated MRs which is not correct. The fix is to expire the branches cache of the project so we have updated data during the post receive hook which will help in the accuracy of the check if we need to close associated MRs or not. --- spec/workers/post_receive_spec.rb | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'spec/workers/post_receive_spec.rb') diff --git a/spec/workers/post_receive_spec.rb b/spec/workers/post_receive_spec.rb index 39f1beb4efa..01ccae194fb 100644 --- a/spec/workers/post_receive_spec.rb +++ b/spec/workers/post_receive_spec.rb @@ -57,12 +57,19 @@ describe PostReceive do context 'with changes' do before do allow_any_instance_of(Gitlab::GitPostReceive).to receive(:identify).and_return(project.owner) + allow(Gitlab::GlRepository).to receive(:parse).and_return([project, Gitlab::GlRepository::PROJECT]) end context "branches" do - let(:changes) { "123456 789012 refs/heads/tést" } + let(:changes) { '123456 789012 refs/heads/tést' } + + it 'expires the branches cache' do + expect(project.repository).to receive(:expire_branches_cache) + + described_class.new.perform(gl_repository, key_id, base64_changes) + end - it "calls Git::BranchPushService" do + it 'calls Git::BranchPushService' do expect_next_instance_of(Git::BranchPushService) do |service| expect(service).to receive(:execute).and_return(true) end @@ -73,16 +80,22 @@ describe PostReceive do end end - context "tags" do - let(:changes) { "123456 789012 refs/tags/tag" } + context 'tags' do + let(:changes) { '123456 789012 refs/tags/tag' } - it "calls Git::TagPushService" do - expect(Git::BranchPushService).not_to receive(:execute) + it 'does not expire branches cache' do + expect(project.repository).not_to receive(:expire_branches_cache) + described_class.new.perform(gl_repository, key_id, base64_changes) + end + + it 'calls Git::TagPushService' do expect_next_instance_of(Git::TagPushService) do |service| expect(service).to receive(:execute).and_return(true) end + expect(Git::BranchPushService).not_to receive(:new) + described_class.new.perform(gl_repository, key_id, base64_changes) end end -- cgit v1.2.1