Commit 2036458e authored by Douwe Maan's avatar Douwe Maan Committed by André Luís

Return discussion object from NotesController#create when return_discussion param is set

parent a2936404
......@@ -55,6 +55,7 @@ export function getNoteFormData(params) {
note_project_id: '',
target_type: noteableData.targetType,
target_id: noteableData.id,
return_discussion: 'true',
note: {
note,
position,
......
......@@ -43,12 +43,26 @@ module NotesActions
@note = Notes::CreateService.new(note_project, current_user, create_params).execute
if @note.is_a?(Note)
prepare_notes_for_rendering([@note], noteable)
end
respond_to do |format|
format.json { render json: note_json(@note) }
format.json do
json = {
commands_changes: @note.commands_changes
}
if @note.persisted? && return_discussion?
json[:valid] = true
discussion = @note.discussion
prepare_notes_for_rendering(discussion.notes)
json[:discussion] = discussion_serializer.represent(discussion, context: self)
else
prepare_notes_for_rendering([@note])
json.merge!(note_json(@note))
end
render json: json
end
format.html { redirect_back_or_default }
end
end
......@@ -57,10 +71,7 @@ module NotesActions
# rubocop:disable Gitlab/ModuleWithInstanceVariables
def update
@note = Notes::UpdateService.new(project, current_user, note_params).execute(note)
if @note.is_a?(Note)
prepare_notes_for_rendering([@note])
end
prepare_notes_for_rendering([@note])
respond_to do |format|
format.json { render json: note_json(@note) }
......@@ -91,14 +102,17 @@ module NotesActions
end
def note_json(note)
attrs = {
commands_changes: note.commands_changes
}
attrs = {}
if note.persisted?
attrs[:valid] = true
if use_note_serializer?
if return_discussion?
discussion = note.discussion
prepare_notes_for_rendering(discussion.notes)
attrs[:discussion] = discussion_serializer.represent(discussion, context: self)
elsif use_note_serializer?
attrs.merge!(note_serializer.represent(note))
else
attrs.merge!(
......@@ -218,6 +232,10 @@ module NotesActions
ProjectNoteSerializer.new(project: project, noteable: noteable, current_user: current_user)
end
def discussion_serializer
DiscussionSerializer.new(project: project, noteable: noteable, current_user: current_user, note_entity: ProjectNoteEntity)
end
def note_project
strong_memoize(:note_project) do
next nil unless project
......@@ -237,6 +255,10 @@ module NotesActions
end
end
def return_discussion?
Gitlab::Utils.to_boolean(params[:return_discussion])
end
def use_note_serializer?
return false if params['html']
......
......@@ -207,6 +207,14 @@ describe Projects::NotesController do
expect(response).to have_gitlab_http_status(200)
end
it 'returns discussion JSON when the return_discussion param is set' do
post :create, request_params.merge(format: :json, return_discussion: 'true')
expect(response).to have_gitlab_http_status(200)
expect(json_response).to have_key 'discussion'
expect(json_response['discussion']['notes'][0]['note']).to eq(request_params[:note][:note])
end
context 'when merge_request_diff_head_sha present' do
before do
service_params = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment