Commit f6d1e7d2 authored by Shinya Maeda's avatar Shinya Maeda

Merge branch 'sh-fix-auto-merge-after-resolve-discussions' into 'master'

Fix auto-merge not running after discussions resolved

Closes #210068

See merge request gitlab-org/gitlab!33371
parents a8cec955 ebdc9b97
......@@ -7,6 +7,7 @@ module Discussions
def initialize(project, user = nil, params = {})
@discussions = Array.wrap(params.fetch(:one_or_more_discussions))
@follow_up_issue = params[:follow_up_issue]
@resolved_count = 0
raise ArgumentError, 'Discussions must be all for the same noteable' \
unless noteable_is_same?
......@@ -16,6 +17,7 @@ module Discussions
def execute
discussions.each(&method(:resolve_discussion))
process_auto_merge
end
private
......@@ -36,6 +38,7 @@ module Discussions
return unless discussion.can_resolve?(current_user)
discussion.resolve!(current_user)
@resolved_count += 1
MergeRequests::ResolvedDiscussionNotificationService.new(project, current_user).execute(merge_request) if merge_request
SystemNoteService.discussion_continued_in_issue(discussion, project, current_user, follow_up_issue) if follow_up_issue
......@@ -50,5 +53,17 @@ module Discussions
first_discussion.noteable if first_discussion.for_merge_request?
end
end
def process_auto_merge
return unless merge_request
return unless @resolved_count.positive?
return unless discussions_ready_to_merge?
AutoMergeProcessWorker.perform_async(merge_request.id)
end
def discussions_ready_to_merge?
merge_request.auto_merge_enabled? && merge_request.mergeable_discussions_state?
end
end
end
---
title: Fix auto-merge not running after discussions resolved
merge_request: 33371
author:
type: fixed
......@@ -6,7 +6,7 @@ describe Discussions::ResolveService do
describe '#execute' do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) { create(:user, developer_projects: [project]) }
let_it_be(:merge_request) { create(:merge_request, source_project: project) }
let_it_be(:merge_request) { create(:merge_request, :merge_when_pipeline_succeeds, source_project: project) }
let(:discussion) { create(:diff_note_on_merge_request, noteable: merge_request, project: project).to_discussion }
let(:service) { described_class.new(project, user, one_or_more_discussions: discussion) }
......@@ -32,6 +32,36 @@ describe Discussions::ResolveService do
service.execute
end
it 'schedules an auto-merge' do
expect(AutoMergeProcessWorker).to receive(:perform_async).with(discussion.noteable.id)
service.execute
end
context 'with a project that requires all discussion to be resolved' do
before do
project.update(only_allow_merge_if_all_discussions_are_resolved: true)
end
after do
project.update(only_allow_merge_if_all_discussions_are_resolved: false)
end
let_it_be(:other_discussion) { create(:diff_note_on_merge_request, noteable: merge_request, project: project).to_discussion }
it 'does not schedule an auto-merge' do
expect(AutoMergeProcessWorker).not_to receive(:perform_async)
service.execute
end
it 'schedules an auto-merge' do
expect(AutoMergeProcessWorker).to receive(:perform_async)
described_class.new(project, user, one_or_more_discussions: [discussion, other_discussion]).execute
end
end
it 'adds a system note to the discussion' do
issue = create(:issue, project: project)
......@@ -70,6 +100,12 @@ describe Discussions::ResolveService do
service.execute
end
it 'does not schedule an auto-merge' do
expect(AutoMergeProcessWorker).not_to receive(:perform_async)
service.execute
end
end
end
end
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