Commit e05135e0 authored by Vladimir Shushlin's avatar Vladimir Shushlin Committed by Thong Kuah

Disabling setting drops merge requests

When the project-level setting is disabled,
merge requests will be immediately dropped.

Closes https://gitlab.com/gitlab-org/gitlab/issues/14959
parent 543f9a22
......@@ -148,7 +148,7 @@ module EE
delegate :log_jira_dvcs_integration_usage, :jira_dvcs_server_last_sync_at, :jira_dvcs_cloud_last_sync_at, to: :feature_usage
delegate :merge_pipelines_enabled, :merge_pipelines_enabled=, :merge_pipelines_enabled?, to: :ci_cd_settings
delegate :merge_pipelines_enabled, :merge_pipelines_enabled=, :merge_pipelines_enabled?, :merge_pipelines_were_disabled?, to: :ci_cd_settings
delegate :merge_trains_enabled, :merge_trains_enabled=, :merge_trains_enabled?, to: :ci_cd_settings
validates :repository_size_limit,
......
......@@ -15,5 +15,9 @@ module EE
merge_pipelines_enabled? && project.feature_available?(:merge_trains) &&
::Feature.enabled?(:merge_trains_enabled, project, default_enabled: true)
end
def merge_pipelines_were_disabled?
saved_change_to_attribute?(:merge_pipelines_enabled, from: true, to: false)
end
end
end
......@@ -21,6 +21,10 @@ class MergeTrain < ApplicationRecord
all_in_train(merge_request).first
end
def first_in_trains(project)
MergeRequest.preload(:target_project).where(id: first_merge_request_ids(project))
end
def first_in_train_from(merge_request_ids)
merge_request = MergeRequest.find(merge_request_ids.first)
all_in_train(merge_request).where(id: merge_request_ids).first
......@@ -35,6 +39,14 @@ class MergeTrain < ApplicationRecord
.where('merge_requests.target_project_id = ?', merge_request.target_project_id)
.where('merge_requests.target_branch = ?', merge_request.target_branch)
end
private
def first_merge_request_ids(project)
MergeTrain.where(target_project: project)
.select('DISTINCT ON (target_branch) merge_request_id')
.order(:target_branch, :id)
end
end
def all_next
......
......@@ -29,6 +29,7 @@ module EE
if result[:status] == :success
cleanup_approvers(project) if should_remove_old_approvers
refresh_merge_trains(project)
log_audit_events
......@@ -65,6 +66,14 @@ module EE
def sync_wiki_on_enable
::Geo::RepositoryUpdatedService.new(project.wiki.repository).execute
end
def refresh_merge_trains(project)
return unless project.merge_pipelines_were_disabled?
MergeTrain.first_in_trains(project).each do |merge_request|
AutoMergeProcessWorker.perform_async(merge_request.id)
end
end
end
end
end
---
title: Drop all merge requests from merge trains when the project-level setting is disabled
merge_request: 17774
author:
type: added
......@@ -67,6 +67,20 @@ describe MergeTrain do
end
end
describe '.first_in_trains' do
let!(:first_on_master) { create_merge_request_on_train(target_branch: 'master', source_branch: 'feature-1') }
let!(:second_on_master) { create_merge_request_on_train(target_branch: 'master', source_branch: 'feature-2') }
let!(:first_on_stable) { create_merge_request_on_train(target_branch: 'stable', source_branch: 'feature-1-backport') }
let!(:second_on_stable) { create_merge_request_on_train(target_branch: 'stable', source_branch: 'feature-2-backport') }
subject { described_class.first_in_trains(project) }
it 'returns only first merge requests per merge train' do
is_expected.to contain_exactly(first_on_master, first_on_stable)
end
end
describe '.first_in_train_from' do
subject { described_class.first_in_train_from(merge_request_ids) }
......
......@@ -64,4 +64,27 @@ describe ProjectCiCdSetting do
it { is_expected.to be_falsy }
end
end
describe '#merge_pipelines_were_disabled?' do
subject { project.merge_pipelines_were_disabled? }
let(:project) { create(:project) }
before do
stub_licensed_features(merge_pipelines: true, merge_trains: true)
project.update(merge_pipelines_enabled: true)
end
context 'when merge pipelines option is disabled' do
before do
project.update(merge_pipelines_enabled: false)
end
it { is_expected.to be true }
end
context 'when merge pipelines option is intact' do
it { is_expected.to be false }
end
end
end
......@@ -237,6 +237,37 @@ describe Projects::UpdateService, '#execute' do
end
end
context 'when there are merge requests in merge train' do
before do
stub_licensed_features(merge_pipelines: true, merge_trains: true)
project.update(merge_pipelines_enabled: true)
end
let!(:first_merge_request) do
create(:merge_request, :on_train, target_project: project, source_project: project)
end
let!(:second_merge_request) do
create(:merge_request, :on_train, target_project: project, source_project: project, source_branch: 'feature-1')
end
context 'when merge pipelines option is disabled' do
it 'drops all merge request in the train' do
expect do
update_project(project, user, merge_pipelines_enabled: false)
end.to change { MergeTrain.count }.from(2).to(0)
end
end
context 'when merge pipelines option stays enabled' do
it 'does not drop all merge request in the train' do
expect do
update_project(project, user, merge_pipelines_enabled: true)
end.not_to change { MergeTrain.count }
end
end
end
it 'returns an error result when record cannot be updated' do
admin = create(:admin)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment