2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Discussions
|
|
|
|
class ResolveService < Discussions::BaseService
|
2020-06-23 00:09:42 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
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?
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
discussions.each(&method(:resolve_discussion))
|
|
|
|
process_auto_merge
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_accessor :discussions, :follow_up_issue
|
|
|
|
|
|
|
|
def noteable_is_same?
|
|
|
|
return true unless discussions.size > 1
|
|
|
|
|
|
|
|
# Perform this check without fetching extra records
|
|
|
|
discussions.all? do |discussion|
|
|
|
|
discussion.noteable_type == first_discussion.noteable_type &&
|
|
|
|
discussion.noteable_id == first_discussion.noteable_id
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def resolve_discussion(discussion)
|
|
|
|
return unless discussion.can_resolve?(current_user)
|
|
|
|
|
|
|
|
discussion.resolve!(current_user)
|
2020-06-23 00:09:42 +05:30
|
|
|
@resolved_count += 1
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
MergeRequests::ResolvedDiscussionNotificationService.new(project, current_user).execute(merge_request) if merge_request
|
2017-08-17 22:00:37 +05:30
|
|
|
SystemNoteService.discussion_continued_in_issue(discussion, project, current_user, follow_up_issue) if follow_up_issue
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def first_discussion
|
|
|
|
@first_discussion ||= discussions.first
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def merge_request
|
2020-06-23 00:09:42 +05:30
|
|
|
strong_memoize(:merge_request) do
|
|
|
|
first_discussion.noteable if first_discussion.for_merge_request?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_auto_merge
|
|
|
|
return unless merge_request
|
2020-10-24 23:57:45 +05:30
|
|
|
return unless @resolved_count > 0
|
2020-06-23 00:09:42 +05:30
|
|
|
return unless discussions_ready_to_merge?
|
|
|
|
|
|
|
|
AutoMergeProcessWorker.perform_async(merge_request.id)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def discussions_ready_to_merge?
|
|
|
|
merge_request.auto_merge_enabled? && merge_request.mergeable_discussions_state?
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|