diff options
author | Felipe Artur <felipefac@gmail.com> | 2018-07-30 21:52:42 +0300 |
---|---|---|
committer | Felipe Artur <felipefac@gmail.com> | 2018-08-06 17:58:22 +0300 |
commit | fdb5f285f924e653400f7bbe18d13718c469d74f (patch) | |
tree | 3570730544d21c8d463aed792e9adcd686ea436a /spec | |
parent | cb2e07309b4e61501a44c3568155bdb73252338f (diff) |
Retrieve merge request closing issues from database cache
Diffstat (limited to 'spec')
-rw-r--r-- | spec/factories/merge_requests.rb | 4 | ||||
-rw-r--r-- | spec/lib/gitlab/import_export/all_models.yml | 1 | ||||
-rw-r--r-- | spec/models/merge_request_spec.rb | 72 | ||||
-rw-r--r-- | spec/presenters/merge_request_presenter_spec.rb | 2 | ||||
-rw-r--r-- | spec/requests/api/merge_requests_spec.rb | 1 | ||||
-rw-r--r-- | spec/services/issues/reopen_service_spec.rb | 2 | ||||
-rw-r--r-- | spec/services/merge_requests/merge_service_spec.rb | 1 | ||||
-rw-r--r-- | spec/services/merge_requests/post_merge_service_spec.rb | 2 | ||||
-rw-r--r-- | spec/services/merge_requests/reopen_service_spec.rb | 6 |
9 files changed, 85 insertions, 6 deletions
diff --git a/spec/factories/merge_requests.rb b/spec/factories/merge_requests.rb index f722bb9cb0d..f3bea17385b 100644 --- a/spec/factories/merge_requests.rb +++ b/spec/factories/merge_requests.rb @@ -100,6 +100,10 @@ FactoryBot.define do end end + after(:create) do |merge_request, evaluator| + merge_request.cache_merge_request_closes_issues! + end + factory :merged_merge_request, traits: [:merged] factory :closed_merge_request, traits: [:closed] factory :reopened_merge_request, traits: [:opened] diff --git a/spec/lib/gitlab/import_export/all_models.yml b/spec/lib/gitlab/import_export/all_models.yml index db5aab0cd76..375a93b28a6 100644 --- a/spec/lib/gitlab/import_export/all_models.yml +++ b/spec/lib/gitlab/import_export/all_models.yml @@ -87,6 +87,7 @@ merge_requests: - merge_request_diff - events - merge_requests_closing_issues +- cached_closes_issues - metrics - timelogs - head_pipeline diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb index b0d9d03bf6c..5904f575f74 100644 --- a/spec/models/merge_request_spec.rb +++ b/spec/models/merge_request_spec.rb @@ -310,6 +310,51 @@ describe MergeRequest do end end + describe '#visible_closing_issues_for' do + let(:guest) { create(:user) } + let(:developer) { create(:user) } + let(:issue_1) { create(:issue, project: subject.source_project) } + let(:issue_2) { create(:issue, project: subject.source_project) } + let(:confidential_issue) { create(:issue, :confidential, project: subject.source_project) } + + before do + subject.project.add_developer(subject.author) + subject.target_branch = subject.project.default_branch + commit = double('commit1', safe_message: "Fixes #{issue_1.to_reference} #{issue_2.to_reference} #{confidential_issue.to_reference}") + allow(subject).to receive(:commits).and_return([commit]) + end + + it 'shows only allowed issues to guest' do + subject.project.add_guest(guest) + + subject.cache_merge_request_closes_issues! + + expect(subject.visible_closing_issues_for(guest)).to match_array([issue_1, issue_2]) + end + + it 'shows only allowed issues to developer' do + subject.project.add_developer(developer) + + subject.cache_merge_request_closes_issues! + + expect(subject.visible_closing_issues_for(developer)).to match_array([issue_1, confidential_issue, issue_2]) + end + + context 'when external issue tracker is enabled' do + before do + subject.project.has_external_issue_tracker = true + subject.project.save! + end + + it 'calls non #closes_issues to retrieve data' do + expect(subject).to receive(:closes_issues) + expect(subject).not_to receive(:cached_closes_issues) + + subject.visible_closing_issues_for + end + end + end + describe '#cache_merge_request_closes_issues!' do before do subject.project.add_developer(subject.author) @@ -324,6 +369,25 @@ describe MergeRequest do expect { subject.cache_merge_request_closes_issues!(subject.author) }.to change(subject.merge_requests_closing_issues, :count).by(1) end + it 'does not cache closed issues when merge request is closed' do + issue = create :issue, project: subject.project + commit = double('commit1', safe_message: "Fixes #{issue.to_reference}") + + allow(subject).to receive(:commits).and_return([commit]) + allow(subject).to receive(:state).and_return("closed") + + expect { subject.cache_merge_request_closes_issues!(subject.author) }.not_to change(subject.merge_requests_closing_issues, :count) + end + + it 'does not cache closed issues when merge request is merged' do + issue = create :issue, project: subject.project + commit = double('commit1', safe_message: "Fixes #{issue.to_reference}") + allow(subject).to receive(:commits).and_return([commit]) + allow(subject).to receive(:state).and_return("merged") + + expect { subject.cache_merge_request_closes_issues!(subject.author) }.not_to change(subject.merge_requests_closing_issues, :count) + end + context 'when both internal and external issue trackers are enabled' do before do subject.project.has_external_issue_tracker = true @@ -632,6 +696,7 @@ describe MergeRequest do allow(subject).to receive(:commits).and_return([commit]) allow(subject.project).to receive(:default_branch) .and_return(subject.target_branch) + subject.cache_merge_request_closes_issues! expect(subject.issues_mentioned_but_not_closing(subject.author)).to match_array([mentioned_issue]) end @@ -649,6 +714,8 @@ describe MergeRequest do end it 'detects issues mentioned in description but not closed' do + subject.cache_merge_request_closes_issues! + expect(subject.issues_mentioned_but_not_closing(subject.author).map(&:to_s)).to match_array(['TEST-2']) end end @@ -779,9 +846,8 @@ describe MergeRequest do subject.project.add_developer(subject.author) subject.description = "This issue Closes #{issue.to_reference}" - - allow(subject.project).to receive(:default_branch) - .and_return(subject.target_branch) + allow(subject.project).to receive(:default_branch).and_return(subject.target_branch) + subject.cache_merge_request_closes_issues! expect(subject.merge_commit_message) .to match("Closes #{issue.to_reference}") diff --git a/spec/presenters/merge_request_presenter_spec.rb b/spec/presenters/merge_request_presenter_spec.rb index 46ba6f442f5..a1b52d8692d 100644 --- a/spec/presenters/merge_request_presenter_spec.rb +++ b/spec/presenters/merge_request_presenter_spec.rb @@ -117,9 +117,9 @@ describe MergeRequestPresenter do before do project.add_developer(user) - allow(resource.project).to receive(:default_branch) .and_return(resource.target_branch) + resource.cache_merge_request_closes_issues! end describe '#closing_issues_links' do diff --git a/spec/requests/api/merge_requests_spec.rb b/spec/requests/api/merge_requests_spec.rb index 1716d182782..4de834bf93a 100644 --- a/spec/requests/api/merge_requests_spec.rb +++ b/spec/requests/api/merge_requests_spec.rb @@ -954,6 +954,7 @@ describe API::MergeRequests do issue = create(:issue, project: project) mr = merge_request.tap do |mr| mr.update_attribute(:description, "Closes #{issue.to_reference(mr.project)}") + mr.cache_merge_request_closes_issues! end get api("/projects/#{project.id}/merge_requests/#{mr.iid}/closes_issues", user) diff --git a/spec/services/issues/reopen_service_spec.rb b/spec/services/issues/reopen_service_spec.rb index 3b617d4ca54..2a56075419b 100644 --- a/spec/services/issues/reopen_service_spec.rb +++ b/spec/services/issues/reopen_service_spec.rb @@ -20,7 +20,7 @@ describe Issues::ReopenService do end end - context 'when user is authrized to reopen issue' do + context 'when user is authorized to reopen issue' do let(:user) { create(:user) } before do diff --git a/spec/services/merge_requests/merge_service_spec.rb b/spec/services/merge_requests/merge_service_spec.rb index 9dd235f6660..5d96b5ce27c 100644 --- a/spec/services/merge_requests/merge_service_spec.rb +++ b/spec/services/merge_requests/merge_service_spec.rb @@ -49,6 +49,7 @@ describe MergeRequests::MergeService do issue = create :issue, project: project commit = double('commit', safe_message: "Fixes #{issue.to_reference}") allow(merge_request).to receive(:commits).and_return([commit]) + merge_request.cache_merge_request_closes_issues! service.execute(merge_request) diff --git a/spec/services/merge_requests/post_merge_service_spec.rb b/spec/services/merge_requests/post_merge_service_spec.rb index ba2b062875b..5ad6f5528f9 100644 --- a/spec/services/merge_requests/post_merge_service_spec.rb +++ b/spec/services/merge_requests/post_merge_service_spec.rb @@ -53,7 +53,7 @@ describe MergeRequests::PostMergeService do allow(project).to receive(:default_branch).and_return('foo') issue = create(:issue, project: project) - allow(merge_request).to receive(:closes_issues).and_return([issue]) + allow(merge_request).to receive(:visible_closing_issues_for).and_return([issue]) allow_any_instance_of(Issues::CloseService).to receive(:execute).with(issue, commit: merge_request).and_raise expect { described_class.new(project, user, {}).execute(merge_request) }.to raise_error diff --git a/spec/services/merge_requests/reopen_service_spec.rb b/spec/services/merge_requests/reopen_service_spec.rb index e10eaa95da4..21e71509ed6 100644 --- a/spec/services/merge_requests/reopen_service_spec.rb +++ b/spec/services/merge_requests/reopen_service_spec.rb @@ -47,6 +47,12 @@ describe MergeRequests::ReopenService do end end + it 'caches merge request closing issues' do + expect(merge_request).to receive(:cache_merge_request_closes_issues!) + + described_class.new(project, user, {}).execute(merge_request) + end + it 'updates metrics' do metrics = merge_request.metrics service = double(MergeRequestMetricsService) |