build_service.rb 2.47 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
module Issues
  class BuildService < Issues::BaseService
5 6
    include ResolveDiscussions

7
    def execute
8
      filter_resolve_discussion_params
9 10 11
      @issue = project.issues.new(issue_params).tap do |issue|
        ensure_milestone_available(issue)
      end
12 13
    end

14
    def issue_params_with_info_from_discussions
Bob Van Landuyt's avatar
Bob Van Landuyt committed
15
      return {} unless merge_request_to_resolve_discussions_of
16

Bob Van Landuyt's avatar
Bob Van Landuyt committed
17
      { title: title_from_merge_request, description: description_for_discussions }
18 19
    end

Bob Van Landuyt's avatar
Bob Van Landuyt committed
20
    def title_from_merge_request
Bob Van Landuyt's avatar
Bob Van Landuyt committed
21
      "Follow-up from \"#{merge_request_to_resolve_discussions_of.title}\""
22 23
    end

24 25
    def description_for_discussions
      if discussions_to_resolve.empty?
26
        return "There are no unresolved discussions. "\
Bob Van Landuyt's avatar
Bob Van Landuyt committed
27
               "Review the conversation in #{merge_request_to_resolve_discussions_of.to_reference}"
28 29
      end

30
      description = "The following #{'discussion'.pluralize(discussions_to_resolve.size)} "\
Bob Van Landuyt's avatar
Bob Van Landuyt committed
31
                    "from #{merge_request_to_resolve_discussions_of.to_reference} "\
32 33
                    "should be addressed:"

34 35 36 37
      [description, *items_for_discussions].join("\n\n")
    end

    def items_for_discussions
38
      discussions_to_resolve.map { |discussion| item_for_discussion(discussion) }
39 40 41
    end

    def item_for_discussion(discussion)
42
      first_note_to_resolve = discussion.first_note_to_resolve || discussion.first_note
43

44 45
      is_very_first_note = first_note_to_resolve == discussion.first_note
      action = is_very_first_note ? "started" : "commented on"
46 47 48 49 50

      note_url = Gitlab::UrlBuilder.build(first_note_to_resolve)

      other_note_count = discussion.notes.size - 1

51 52
      discussion_info = ["- [ ] #{first_note_to_resolve.author.to_reference} #{action} a [discussion](#{note_url}): "]
      discussion_info << "(+#{other_note_count} #{'comment'.pluralize(other_note_count)})" if other_note_count > 0
53

54
      note_without_block_quotes = Banzai::Filter::BlockquoteFenceFilter.new(first_note_to_resolve.note).call
55 56
      spaces = ' ' * 4
      quote = note_without_block_quotes.lines.map { |line| "#{spaces}> #{line}" }.join
57

58
      [discussion_info.join(' '), quote].join("\n\n")
59 60 61
    end

    def issue_params
62
      @issue_params ||= issue_params_with_info_from_discussions.merge(whitelisted_issue_params)
63 64 65 66 67 68 69 70
    end

    def whitelisted_issue_params
      if can?(current_user, :admin_issue, project)
        params.slice(:title, :description, :milestone_id)
      else
        params.slice(:title, :description)
      end
71 72 73
    end
  end
end