summaryrefslogtreecommitdiff
path: root/app/services/discussions/resolve_service.rb
blob: 946fb5f1372a06d2986b43504c9905cbb3d08b36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true

module Discussions
  class ResolveService < Discussions::BaseService
    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) 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
      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 discussions_ready_to_merge?
      merge_request.auto_merge_enabled? && merge_request.mergeable_discussions_state?
    end
  end
end