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
|
2021-09-10 11:11:12 -04:00
|
|
|
|
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
|
2021-09-10 11:11:12 -04:00
|
|
|
|
|
|
|
# If :issue_type is nil then params[:issue_type] was either nil
|
|
|
|
# or not permitted. Either way, the :issue_type will default
|
|
|
|
# to the column default of `issue`. And that means we need to
|
|
|
|
# ensure the work_item_type_id is set
|
|
|
|
@issue_params[:work_item_type_id] = get_work_item_type_id(@issue_params[:issue_type])
|
|
|
|
@issue_params
|
2017-01-28 19:14:56 -05:00
|
|
|
end
|
|
|
|
|
2019-02-06 07:41:17 -05:00
|
|
|
private
|
|
|
|
|
2021-05-14 08:10:58 -04:00
|
|
|
def allowed_issue_params
|
|
|
|
allowed_params = [
|
|
|
|
:title,
|
|
|
|
:description,
|
|
|
|
:confidential
|
|
|
|
]
|
2020-10-20 05:08:43 -04:00
|
|
|
|
2021-05-14 08:10:58 -04:00
|
|
|
allowed_params << :milestone_id if can?(current_user, :admin_issue, project)
|
2021-11-08 10:13:35 -05:00
|
|
|
allowed_params << :issue_type if create_issue_type_allowed?(project, params[:issue_type])
|
2020-05-08 05:09:39 -04:00
|
|
|
|
2021-05-14 08:10:58 -04:00
|
|
|
params.slice(*allowed_params)
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
2019-02-06 07:41:17 -05:00
|
|
|
|
|
|
|
def build_issue_params
|
2020-10-20 05:08:43 -04:00
|
|
|
{ author: current_user }
|
|
|
|
.merge(issue_params_with_info_from_discussions)
|
|
|
|
.merge(allowed_issue_params)
|
2021-09-10 11:11:12 -04:00
|
|
|
.with_indifferent_access
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_work_item_type_id(issue_type = :issue)
|
|
|
|
find_work_item_type_id(issue_type)
|
2019-02-06 07:41:17 -05:00
|
|
|
end
|
2016-10-26 17:21:50 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Issues::BuildService.prepend_mod_with('Issues::BuildService')
|