diff options
author | Sean McGivern <sean@gitlab.com> | 2018-08-16 11:07:43 +0100 |
---|---|---|
committer | Sean McGivern <sean@gitlab.com> | 2018-08-21 12:40:44 +0100 |
commit | c73da6c1e73f04ece18b5fca5ccd67bf918682f8 (patch) | |
tree | 40343fc8215ec0496d1aec604c394bbaac511510 /spec | |
parent | 6ac7162395a2651b992cf8c25436e20fde92252d (diff) | |
download | gitlab-ce-c73da6c1e73f04ece18b5fca5ccd67bf918682f8.tar.gz |
Move Issue#{referenced,closed_by}_merge_requests to service
These methods don't really need to be on the Issue model. Issue#related_branches
can also be moved to a service, but we can do that in a separate commit.
This commit does not change any behaviour; it just moves code around, renames
the service, and refactors the specs.
Diffstat (limited to 'spec')
4 files changed, 81 insertions, 129 deletions
diff --git a/spec/models/issue_spec.rb b/spec/models/issue_spec.rb index 84edfc3ff00..c21d85fb2a4 100644 --- a/spec/models/issue_spec.rb +++ b/spec/models/issue_spec.rb @@ -188,98 +188,6 @@ describe Issue do end end - describe '#closed_by_merge_requests' do - let(:project) { create(:project, :repository) } - let(:issue) { create(:issue, project: project)} - let(:closed_issue) { build(:issue, :closed, project: project)} - - let(:mr) do - opts = { - title: 'Awesome merge_request', - description: "Fixes #{issue.to_reference}", - source_branch: 'feature', - target_branch: 'master' - } - MergeRequests::CreateService.new(project, project.owner, opts).execute - end - - let(:closed_mr) do - opts = { - title: 'Awesome merge_request 2', - description: "Fixes #{issue.to_reference}", - source_branch: 'feature', - target_branch: 'master', - state: 'closed' - } - MergeRequests::CreateService.new(project, project.owner, opts).execute - end - - it 'returns the merge request to close this issue' do - expect(issue.closed_by_merge_requests(mr.author)).to eq([mr]) - end - - it "returns an empty array when the merge request is closed already" do - expect(issue.closed_by_merge_requests(closed_mr.author)).to eq([]) - end - - it "returns an empty array when the current issue is closed already" do - expect(closed_issue.closed_by_merge_requests(closed_issue.author)).to eq([]) - end - end - - describe '#referenced_merge_requests' do - let(:project) { create(:project, :public) } - let(:issue) do - create(:issue, description: merge_request.to_reference, project: project) - end - let!(:merge_request) do - create(:merge_request, - source_project: project, - source_branch: 'master', - target_branch: 'feature') - end - - it 'returns the referenced merge requests' do - mr2 = create(:merge_request, - source_project: project, - source_branch: 'feature', - target_branch: 'master') - - create(:note_on_issue, - noteable: issue, - note: mr2.to_reference, - project_id: project.id) - - expect(issue.referenced_merge_requests).to eq([merge_request, mr2]) - end - - it 'returns cross project referenced merge requests' do - other_project = create(:project, :public) - cross_project_merge_request = create(:merge_request, source_project: other_project) - create(:note_on_issue, - noteable: issue, - note: cross_project_merge_request.to_reference(issue.project), - project_id: issue.project.id) - - expect(issue.referenced_merge_requests).to eq([merge_request, cross_project_merge_request]) - end - - it 'excludes cross project references if the user cannot read cross project' do - user = create(:user) - allow(Ability).to receive(:allowed?).and_call_original - expect(Ability).to receive(:allowed?).with(user, :read_cross_project) { false } - - other_project = create(:project, :public) - cross_project_merge_request = create(:merge_request, source_project: other_project) - create(:note_on_issue, - noteable: issue, - note: cross_project_merge_request.to_reference(issue.project), - project_id: issue.project.id) - - expect(issue.referenced_merge_requests(user)).to eq([merge_request]) - end - end - describe '#can_move?' do let(:user) { create(:user) } let(:issue) { create(:issue) } @@ -365,7 +273,12 @@ describe Issue do source_project: subject.project, source_branch: "#{subject.iid}-branch" }) merge_request.create_cross_references!(user) - expect(subject.referenced_merge_requests(user)).not_to be_empty + + referenced_merge_requests = Issues::ReferencedMergeRequestsService + .new(subject.project, user) + .referenced_merge_requests(subject) + + expect(referenced_merge_requests).not_to be_empty expect(subject.related_branches(user)).to eq([subject.to_branch_name]) end diff --git a/spec/services/issues/fetch_referenced_merge_requests_service_spec.rb b/spec/services/issues/fetch_referenced_merge_requests_service_spec.rb deleted file mode 100644 index 4e58179f45f..00000000000 --- a/spec/services/issues/fetch_referenced_merge_requests_service_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -require 'spec_helper.rb' - -describe Issues::FetchReferencedMergeRequestsService do - let(:project) { create(:project) } - let(:issue) { create(:issue, project: project) } - let(:other_project) { create(:project) } - - let(:mr) { create(:merge_request, source_project: project, target_project: project, id: 2)} - let(:other_mr) { create(:merge_request, source_project: other_project, target_project: other_project, id: 1)} - - let(:user) { create(:user) } - let(:service) { described_class.new(project, user) } - - context 'with mentioned merge requests' do - it 'returns a list of sorted merge requests' do - allow(issue).to receive(:referenced_merge_requests).with(user).and_return([other_mr, mr]) - - mrs, closed_by_mrs = service.execute(issue) - - expect(mrs).to match_array([mr, other_mr]) - expect(closed_by_mrs).to match_array([]) - end - end - - context 'with closed-by merge requests' do - it 'returns a list of sorted merge requests' do - allow(issue).to receive(:closed_by_merge_requests).with(user).and_return([other_mr, mr]) - - mrs, closed_by_mrs = service.execute(issue) - - expect(mrs).to match_array([]) - expect(closed_by_mrs).to match_array([mr, other_mr]) - end - end -end diff --git a/spec/services/issues/referenced_merge_requests_service_spec.rb b/spec/services/issues/referenced_merge_requests_service_spec.rb new file mode 100644 index 00000000000..a10893e502f --- /dev/null +++ b/spec/services/issues/referenced_merge_requests_service_spec.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'spec_helper.rb' + +describe Issues::ReferencedMergeRequestsService do + def create_referencing_mr(attributes = {}) + create(:merge_request, attributes).tap do |merge_request| + create(:note, :system, project: project, noteable: issue, note: merge_request.to_reference(full: true)) + end + end + + def create_closing_mr(attributes = {}) + create_referencing_mr(attributes).tap do |merge_request| + create(:merge_requests_closing_issues, issue: issue, merge_request: merge_request) + end + end + + set(:user) { create(:user) } + set(:project) { create(:project, :public, :repository) } + set(:other_project) { create(:project, :public, :repository) } + set(:issue) { create(:issue, author: user, project: project) } + + set(:closing_mr) { create_closing_mr(source_project: project) } + set(:closing_mr_other_project) { create_closing_mr(source_project: other_project) } + + set(:referencing_mr) { create_referencing_mr(source_project: project, source_branch: 'csv') } + set(:referencing_mr_other_project) { create_referencing_mr(source_project: other_project, source_branch: 'csv') } + + let(:service) { described_class.new(project, user) } + + describe '#execute' do + it 'returns a list of sorted merge requests' do + mrs, closed_by_mrs = service.execute(issue) + + expect(mrs).to eq([closing_mr, referencing_mr, closing_mr_other_project, referencing_mr_other_project]) + expect(closed_by_mrs).to eq([closing_mr, closing_mr_other_project]) + end + end + + describe '#referenced_merge_requests' do + it 'returns the referenced merge requests' do + expect(service.referenced_merge_requests(issue)).to match_array([ + closing_mr, + closing_mr_other_project, + referencing_mr, + referencing_mr_other_project + ]) + end + + it 'excludes cross project references if the user cannot read cross project' do + allow(Ability).to receive(:allowed?).and_call_original + expect(Ability).to receive(:allowed?).with(user, :read_cross_project).at_least(:once).and_return(false) + + expect(service.referenced_merge_requests(issue)).not_to include(closing_mr_other_project) + expect(service.referenced_merge_requests(issue)).not_to include(referencing_mr_other_project) + end + end + + describe '#closed_by_merge_requests' do + let(:closed_issue) { build(:issue, :closed, project: project)} + + it 'returns the open merge requests that close this issue' do + create_closing_mr(source_project: project, state: 'closed') + + expect(service.closed_by_merge_requests(issue)).to match_array([closing_mr, closing_mr_other_project]) + end + + it 'returns an empty array when the current issue is closed already' do + expect(service.closed_by_merge_requests(closed_issue)).to eq([]) + end + end +end diff --git a/spec/support/helpers/cycle_analytics_helpers.rb b/spec/support/helpers/cycle_analytics_helpers.rb index c228bd2393b..e0fceae88de 100644 --- a/spec/support/helpers/cycle_analytics_helpers.rb +++ b/spec/support/helpers/cycle_analytics_helpers.rb @@ -65,7 +65,9 @@ module CycleAnalyticsHelpers end def merge_merge_requests_closing_issue(user, project, issue) - merge_requests = issue.closed_by_merge_requests(user) + merge_requests = Issues::ReferencedMergeRequestsService + .new(project, user) + .closed_by_merge_requests(issue) merge_requests.each { |merge_request| MergeRequests::MergeService.new(project, user).execute(merge_request) } end |