2013-05-14 08:33:31 -04:00
|
|
|
module API
|
2012-11-27 14:43:39 -05:00
|
|
|
# Notes API
|
|
|
|
class Notes < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
|
2013-01-30 00:15:13 -05:00
|
|
|
NOTEABLE_TYPES = [Issue, MergeRequest, Snippet]
|
2012-11-27 14:43:39 -05:00
|
|
|
|
|
|
|
resource :projects do
|
|
|
|
NOTEABLE_TYPES.each do |noteable_type|
|
|
|
|
noteables_str = noteable_type.to_s.underscore.pluralize
|
|
|
|
noteable_id_str = "#{noteable_type.to_s.underscore}_id"
|
|
|
|
|
|
|
|
# Get a list of project +noteable+ notes
|
|
|
|
#
|
|
|
|
# Parameters:
|
2012-12-21 12:47:04 -05:00
|
|
|
# id (required) - The ID of a project
|
2012-11-27 14:43:39 -05:00
|
|
|
# noteable_id (required) - The ID of an issue or snippet
|
|
|
|
# Example Request:
|
2012-11-29 14:21:14 -05:00
|
|
|
# GET /projects/:id/issues/:noteable_id/notes
|
|
|
|
# GET /projects/:id/snippets/:noteable_id/notes
|
2012-11-27 14:43:39 -05:00
|
|
|
get ":id/#{noteables_str}/:#{noteable_id_str}/notes" do
|
|
|
|
@noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"])
|
|
|
|
present paginate(@noteable.notes), with: Entities::Note
|
|
|
|
end
|
2012-11-29 14:33:41 -05:00
|
|
|
|
|
|
|
# Get a single +noteable+ note
|
|
|
|
#
|
|
|
|
# Parameters:
|
2012-12-21 12:47:04 -05:00
|
|
|
# id (required) - The ID of a project
|
2012-11-29 14:33:41 -05:00
|
|
|
# noteable_id (required) - The ID of an issue or snippet
|
|
|
|
# note_id (required) - The ID of a note
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/issues/:noteable_id/notes/:note_id
|
|
|
|
# GET /projects/:id/snippets/:noteable_id/notes/:note_id
|
|
|
|
get ":id/#{noteables_str}/:#{noteable_id_str}/notes/:note_id" do
|
|
|
|
@noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"])
|
|
|
|
@note = @noteable.notes.find(params[:note_id])
|
|
|
|
present @note, with: Entities::Note
|
|
|
|
end
|
2012-11-29 15:06:24 -05:00
|
|
|
|
|
|
|
# Create a new +noteable+ note
|
|
|
|
#
|
|
|
|
# Parameters:
|
2012-12-21 12:47:04 -05:00
|
|
|
# id (required) - The ID of a project
|
2012-11-29 15:06:24 -05:00
|
|
|
# noteable_id (required) - The ID of an issue or snippet
|
|
|
|
# body (required) - The content of a note
|
|
|
|
# Example Request:
|
|
|
|
# POST /projects/:id/issues/:noteable_id/notes
|
|
|
|
# POST /projects/:id/snippets/:noteable_id/notes
|
|
|
|
post ":id/#{noteables_str}/:#{noteable_id_str}/notes" do
|
2014-06-10 10:56:35 -04:00
|
|
|
required_attributes! [:body]
|
2013-02-20 16:17:05 -05:00
|
|
|
|
2014-06-18 02:26:04 -04:00
|
|
|
opts = {
|
|
|
|
note: params[:body],
|
|
|
|
noteable_type: noteables_str.classify,
|
|
|
|
noteable_id: params[noteable_id_str]
|
|
|
|
}
|
|
|
|
|
|
|
|
@note = ::Notes::CreateService.new(user_project, current_user, opts).execute
|
2012-11-29 15:06:24 -05:00
|
|
|
|
2014-06-18 02:26:04 -04:00
|
|
|
if @note.valid?
|
2014-06-10 10:56:35 -04:00
|
|
|
present @note, with: Entities::Note
|
|
|
|
else
|
2014-12-30 09:17:46 -05:00
|
|
|
not_found!("Note #{@note.errors.messages}")
|
2012-11-29 15:06:24 -05:00
|
|
|
end
|
|
|
|
end
|
2014-09-02 11:12:13 -04:00
|
|
|
|
|
|
|
# Modify existing +noteable+ note
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# noteable_id (required) - The ID of an issue or snippet
|
|
|
|
# node_id (required) - The ID of a note
|
|
|
|
# body (required) - New content of a note
|
|
|
|
# Example Request:
|
|
|
|
# PUT /projects/:id/issues/:noteable_id/notes/:note_id
|
|
|
|
# PUT /projects/:id/snippets/:noteable_id/notes/:node_id
|
|
|
|
put ":id/#{noteables_str}/:#{noteable_id_str}/notes/:note_id" do
|
|
|
|
required_attributes! [:body]
|
|
|
|
|
2015-07-30 08:45:54 -04:00
|
|
|
note = user_project.notes.find(params[:note_id])
|
|
|
|
|
|
|
|
authorize! :admin_note, note
|
2014-09-02 11:12:13 -04:00
|
|
|
|
|
|
|
opts = {
|
2015-07-30 08:45:54 -04:00
|
|
|
note: params[:body]
|
2014-09-02 11:12:13 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 08:45:54 -04:00
|
|
|
@note = ::Notes::UpdateService.new(user_project, current_user, opts).execute(note)
|
2014-09-02 11:12:13 -04:00
|
|
|
|
|
|
|
if @note.valid?
|
|
|
|
present @note, with: Entities::Note
|
|
|
|
else
|
2015-01-07 04:46:00 -05:00
|
|
|
render_api_error!("Failed to save note #{note.errors.messages}", 400)
|
2014-09-02 11:12:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-27 14:43:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|