2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe MergeRequests::PostMergeService do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:user) { create(:user) }
|
2019-07-31 22:56:46 +05:30
|
|
|
let(:merge_request) { create(:merge_request, assignees: [user]) }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:project) { merge_request.project }
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
subject { described_class.new(project, user).execute(merge_request) }
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
before do
|
2018-11-18 11:00:15 +05:30
|
|
|
project.add_maintainer(user)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
it_behaves_like 'cache counters invalidator'
|
2021-03-08 18:12:59 +05:30
|
|
|
it_behaves_like 'merge request reviewers cache counters invalidator'
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
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')
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
expect { subject }.to change { project.open_merge_requests_count }.from(1).to(0)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
subject
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it 'calls the merge request activity counter' do
|
|
|
|
expect(Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter)
|
|
|
|
.to receive(:track_merge_mr_action)
|
|
|
|
.with(user: user)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
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)
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
subject
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
expect(diff_removal_service).to have_received(:execute)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'marks MR as merged regardless of errors when closing issues' do
|
2020-11-24 15:15:51 +05:30
|
|
|
merge_request.update!(target_branch: 'foo')
|
2018-11-08 19:23:39 +05:30
|
|
|
allow(project).to receive(:default_branch).and_return('foo')
|
|
|
|
|
|
|
|
issue = create(:issue, project: project)
|
2018-11-18 11:00:15 +05:30
|
|
|
allow(merge_request).to receive(:visible_closing_issues_for).and_return([issue])
|
2020-07-28 23:09:34 +05:30
|
|
|
expect_next_instance_of(Issues::CloseService) do |close_service|
|
|
|
|
allow(close_service).to receive(:execute).with(issue, commit: merge_request).and_raise(RuntimeError)
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
expect { subject }.to raise_error(RuntimeError)
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
expect(merge_request.reload).to be_merged
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
it 'clean up environments for the merge request' do
|
2020-07-28 23:09:34 +05:30
|
|
|
expect_next_instance_of(Ci::StopEnvironmentsService) do |stop_environment_service|
|
|
|
|
expect(stop_environment_service).to receive(:execute_for_merge_request).with(merge_request)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it 'schedules CleanupRefsService' do
|
|
|
|
expect(MergeRequests::CleanupRefsService).to receive(:schedule).with(merge_request)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
context 'when the merge request has review apps' do
|
|
|
|
it 'cancels all review app deployments' do
|
|
|
|
pipeline = create(:ci_pipeline,
|
|
|
|
source: :merge_request_event,
|
|
|
|
merge_request: merge_request,
|
|
|
|
project: project,
|
|
|
|
sha: merge_request.diff_head_sha,
|
|
|
|
merge_requests_as_head_pipeline: [merge_request])
|
|
|
|
|
|
|
|
review_env_a = create(:environment, project: project, state: :available, name: 'review/a')
|
|
|
|
review_env_b = create(:environment, project: project, state: :available, name: 'review/b')
|
|
|
|
review_env_c = create(:environment, project: project, state: :stopped, name: 'review/c')
|
|
|
|
deploy_env = create(:environment, project: project, state: :available, name: 'deploy')
|
|
|
|
|
|
|
|
review_job_a1 = create(:ci_build, :with_deployment, :start_review_app,
|
|
|
|
pipeline: pipeline, project: project, environment: review_env_a.name)
|
|
|
|
review_job_a2 = create(:ci_build, :with_deployment, :start_review_app,
|
|
|
|
pipeline: pipeline, project: project, environment: review_env_a.name)
|
|
|
|
finished_review_job_a = create(:ci_build, :with_deployment, :start_review_app,
|
|
|
|
pipeline: pipeline, project: project, status: :success, environment: review_env_a.name)
|
|
|
|
review_job_b1 = create(:ci_build, :with_deployment, :start_review_app,
|
|
|
|
pipeline: pipeline, project: project, environment: review_env_b.name)
|
|
|
|
review_job_b2 = create(:ci_build, :start_review_app,
|
|
|
|
pipeline: pipeline, project: project, environment: review_env_b.name)
|
|
|
|
review_job_c1 = create(:ci_build, :with_deployment, :start_review_app,
|
|
|
|
pipeline: pipeline, project: project, environment: review_env_c.name)
|
|
|
|
deploy_job = create(:ci_build, :with_deployment, :deploy_to_production,
|
|
|
|
pipeline: pipeline, project: project, environment: deploy_env.name)
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(review_job_a1.reload.canceled?).to be true
|
|
|
|
expect(review_job_a2.reload.canceled?).to be true
|
|
|
|
expect(finished_review_job_a.reload.status).to eq "success"
|
|
|
|
expect(finished_review_job_a.reload.canceled?).to be false
|
|
|
|
expect(review_job_b1.reload.canceled?).to be true
|
|
|
|
expect(review_job_b2.reload.canceled?).to be false
|
|
|
|
expect(review_job_c1.reload.canceled?).to be false
|
|
|
|
expect(deploy_job.reload.canceled?).to be false
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|