debian-mirror-gitlab/app/services/draft_notes/publish_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

113 lines
3.7 KiB
Ruby
Raw Normal View History

2020-06-23 00:09:42 +05:30
# 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-03-08 18:12:59 +05:30
merge_request_activity_counter.track_publish_review_action(user: current_user)
2020-06-23 00:09:42 +05:30
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-06-08 01:23:25 +05:30
MergeRequests::ResolvedDiscussionNotificationService.new(project: project, current_user: current_user).execute(merge_request)
2020-06-23 00:09:42 +05:30
end
def publish_draft_notes
return if draft_notes.empty?
review = Review.create!(author: current_user, merge_request: merge_request, project: project)
2021-10-27 15:23:28 +05:30
created_notes = draft_notes.map do |draft_note|
2020-06-23 00:09:42 +05:30
draft_note.review = review
2023-03-17 16:20:25 +05:30
create_note_from_draft(
draft_note,
skip_capture_diff_note_position: true,
skip_keep_around_commits: true,
skip_merge_status_trigger: true
)
2020-06-23 00:09:42 +05:30
end
2021-10-27 15:23:28 +05:30
capture_diff_note_positions(created_notes)
2021-11-11 11:23:49 +05:30
keep_around_commits(created_notes)
2021-10-27 15:23:28 +05:30
draft_notes.delete_all
2021-03-11 19:13:27 +05:30
set_reviewed
2020-06-23 00:09:42 +05:30
notification_service.async.new_review(review)
2021-06-08 01:23:25 +05:30
MergeRequests::ResolvedDiscussionNotificationService.new(project: project, current_user: current_user).execute(merge_request)
2023-03-17 16:20:25 +05:30
GraphqlTriggers.merge_request_merge_status_updated(merge_request)
2020-06-23 00:09:42 +05:30
end
2023-03-17 16:20:25 +05:30
def create_note_from_draft(draft, skip_capture_diff_note_position: false, skip_keep_around_commits: false, skip_merge_status_trigger: false)
2020-06-23 00:09:42 +05:30
# Make sure the diff file is unfolded in order to find the correct line
# codes.
draft.diff_file&.unfold_diff_lines(draft.original_position)
2021-11-11 11:23:49 +05:30
note_params = draft.publish_params.merge(skip_keep_around_commits: skip_keep_around_commits)
note = Notes::CreateService.new(draft.project, draft.author, note_params).execute(
2023-03-17 16:20:25 +05:30
skip_capture_diff_note_position: skip_capture_diff_note_position,
2023-07-09 08:55:56 +05:30
skip_merge_status_trigger: skip_merge_status_trigger,
skip_set_reviewed: true
2021-10-27 15:23:28 +05:30
)
2020-06-23 00:09:42 +05:30
2021-10-27 15:23:28 +05:30
set_discussion_resolve_status(note, draft)
2020-06-23 00:09:42 +05:30
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-03-11 19:13:27 +05:30
def set_reviewed
2021-06-08 01:23:25 +05:30
::MergeRequests::MarkReviewerReviewedService.new(project: project, current_user: current_user).execute(merge_request)
2021-03-11 19:13:27 +05:30
end
2021-10-27 15:23:28 +05:30
def capture_diff_note_positions(notes)
paths = notes.flat_map do |note|
note.diff_file&.paths if note.diff_note?
end
return if paths.empty?
capture_service = Discussions::CaptureDiffNotePositionService.new(merge_request, paths.compact)
notes.each do |note|
capture_service.execute(note.discussion) if note.diff_note? && note.start_of_discussion?
end
end
2021-11-11 11:23:49 +05:30
def keep_around_commits(notes)
shas = notes.flat_map do |note|
note.shas if note.diff_note?
end.uniq
# We are allowing this since gitaly call will be created for each sha and
# even though they're unique, there will still be multiple Gitaly calls.
Gitlab::GitalyClient.allow_n_plus_1_calls do
project.repository.keep_around(*shas)
end
end
2020-06-23 00:09:42 +05:30
end
end