2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Issues
|
|
|
|
class BuildService < Issues::BaseService
|
|
|
|
include ResolveDiscussions
|
|
|
|
|
|
|
|
def execute
|
|
|
|
filter_resolve_discussion_params
|
2019-03-13 22:55:13 +05:30
|
|
|
@issue = project.issues.new(issue_params).tap do |issue|
|
|
|
|
ensure_milestone_available(issue)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def issue_params_with_info_from_discussions
|
|
|
|
return {} unless merge_request_to_resolve_discussions_of
|
|
|
|
|
|
|
|
{ title: title_from_merge_request, description: description_for_discussions }
|
|
|
|
end
|
|
|
|
|
|
|
|
def title_from_merge_request
|
|
|
|
"Follow-up from \"#{merge_request_to_resolve_discussions_of.title}\""
|
|
|
|
end
|
|
|
|
|
|
|
|
def description_for_discussions
|
|
|
|
if discussions_to_resolve.empty?
|
|
|
|
return "There are no unresolved discussions. "\
|
|
|
|
"Review the conversation in #{merge_request_to_resolve_discussions_of.to_reference}"
|
|
|
|
end
|
|
|
|
|
|
|
|
description = "The following #{'discussion'.pluralize(discussions_to_resolve.size)} "\
|
|
|
|
"from #{merge_request_to_resolve_discussions_of.to_reference} "\
|
|
|
|
"should be addressed:"
|
|
|
|
|
|
|
|
[description, *items_for_discussions].join("\n\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def items_for_discussions
|
|
|
|
discussions_to_resolve.map { |discussion| item_for_discussion(discussion) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def item_for_discussion(discussion)
|
|
|
|
first_note_to_resolve = discussion.first_note_to_resolve || discussion.first_note
|
|
|
|
|
|
|
|
is_very_first_note = first_note_to_resolve == discussion.first_note
|
|
|
|
action = is_very_first_note ? "started" : "commented on"
|
|
|
|
|
|
|
|
note_url = Gitlab::UrlBuilder.build(first_note_to_resolve)
|
|
|
|
|
|
|
|
other_note_count = discussion.notes.size - 1
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
note_without_block_quotes = Banzai::Filter::BlockquoteFenceFilter.new(first_note_to_resolve.note).call
|
|
|
|
spaces = ' ' * 4
|
|
|
|
quote = note_without_block_quotes.lines.map { |line| "#{spaces}> #{line}" }.join
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
[discussion_info.join(' '), quote].join("\n\n")
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def issue_params
|
2019-07-07 11:18:12 +05:30
|
|
|
@issue_params ||= build_issue_params
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
private
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def allowed_issue_base_params
|
|
|
|
[:title, :description, :confidential, :issue_type]
|
|
|
|
end
|
|
|
|
|
|
|
|
def allowed_issue_admin_params
|
|
|
|
[:milestone_id]
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def allowed_issue_params
|
2017-08-17 22:00:37 +05:30
|
|
|
if can?(current_user, :admin_issue, project)
|
2021-01-03 14:25:43 +05:30
|
|
|
params.slice(*(allowed_issue_base_params + allowed_issue_admin_params))
|
2017-08-17 22:00:37 +05:30
|
|
|
else
|
2021-01-03 14:25:43 +05:30
|
|
|
params.slice(*allowed_issue_base_params)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
def build_issue_params
|
2021-01-03 14:25:43 +05:30
|
|
|
{ author: current_user }
|
|
|
|
.merge(issue_params_with_info_from_discussions)
|
|
|
|
.merge(allowed_issue_params)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
Issues::BuildService.prepend_if_ee('EE::Issues::BuildService')
|