2019-04-11 12:17:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-26 13:29:42 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 15:08:50 +00:00
|
|
|
RSpec.describe MergeRequests::PostMergeService do
|
2021-02-17 09:09:36 +00:00
|
|
|
include ProjectForksHelper
|
|
|
|
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:merge_request, reload: true) { create(:merge_request, assignees: [user]) }
|
|
|
|
let_it_be(:project) { merge_request.project }
|
2017-05-26 13:29:42 +00:00
|
|
|
|
2021-05-12 03:10:21 +00:00
|
|
|
subject { described_class.new(project: project, current_user: user).execute(merge_request) }
|
2020-06-26 12:08:51 +00:00
|
|
|
|
2017-05-26 13:29:42 +00:00
|
|
|
before do
|
2018-07-11 14:36:08 +00:00
|
|
|
project.add_maintainer(user)
|
2017-05-26 13:29:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
it_behaves_like 'cache counters invalidator'
|
2021-01-08 21:10:30 +00:00
|
|
|
it_behaves_like 'merge request reviewers cache counters invalidator'
|
2017-09-28 16:09:35 +00: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
|
|
|
|
|
2020-06-26 12:08:51 +00:00
|
|
|
expect { subject }.to change { project.open_merge_requests_count }.from(1).to(0)
|
2017-09-28 16:09:35 +00:00
|
|
|
end
|
2017-12-07 17:41:30 +00: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)
|
|
|
|
|
2020-06-26 12:08:51 +00:00
|
|
|
subject
|
2017-12-07 17:41:30 +00:00
|
|
|
end
|
2018-06-11 20:45:16 +00:00
|
|
|
|
2021-01-07 18:10:38 +00:00
|
|
|
it 'calls the merge request activity counter' do
|
|
|
|
expect(Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter)
|
|
|
|
.to receive(:track_merge_mr_action)
|
|
|
|
.with(user: user)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2018-06-11 20:45:16 +00: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)
|
|
|
|
|
2020-06-26 12:08:51 +00:00
|
|
|
subject
|
2018-06-11 20:45:16 +00:00
|
|
|
|
|
|
|
expect(diff_removal_service).to have_received(:execute)
|
|
|
|
end
|
2018-06-27 15:04:54 +00:00
|
|
|
|
2019-04-24 11:37:29 +00:00
|
|
|
it 'clean up environments for the merge request' do
|
2021-08-06 18:09:57 +00:00
|
|
|
expect_next_instance_of(::Environments::StopService) do |stop_environment_service|
|
2022-04-27 09:10:46 +00:00
|
|
|
expect(stop_environment_service).to receive(:execute_for_merge_request_pipeline).with(merge_request)
|
2019-04-24 11:37:29 +00:00
|
|
|
end
|
|
|
|
|
2020-06-26 12:08:51 +00:00
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2020-09-11 12:08:50 +00:00
|
|
|
it 'schedules CleanupRefsService' do
|
|
|
|
expect(MergeRequests::CleanupRefsService).to receive(:schedule).with(merge_request)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2022-05-12 15:08:59 +00:00
|
|
|
context 'when there are issues to be closed' do
|
|
|
|
let_it_be(:issue) { create(:issue, project: project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
merge_request.update!(target_branch: 'foo')
|
|
|
|
|
|
|
|
allow(project).to receive(:default_branch).and_return('foo')
|
|
|
|
allow(merge_request).to receive(:visible_closing_issues_for).and_return([issue])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'performs MergeRequests::CloseIssueWorker asynchronously' do
|
|
|
|
expect(MergeRequests::CloseIssueWorker)
|
|
|
|
.to receive(:perform_async)
|
|
|
|
.with(project.id, user.id, issue.id, merge_request.id)
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(merge_request.reload).to be_merged
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when issue is an external issue' do
|
|
|
|
let_it_be(:issue) { ExternalIssue.new('JIRA-123', project) }
|
|
|
|
|
|
|
|
it 'executes Issues::CloseService' do
|
|
|
|
expect_next_instance_of(Issues::CloseService) do |close_service|
|
|
|
|
expect(close_service).to receive(:execute).with(issue, commit: merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(merge_request.reload).to be_merged
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-26 12:08:51 +00:00
|
|
|
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)
|
2020-07-09 00:09:11 +00:00
|
|
|
finished_review_job_a = create(:ci_build, :with_deployment, :start_review_app,
|
|
|
|
pipeline: pipeline, project: project, status: :success, environment: review_env_a.name)
|
2020-06-26 12:08:51 +00:00
|
|
|
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
|
2020-07-09 00:09:11 +00:00
|
|
|
expect(finished_review_job_a.reload.status).to eq "success"
|
|
|
|
expect(finished_review_job_a.reload.canceled?).to be false
|
2020-06-26 12:08:51 +00:00
|
|
|
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-04-24 11:37:29 +00:00
|
|
|
end
|
2017-05-26 13:29:42 +00:00
|
|
|
end
|
|
|
|
end
|