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) { ...@@ -55,6 +55,7 @@ export function getNoteFormData(params) {
note_project_id: '', note_project_id: '',
target_type: noteableData.targetType, target_type: noteableData.targetType,
target_id: noteableData.id, target_id: noteableData.id,
return_discussion: 'true',
note: { note: {
note, note,
position, position,
......
...@@ -43,12 +43,26 @@ module NotesActions ...@@ -43,12 +43,26 @@ module NotesActions
@note = Notes::CreateService.new(note_project, current_user, create_params).execute @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| 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 } format.html { redirect_back_or_default }
end end
end end
...@@ -57,10 +71,7 @@ module NotesActions ...@@ -57,10 +71,7 @@ module NotesActions
# rubocop:disable Gitlab/ModuleWithInstanceVariables # rubocop:disable Gitlab/ModuleWithInstanceVariables
def update def update
@note = Notes::UpdateService.new(project, current_user, note_params).execute(note) @note = Notes::UpdateService.new(project, current_user, note_params).execute(note)
prepare_notes_for_rendering([@note])
if @note.is_a?(Note)
prepare_notes_for_rendering([@note])
end
respond_to do |format| respond_to do |format|
format.json { render json: note_json(@note) } format.json { render json: note_json(@note) }
...@@ -91,14 +102,17 @@ module NotesActions ...@@ -91,14 +102,17 @@ module NotesActions
end end
def note_json(note) def note_json(note)
attrs = { attrs = {}
commands_changes: note.commands_changes
}
if note.persisted? if note.persisted?
attrs[:valid] = true 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)) attrs.merge!(note_serializer.represent(note))
else else
attrs.merge!( attrs.merge!(
...@@ -218,6 +232,10 @@ module NotesActions ...@@ -218,6 +232,10 @@ module NotesActions
ProjectNoteSerializer.new(project: project, noteable: noteable, current_user: current_user) ProjectNoteSerializer.new(project: project, noteable: noteable, current_user: current_user)
end end
def discussion_serializer
DiscussionSerializer.new(project: project, noteable: noteable, current_user: current_user, note_entity: ProjectNoteEntity)
end
def note_project def note_project
strong_memoize(:note_project) do strong_memoize(:note_project) do
next nil unless project next nil unless project
...@@ -237,6 +255,10 @@ module NotesActions ...@@ -237,6 +255,10 @@ module NotesActions
end end
end end
def return_discussion?
Gitlab::Utils.to_boolean(params[:return_discussion])
end
def use_note_serializer? def use_note_serializer?
return false if params['html'] return false if params['html']
......
...@@ -207,6 +207,14 @@ describe Projects::NotesController do ...@@ -207,6 +207,14 @@ describe Projects::NotesController do
expect(response).to have_gitlab_http_status(200) expect(response).to have_gitlab_http_status(200)
end 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 context 'when merge_request_diff_head_sha present' do
before do before do
service_params = { 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