1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# frozen_string_literal: true
class Projects::MergeRequests::DraftsController < Projects::MergeRequests::ApplicationController
include Gitlab::Utils::StrongMemoize
respond_to :json
before_action :authorize_create_note!, only: [:create, :publish]
before_action :authorize_admin_draft!, only: [:update, :destroy]
before_action :authorize_admin_draft!, if: -> { action_name == 'publish' && params[:id].present? }
urgency :low, [
:create,
:update,
:destroy,
:publish
]
def index
drafts = prepare_notes_for_rendering(draft_notes)
render json: DraftNoteSerializer.new(current_user: current_user).represent(drafts)
end
def create
create_params = draft_note_params.merge(in_reply_to_discussion_id: params[:in_reply_to_discussion_id])
create_service = DraftNotes::CreateService.new(merge_request, current_user, create_params)
draft_note = create_service.execute
if draft_note.errors.present?
render json: { errors: draft_note.errors.full_messages.to_sentence }, status: :unprocessable_entity
return
end
prepare_notes_for_rendering(draft_note)
render json: DraftNoteSerializer.new(current_user: current_user).represent(draft_note)
end
def update
if draft_note.update(draft_note_params)
prepare_notes_for_rendering(draft_note)
render json: DraftNoteSerializer.new(current_user: current_user).represent(draft_note)
else
render json: { errors: draft_note.errors.full_messages.to_sentence }, status: :unprocessable_entity
end
end
def destroy
DraftNotes::DestroyService.new(merge_request, current_user).execute(draft_note)
head :ok
end
def publish
result = DraftNotes::PublishService.new(merge_request, current_user).execute(draft_note(allow_nil: true))
if create_note_params[:note]
::Notes::CreateService.new(@project, current_user, create_note_params).execute
merge_request_activity_counter.track_submit_review_comment(user: current_user)
end
if Feature.enabled?(:mr_request_changes, current_user) && reviewer_state_params[:reviewer_state]
update_reviewer_state
elsif Gitlab::Utils.to_boolean(approve_params[:approve])
unless merge_request.approved_by?(current_user)
success = ::MergeRequests::ApprovalService
.new(project: @project, current_user: current_user, params: approve_params)
.execute(merge_request)
unless success
return render json: { message: _('An error occurred while approving, please try again.') },
status: :internal_server_error
end
end
merge_request_activity_counter.track_submit_review_approve(user: current_user)
end
if result[:status] == :success
head :ok
else
render json: { message: result[:message] }, status: :internal_server_error
end
end
def discard
DraftNotes::DestroyService.new(merge_request, current_user).execute
head :ok
end
private
def draft_note(allow_nil: false)
strong_memoize(:draft_note) do
draft_notes.find(params[:id])
end
rescue ActiveRecord::RecordNotFound => e
# draft_note is allowed to be nil in #publish
raise e unless allow_nil
end
def draft_notes
return unless current_user
strong_memoize(:draft_notes) do
merge_request.draft_notes.authored_by(current_user)
end
end
# rubocop: disable CodeReuse/ActiveRecord
def merge_request
@merge_request ||= MergeRequestsFinder
.new(current_user, project_id: @project.id)
.find_by!(iid: params[:merge_request_id])
end
# rubocop: enable CodeReuse/ActiveRecord
def draft_note_params
params.require(:draft_note).permit(
:commit_id,
:note,
:position,
:resolve_discussion,
:line_code
).tap do |h|
# Old FE version will still be sending `draft_note[commit_id]` as 'undefined'.
# That can result to having a note linked to a commit with 'undefined' ID
# which is non-existent.
h[:commit_id] = nil if h[:commit_id] == 'undefined'
end
end
def create_note_params
params.permit(
:note
).tap do |create_params|
create_params[:noteable_type] = merge_request.class.name
create_params[:noteable_id] = merge_request.id
end
end
def approve_params
params.permit(:approve)
end
def reviewer_state_params
params.permit(:reviewer_state)
end
def prepare_notes_for_rendering(notes)
return [] unless notes
notes = Array.wrap(notes)
# Preload author and access-level information
DraftNote.preload_author(notes)
user_ids = notes.map(&:author_id)
project.team.max_member_access_for_user_ids(user_ids)
notes.map { |note| render_draft_note(note) }
end
def render_draft_note(note)
params = { target_id: merge_request.id, target_type: 'MergeRequest', text: note.note }
result = PreviewMarkdownService.new(@project, current_user, params).execute
markdown_params = { markdown_engine: result[:markdown_engine], issuable_reference_expansion_enabled: true }
note.rendered_note = view_context.markdown(result[:text], markdown_params)
note.users_referenced = result[:users]
note.commands_changes = view_context.markdown(result[:commands])
note
end
def authorize_admin_draft!
access_denied! unless can?(current_user, :admin_note, draft_note)
end
def authorize_create_note!
access_denied! unless can?(current_user, :create_note, merge_request)
end
def merge_request_activity_counter
Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter
end
def update_reviewer_state
if reviewer_state_params[:reviewer_state] === 'approved'
::MergeRequests::ApprovalService
.new(project: @project, current_user: current_user, params: approve_params)
.execute(merge_request)
else
::MergeRequests::UpdateReviewerStateService
.new(project: @project, current_user: current_user)
.execute(merge_request, reviewer_state_params[:reviewer_state])
end
end
end
Projects::MergeRequests::DraftsController.prepend_mod
|