2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-26 09:29:42 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe MergeRequests::PostMergeService do
|
2017-05-26 09:29:42 -04:00
|
|
|
let(:user) { create(:user) }
|
2019-04-07 14:35:16 -04:00
|
|
|
let(:merge_request) { create(:merge_request, assignees: [user]) }
|
2017-05-26 09:29:42 -04:00
|
|
|
let(:project) { merge_request.project }
|
|
|
|
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2017-05-26 09:29:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
it_behaves_like 'cache counters invalidator'
|
2017-09-28 12:09:35 -04:00
|
|
|
|
|
|
|
it 'refreshes the number of open merge requests for a valid MR', :use_clean_rails_memory_store_caching do
|
|
|
|
# Cache the counter before the MR changed state.
|
|
|
|
project.open_merge_requests_count
|
|
|
|
merge_request.update!(state: 'merged')
|
|
|
|
|
|
|
|
service = described_class.new(project, user, {})
|
|
|
|
|
|
|
|
expect { service.execute(merge_request) }
|
|
|
|
.to change { project.open_merge_requests_count }.from(1).to(0)
|
|
|
|
end
|
2017-12-07 12:41:30 -05:00
|
|
|
|
|
|
|
it 'updates metrics' do
|
|
|
|
metrics = merge_request.metrics
|
|
|
|
metrics_service = double(MergeRequestMetricsService)
|
|
|
|
allow(MergeRequestMetricsService)
|
|
|
|
.to receive(:new)
|
|
|
|
.with(metrics)
|
|
|
|
.and_return(metrics_service)
|
|
|
|
|
|
|
|
expect(metrics_service).to receive(:merge)
|
|
|
|
|
|
|
|
described_class.new(project, user, {}).execute(merge_request)
|
|
|
|
end
|
2018-06-11 16:45:16 -04:00
|
|
|
|
|
|
|
it 'deletes non-latest diffs' do
|
|
|
|
diff_removal_service = instance_double(MergeRequests::DeleteNonLatestDiffsService, execute: nil)
|
|
|
|
|
|
|
|
expect(MergeRequests::DeleteNonLatestDiffsService)
|
|
|
|
.to receive(:new).with(merge_request)
|
|
|
|
.and_return(diff_removal_service)
|
|
|
|
|
|
|
|
described_class.new(project, user, {}).execute(merge_request)
|
|
|
|
|
|
|
|
expect(diff_removal_service).to have_received(:execute)
|
|
|
|
end
|
2018-06-27 11:04:54 -04:00
|
|
|
|
|
|
|
it 'marks MR as merged regardless of errors when closing issues' do
|
|
|
|
merge_request.update(target_branch: 'foo')
|
|
|
|
allow(project).to receive(:default_branch).and_return('foo')
|
|
|
|
|
|
|
|
issue = create(:issue, project: project)
|
2018-07-30 14:52:42 -04:00
|
|
|
allow(merge_request).to receive(:visible_closing_issues_for).and_return([issue])
|
2018-06-27 11:04:54 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
expect(merge_request.reload).to be_merged
|
|
|
|
end
|
2019-04-24 07:37:29 -04:00
|
|
|
|
|
|
|
it 'clean up environments for the merge request' do
|
|
|
|
expect_next_instance_of(Ci::StopEnvironmentsService) do |service|
|
|
|
|
expect(service).to receive(:execute_for_merge_request).with(merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
described_class.new(project, user).execute(merge_request)
|
|
|
|
end
|
2017-05-26 09:29:42 -04:00
|
|
|
end
|
|
|
|
end
|