diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 11:18:50 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 11:18:50 +0000 |
commit | 8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781 (patch) | |
tree | a77e7fe7a93de11213032ed4ab1f33a3db51b738 /app/services/discussions/resolve_service.rb | |
parent | 00b35af3db1abfe813a778f643dad221aad51fca (diff) | |
download | gitlab-ce-8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781.tar.gz |
Add latest changes from gitlab-org/gitlab@13-1-stable-ee
Diffstat (limited to 'app/services/discussions/resolve_service.rb')
-rw-r--r-- | app/services/discussions/resolve_service.rb | 55 |
1 files changed, 49 insertions, 6 deletions
diff --git a/app/services/discussions/resolve_service.rb b/app/services/discussions/resolve_service.rb index 816cd45b07a..946fb5f1372 100644 --- a/app/services/discussions/resolve_service.rb +++ b/app/services/discussions/resolve_service.rb @@ -2,25 +2,68 @@ module Discussions class ResolveService < Discussions::BaseService - def execute(one_or_more_discussions) - Array(one_or_more_discussions).each { |discussion| resolve_discussion(discussion) } + 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 end def resolve_discussion(discussion) return unless discussion.can_resolve?(current_user) discussion.resolve!(current_user) + @resolved_count += 1 - MergeRequests::ResolvedDiscussionNotificationService.new(project, current_user).execute(merge_request) + 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 end + def first_discussion + @first_discussion ||= discussions.first + end + def merge_request - params[:merge_request] + strong_memoize(:merge_request) do + 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 follow_up_issue - params[:follow_up_issue] + def discussions_ready_to_merge? + merge_request.auto_merge_enabled? && merge_request.mergeable_discussions_state? end end end |