2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-26 17:21:50 -04:00
|
|
|
module Issues
|
|
|
|
class BuildService < Issues::BaseService
|
2017-02-27 12:12:52 -05:00
|
|
|
include ResolveDiscussions
|
|
|
|
|
2016-10-26 17:21:50 -04:00
|
|
|
def execute
|
2017-02-27 12:12:52 -05:00
|
|
|
filter_resolve_discussion_params
|
2019-01-14 05:46:39 -05:00
|
|
|
@issue = project.issues.new(issue_params).tap do |issue|
|
|
|
|
ensure_milestone_available(issue)
|
|
|
|
end
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
|
|
|
|
2016-12-22 07:31:12 -05:00
|
|
|
def issue_params_with_info_from_discussions
|
2017-03-10 03:19:12 -05:00
|
|
|
return {} unless merge_request_to_resolve_discussions_of
|
2016-10-26 17:21:50 -04:00
|
|
|
|
2017-03-07 18:00:21 -05:00
|
|
|
{ title: title_from_merge_request, description: description_for_discussions }
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
|
|
|
|
2017-03-07 18:00:21 -05:00
|
|
|
def title_from_merge_request
|
2017-03-10 03:19:12 -05:00
|
|
|
"Follow-up from \"#{merge_request_to_resolve_discussions_of.title}\""
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
|
|
|
|
2016-12-22 07:31:12 -05:00
|
|
|
def description_for_discussions
|
|
|
|
if discussions_to_resolve.empty?
|
2016-10-26 17:21:50 -04:00
|
|
|
return "There are no unresolved discussions. "\
|
2017-03-10 03:19:12 -05:00
|
|
|
"Review the conversation in #{merge_request_to_resolve_discussions_of.to_reference}"
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
|
|
|
|
2016-12-22 07:31:12 -05:00
|
|
|
description = "The following #{'discussion'.pluralize(discussions_to_resolve.size)} "\
|
2017-03-10 03:19:12 -05:00
|
|
|
"from #{merge_request_to_resolve_discussions_of.to_reference} "\
|
2016-12-22 07:31:12 -05:00
|
|
|
"should be addressed:"
|
|
|
|
|
2016-10-26 17:21:50 -04:00
|
|
|
[description, *items_for_discussions].join("\n\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def items_for_discussions
|
2016-12-22 07:31:12 -05:00
|
|
|
discussions_to_resolve.map { |discussion| item_for_discussion(discussion) }
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def item_for_discussion(discussion)
|
2017-04-05 21:48:26 -04:00
|
|
|
first_note_to_resolve = discussion.first_note_to_resolve || discussion.first_note
|
2016-10-26 17:21:50 -04:00
|
|
|
|
2017-04-05 21:48:26 -04:00
|
|
|
is_very_first_note = first_note_to_resolve == discussion.first_note
|
|
|
|
action = is_very_first_note ? "started" : "commented on"
|
2017-04-06 11:05:57 -04:00
|
|
|
|
|
|
|
note_url = Gitlab::UrlBuilder.build(first_note_to_resolve)
|
|
|
|
|
|
|
|
other_note_count = discussion.notes.size - 1
|
|
|
|
|
2018-07-16 12:31:01 -04:00
|
|
|
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
|
2016-10-26 17:21:50 -04:00
|
|
|
|
2017-04-05 21:48:26 -04:00
|
|
|
note_without_block_quotes = Banzai::Filter::BlockquoteFenceFilter.new(first_note_to_resolve.note).call
|
2017-03-07 18:55:50 -05:00
|
|
|
spaces = ' ' * 4
|
|
|
|
quote = note_without_block_quotes.lines.map { |line| "#{spaces}> #{line}" }.join
|
2016-10-26 17:21:50 -04:00
|
|
|
|
2018-07-16 12:31:01 -04:00
|
|
|
[discussion_info.join(' '), quote].join("\n\n")
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def issue_params
|
2019-02-06 07:41:17 -05:00
|
|
|
@issue_params ||= build_issue_params
|
2017-01-28 19:14:56 -05:00
|
|
|
end
|
|
|
|
|
2019-02-06 07:41:17 -05:00
|
|
|
private
|
|
|
|
|
2017-01-28 19:14:56 -05:00
|
|
|
def whitelisted_issue_params
|
|
|
|
if can?(current_user, :admin_issue, project)
|
|
|
|
params.slice(:title, :description, :milestone_id)
|
|
|
|
else
|
|
|
|
params.slice(:title, :description)
|
|
|
|
end
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
2019-02-06 07:41:17 -05:00
|
|
|
|
|
|
|
def build_issue_params
|
|
|
|
issue_params_with_info_from_discussions.merge(whitelisted_issue_params)
|
|
|
|
end
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Issues::BuildService.prepend_if_ee('EE::Issues::BuildService')
|