Commit 45c737f4 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'grapify-notes-api' into 'master'

Grapify the notes API

## What are the relevant issue numbers?

Related to #22928

See merge request !7375
parents 5f4ab1cd 5703d6af
...@@ -5,23 +5,23 @@ module API ...@@ -5,23 +5,23 @@ module API
NOTEABLE_TYPES = [Issue, MergeRequest, Snippet] NOTEABLE_TYPES = [Issue, MergeRequest, Snippet]
params do
requires :id, type: String, desc: 'The ID of a project'
end
resource :projects do resource :projects do
NOTEABLE_TYPES.each do |noteable_type| NOTEABLE_TYPES.each do |noteable_type|
noteables_str = noteable_type.to_s.underscore.pluralize noteables_str = noteable_type.to_s.underscore.pluralize
noteable_id_str = "#{noteable_type.to_s.underscore}_id"
desc 'Get a list of project +noteable+ notes' do
# Get a list of project +noteable+ notes success Entities::Note
# end
# Parameters: params do
# id (required) - The ID of a project requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
# noteable_id (required) - The ID of an issue or snippet end
# Example Request: get ":id/#{noteables_str}/:noteable_id/notes" do
# GET /projects/:id/issues/:noteable_id/notes noteable = user_project.send(noteables_str.to_sym).find(params[:noteable_id])
# GET /projects/:id/snippets/:noteable_id/notes
get ":id/#{noteables_str}/:#{noteable_id_str}/notes" do if can?(current_user, noteable_read_ability_name(noteable), noteable)
@noteable = user_project.send(noteables_str.to_sym).find(params[noteable_id_str.to_sym])
if can?(current_user, noteable_read_ability_name(@noteable), @noteable)
# We exclude notes that are cross-references and that cannot be viewed # We exclude notes that are cross-references and that cannot be viewed
# by the current user. By doing this exclusion at this level and not # by the current user. By doing this exclusion at this level and not
# at the DB query level (which we cannot in that case), the current # at the DB query level (which we cannot in that case), the current
...@@ -31,7 +31,7 @@ module API ...@@ -31,7 +31,7 @@ module API
# paginate() only works with a relation. This could lead to a # paginate() only works with a relation. This could lead to a
# mismatch between the pagination headers info and the actual notes # mismatch between the pagination headers info and the actual notes
# array returned, but this is really a edge-case. # array returned, but this is really a edge-case.
paginate(@noteable.notes). paginate(noteable.notes).
reject { |n| n.cross_reference_not_visible_for?(current_user) } reject { |n| n.cross_reference_not_visible_for?(current_user) }
present notes, with: Entities::Note present notes, with: Entities::Note
else else
...@@ -39,44 +39,40 @@ module API ...@@ -39,44 +39,40 @@ module API
end end
end end
# Get a single +noteable+ note desc 'Get a single +noteable+ note' do
# success Entities::Note
# Parameters: end
# id (required) - The ID of a project params do
# noteable_id (required) - The ID of an issue or snippet requires :note_id, type: Integer, desc: 'The ID of a note'
# note_id (required) - The ID of a note requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
# Example Request: end
# GET /projects/:id/issues/:noteable_id/notes/:note_id get ":id/#{noteables_str}/:noteable_id/notes/:note_id" do
# GET /projects/:id/snippets/:noteable_id/notes/:note_id noteable = user_project.send(noteables_str.to_sym).find(params[:noteable_id])
get ":id/#{noteables_str}/:#{noteable_id_str}/notes/:note_id" do note = noteable.notes.find(params[:note_id])
@noteable = user_project.send(noteables_str.to_sym).find(params[noteable_id_str.to_sym]) can_read_note = can?(current_user, noteable_read_ability_name(noteable), noteable) && !note.cross_reference_not_visible_for?(current_user)
@note = @noteable.notes.find(params[:note_id])
can_read_note = can?(current_user, noteable_read_ability_name(@noteable), @noteable) && !@note.cross_reference_not_visible_for?(current_user)
if can_read_note if can_read_note
present @note, with: Entities::Note present note, with: Entities::Note
else else
not_found!("Note") not_found!("Note")
end end
end end
# Create a new +noteable+ note desc 'Create a new +noteable+ note' do
# success Entities::Note
# Parameters: end
# id (required) - The ID of a project params do
# noteable_id (required) - The ID of an issue or snippet requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
# body (required) - The content of a note requires :body, type: String, desc: 'The content of a note'
# created_at (optional) - The date optional :created_at, type: String, desc: 'The creation date of the note'
# Example Request: end
# POST /projects/:id/issues/:noteable_id/notes post ":id/#{noteables_str}/:noteable_id/notes" do
# POST /projects/:id/snippets/:noteable_id/notes
post ":id/#{noteables_str}/:#{noteable_id_str}/notes" do
required_attributes! [:body] required_attributes! [:body]
opts = { opts = {
note: params[:body], note: params[:body],
noteable_type: noteables_str.classify, noteable_type: noteables_str.classify,
noteable_id: params[noteable_id_str] noteable_id: params[:noteable_id]
} }
if params[:created_at] && (current_user.is_admin? || user_project.owner == current_user) if params[:created_at] && (current_user.is_admin? || user_project.owner == current_user)
...@@ -92,19 +88,15 @@ module API ...@@ -92,19 +88,15 @@ module API
end end
end end
# Modify existing +noteable+ note desc 'Update an existing +noteable+ note' do
# success Entities::Note
# Parameters: end
# id (required) - The ID of a project params do
# noteable_id (required) - The ID of an issue or snippet requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
# node_id (required) - The ID of a note requires :note_id, type: Integer, desc: 'The ID of a note'
# body (required) - New content of a note requires :body, type: String, desc: 'The content of a note'
# Example Request: end
# PUT /projects/:id/issues/:noteable_id/notes/:note_id put ":id/#{noteables_str}/:noteable_id/notes/:note_id" do
# PUT /projects/:id/snippets/:noteable_id/notes/:node_id
put ":id/#{noteables_str}/:#{noteable_id_str}/notes/:note_id" do
required_attributes! [:body]
note = user_project.notes.find(params[:note_id]) note = user_project.notes.find(params[:note_id])
authorize! :admin_note, note authorize! :admin_note, note
...@@ -113,25 +105,23 @@ module API ...@@ -113,25 +105,23 @@ module API
note: params[:body] note: params[:body]
} }
@note = ::Notes::UpdateService.new(user_project, current_user, opts).execute(note) note = ::Notes::UpdateService.new(user_project, current_user, opts).execute(note)
if @note.valid? if note.valid?
present @note, with: Entities::Note present note, with: Entities::Note
else else
render_api_error!("Failed to save note #{note.errors.messages}", 400) render_api_error!("Failed to save note #{note.errors.messages}", 400)
end end
end end
# Delete a +noteable+ note desc 'Delete a +noteable+ note' do
# success Entities::Note
# Parameters: end
# id (required) - The ID of a project params do
# noteable_id (required) - The ID of an issue, MR, or snippet requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
# node_id (required) - The ID of a note requires :note_id, type: Integer, desc: 'The ID of a note'
# Example Request: end
# DELETE /projects/:id/issues/:noteable_id/notes/:note_id delete ":id/#{noteables_str}/:noteable_id/notes/:note_id" do
# DELETE /projects/:id/snippets/:noteable_id/notes/:node_id
delete ":id/#{noteables_str}/:#{noteable_id_str}/notes/:note_id" do
note = user_project.notes.find(params[:note_id]) note = user_project.notes.find(params[:note_id])
authorize! :admin_note, note authorize! :admin_note, note
......
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