summaryrefslogtreecommitdiff
path: root/app/services/draft_notes/create_service.rb
blob: 501778b7d5f8f3236dbca45e7b94e1be6f80a79f (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
# frozen_string_literal: true

module DraftNotes
  class CreateService < DraftNotes::BaseService
    attr_accessor :in_draft_mode, :in_reply_to_discussion_id

    def initialize(merge_request, current_user, params = nil)
      @in_reply_to_discussion_id = params.delete(:in_reply_to_discussion_id)
      super
    end

    def execute
      if in_reply_to_discussion_id.present?
        unless discussion
          return base_error(_('Thread to reply to cannot be found'))
        end

        params[:discussion_id] = discussion.reply_id
      end

      if params[:resolve_discussion] && !can_resolve_discussion?
        return base_error(_('User is not allowed to resolve thread'))
      end

      draft_note = DraftNote.new(params)
      draft_note.merge_request = merge_request
      draft_note.author = current_user
      draft_note.save

      if in_reply_to_discussion_id.blank? && draft_note.diff_file&.unfolded?
        merge_request.diffs.clear_cache
      end

      draft_note
    end

    private

    def base_error(text)
      DraftNote.new.tap do |draft|
        draft.errors.add(:base, text)
      end
    end

    def discussion
      @discussion ||= merge_request.notes.find_discussion(in_reply_to_discussion_id)
    end

    def can_resolve_discussion?
      note = discussion&.notes&.first
      return false unless note

      current_user && Ability.allowed?(current_user, :resolve_note, note)
    end
  end
end