2020-05-27 14:08:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DraftNotes
|
|
|
|
class PublishService < DraftNotes::BaseService
|
|
|
|
def execute(draft = nil)
|
|
|
|
return error('Not allowed to create notes') unless can?(current_user, :create_note, merge_request)
|
|
|
|
|
|
|
|
if draft
|
|
|
|
publish_draft_note(draft)
|
|
|
|
else
|
|
|
|
publish_draft_notes
|
2021-01-14 10:10:46 -05:00
|
|
|
merge_request_activity_counter.track_publish_review_action(user: current_user)
|
2020-05-27 14:08:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
success
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
message = "Unable to save #{e.record.class.name}: #{e.record.errors.full_messages.join(", ")} "
|
|
|
|
error(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def publish_draft_note(draft)
|
|
|
|
create_note_from_draft(draft)
|
|
|
|
draft.delete
|
|
|
|
|
2021-05-11 23:10:21 -04:00
|
|
|
MergeRequests::ResolvedDiscussionNotificationService.new(project: project, current_user: current_user).execute(merge_request)
|
2020-05-27 14:08:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def publish_draft_notes
|
|
|
|
return if draft_notes.empty?
|
|
|
|
|
|
|
|
review = Review.create!(author: current_user, merge_request: merge_request, project: project)
|
|
|
|
|
|
|
|
draft_notes.map do |draft_note|
|
|
|
|
draft_note.review = review
|
|
|
|
create_note_from_draft(draft_note)
|
|
|
|
end
|
|
|
|
draft_notes.delete_all
|
|
|
|
|
2021-02-02 07:10:15 -05:00
|
|
|
set_reviewed
|
|
|
|
|
2020-05-27 14:08:14 -04:00
|
|
|
notification_service.async.new_review(review)
|
2021-05-11 23:10:21 -04:00
|
|
|
MergeRequests::ResolvedDiscussionNotificationService.new(project: project, current_user: current_user).execute(merge_request)
|
2020-05-27 14:08:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_note_from_draft(draft)
|
|
|
|
# Make sure the diff file is unfolded in order to find the correct line
|
|
|
|
# codes.
|
|
|
|
draft.diff_file&.unfold_diff_lines(draft.original_position)
|
|
|
|
|
|
|
|
note = Notes::CreateService.new(draft.project, draft.author, draft.publish_params).execute
|
|
|
|
set_discussion_resolve_status(note, draft)
|
|
|
|
|
|
|
|
note
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_discussion_resolve_status(note, draft_note)
|
|
|
|
return unless draft_note.discussion_id.present?
|
|
|
|
|
|
|
|
discussion = note.discussion
|
|
|
|
|
|
|
|
if draft_note.resolve_discussion && discussion.can_resolve?(current_user)
|
|
|
|
discussion.resolve!(current_user)
|
|
|
|
else
|
|
|
|
discussion.unresolve!
|
|
|
|
end
|
|
|
|
end
|
2021-02-02 07:10:15 -05:00
|
|
|
|
|
|
|
def set_reviewed
|
2021-05-11 23:10:21 -04:00
|
|
|
::MergeRequests::MarkReviewerReviewedService.new(project: project, current_user: current_user).execute(merge_request)
|
2021-02-02 07:10:15 -05:00
|
|
|
end
|
2020-05-27 14:08:14 -04:00
|
|
|
end
|
|
|
|
end
|