2013-05-14 08:33:31 -04:00
|
|
|
module API
|
2012-11-27 14:43:39 -05:00
|
|
|
class Notes < Grape::API
|
2016-12-04 12:11:19 -05:00
|
|
|
include PaginationParams
|
|
|
|
|
2012-11-27 14:43:39 -05:00
|
|
|
before { authenticate! }
|
|
|
|
|
2017-02-21 18:32:18 -05:00
|
|
|
NOTEABLE_TYPES = [Issue, MergeRequest, Snippet].freeze
|
2012-11-27 14:43:39 -05:00
|
|
|
|
2016-11-09 09:26:27 -05:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2017-08-31 07:44:49 -04:00
|
|
|
resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
|
2012-11-27 14:43:39 -05:00
|
|
|
NOTEABLE_TYPES.each do |noteable_type|
|
|
|
|
noteables_str = noteable_type.to_s.underscore.pluralize
|
2016-11-09 09:26:27 -05:00
|
|
|
|
|
|
|
desc 'Get a list of project +noteable+ notes' do
|
|
|
|
success Entities::Note
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
|
2017-11-29 11:22:22 -05:00
|
|
|
optional :order_by, type: String, values: %w[created_at updated_at], default: 'created_at',
|
|
|
|
desc: 'Return notes ordered by `created_at` or `updated_at` fields.'
|
|
|
|
optional :sort, type: String, values: %w[asc desc], default: 'desc',
|
|
|
|
desc: 'Return notes sorted in `asc` or `desc` order.'
|
2016-12-04 12:11:19 -05:00
|
|
|
use :pagination
|
2016-11-09 09:26:27 -05:00
|
|
|
end
|
|
|
|
get ":id/#{noteables_str}/:noteable_id/notes" do
|
2017-03-27 09:01:45 -04:00
|
|
|
noteable = find_project_noteable(noteables_str, params[:noteable_id])
|
2016-11-09 09:26:27 -05:00
|
|
|
|
|
|
|
if can?(current_user, noteable_read_ability_name(noteable), noteable)
|
2016-05-09 18:35:37 -04:00
|
|
|
# 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
|
|
|
|
# at the DB query level (which we cannot in that case), the current
|
|
|
|
# page can have less elements than :per_page even if
|
|
|
|
# there's more than one page.
|
2017-11-29 11:22:22 -05:00
|
|
|
raw_notes = noteable.notes.with_metadata.reorder(params[:order_by] => params[:sort])
|
2016-05-09 18:35:37 -04:00
|
|
|
notes =
|
|
|
|
# paginate() only works with a relation. This could lead to a
|
|
|
|
# mismatch between the pagination headers info and the actual notes
|
|
|
|
# array returned, but this is really a edge-case.
|
2017-11-29 11:22:22 -05:00
|
|
|
paginate(raw_notes)
|
2017-06-21 09:48:12 -04:00
|
|
|
.reject { |n| n.cross_reference_not_visible_for?(current_user) }
|
2016-05-09 18:35:37 -04:00
|
|
|
present notes, with: Entities::Note
|
|
|
|
else
|
2016-05-10 15:06:02 -04:00
|
|
|
not_found!("Notes")
|
2016-05-09 18:35:37 -04:00
|
|
|
end
|
2012-11-27 14:43:39 -05:00
|
|
|
end
|
2012-11-29 14:33:41 -05:00
|
|
|
|
2016-11-09 09:26:27 -05:00
|
|
|
desc 'Get a single +noteable+ note' do
|
|
|
|
success Entities::Note
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :note_id, type: Integer, desc: 'The ID of a note'
|
|
|
|
requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
|
|
|
|
end
|
|
|
|
get ":id/#{noteables_str}/:noteable_id/notes/:note_id" do
|
2017-03-27 09:01:45 -04:00
|
|
|
noteable = find_project_noteable(noteables_str, params[:noteable_id])
|
2017-11-21 08:31:23 -05:00
|
|
|
note = noteable.notes.with_metadata.find(params[:note_id])
|
2016-11-09 09:26:27 -05:00
|
|
|
can_read_note = can?(current_user, noteable_read_ability_name(noteable), noteable) && !note.cross_reference_not_visible_for?(current_user)
|
2016-01-13 13:42:36 -05:00
|
|
|
|
2016-05-16 15:43:19 -04:00
|
|
|
if can_read_note
|
2016-11-09 09:26:27 -05:00
|
|
|
present note, with: Entities::Note
|
2016-05-16 15:43:19 -04:00
|
|
|
else
|
|
|
|
not_found!("Note")
|
2016-01-13 13:42:36 -05:00
|
|
|
end
|
2012-11-29 14:33:41 -05:00
|
|
|
end
|
2012-11-29 15:06:24 -05:00
|
|
|
|
2016-11-09 09:26:27 -05:00
|
|
|
desc 'Create a new +noteable+ note' do
|
|
|
|
success Entities::Note
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
|
|
|
|
requires :body, type: String, desc: 'The content of a note'
|
|
|
|
optional :created_at, type: String, desc: 'The creation date of the note'
|
|
|
|
end
|
|
|
|
post ":id/#{noteables_str}/:noteable_id/notes" do
|
2017-03-27 09:01:45 -04:00
|
|
|
noteable = find_project_noteable(noteables_str, params[:noteable_id])
|
|
|
|
|
2014-06-18 02:26:04 -04:00
|
|
|
opts = {
|
2017-01-19 16:00:47 -05:00
|
|
|
note: params[:body],
|
|
|
|
noteable_type: noteables_str.classify,
|
2017-03-27 09:01:45 -04:00
|
|
|
noteable_id: noteable.id
|
2014-06-18 02:26:04 -04:00
|
|
|
}
|
|
|
|
|
2017-01-19 16:00:47 -05:00
|
|
|
if can?(current_user, noteable_read_ability_name(noteable), noteable)
|
2017-09-01 08:03:57 -04:00
|
|
|
authorize! :create_note, noteable
|
|
|
|
|
2017-04-08 22:20:57 -04:00
|
|
|
if params[:created_at] && (current_user.admin? || user_project.owner == current_user)
|
2017-01-19 16:00:47 -05:00
|
|
|
opts[:created_at] = params[:created_at]
|
|
|
|
end
|
2012-11-29 15:06:24 -05:00
|
|
|
|
2017-01-19 16:00:47 -05:00
|
|
|
note = ::Notes::CreateService.new(user_project, current_user, opts).execute
|
|
|
|
|
|
|
|
if note.valid?
|
2017-02-21 19:51:36 -05:00
|
|
|
present note, with: Entities.const_get(note.class.name)
|
2017-01-19 16:00:47 -05:00
|
|
|
else
|
|
|
|
not_found!("Note #{note.errors.messages}")
|
|
|
|
end
|
2014-06-10 10:56:35 -04:00
|
|
|
else
|
2017-01-19 16:00:47 -05:00
|
|
|
not_found!("Note")
|
2012-11-29 15:06:24 -05:00
|
|
|
end
|
|
|
|
end
|
2014-09-02 11:12:13 -04:00
|
|
|
|
2016-11-09 09:26:27 -05:00
|
|
|
desc 'Update an existing +noteable+ note' do
|
|
|
|
success Entities::Note
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
|
|
|
|
requires :note_id, type: Integer, desc: 'The ID of a note'
|
|
|
|
requires :body, type: String, desc: 'The content of a note'
|
|
|
|
end
|
|
|
|
put ":id/#{noteables_str}/:noteable_id/notes/:note_id" do
|
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
|
|
|
}
|
|
|
|
|
2016-11-09 09:26:27 -05:00
|
|
|
note = ::Notes::UpdateService.new(user_project, current_user, opts).execute(note)
|
2014-09-02 11:12:13 -04:00
|
|
|
|
2016-11-09 09:26:27 -05:00
|
|
|
if note.valid?
|
|
|
|
present note, with: Entities::Note
|
2014-09-02 11:12:13 -04:00
|
|
|
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
|
|
|
|
|
2016-11-09 09:26:27 -05:00
|
|
|
desc 'Delete a +noteable+ note' do
|
|
|
|
success Entities::Note
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
|
|
|
|
requires :note_id, type: Integer, desc: 'The ID of a note'
|
|
|
|
end
|
|
|
|
delete ":id/#{noteables_str}/:noteable_id/notes/:note_id" do
|
2016-04-05 19:21:02 -04:00
|
|
|
note = user_project.notes.find(params[:note_id])
|
2017-03-01 08:35:48 -05:00
|
|
|
|
2016-04-05 19:21:02 -04:00
|
|
|
authorize! :admin_note, note
|
2016-04-12 09:43:29 -04:00
|
|
|
|
2017-03-02 07:14:13 -05:00
|
|
|
destroy_conditionally!(note) do |note|
|
|
|
|
::Notes::DestroyService.new(user_project, current_user).execute(note)
|
|
|
|
end
|
2016-04-05 19:21:02 -04:00
|
|
|
end
|
2012-11-27 14:43:39 -05:00
|
|
|
end
|
|
|
|
end
|
2016-05-17 22:41:53 -04:00
|
|
|
|
|
|
|
helpers do
|
2017-03-27 09:01:45 -04:00
|
|
|
def find_project_noteable(noteables_str, noteable_id)
|
2017-08-03 22:20:34 -04:00
|
|
|
public_send("find_project_#{noteables_str.singularize}", noteable_id) # rubocop:disable GitlabSecurity/PublicSend
|
2017-03-27 09:01:45 -04:00
|
|
|
end
|
|
|
|
|
2016-05-17 22:41:53 -04:00
|
|
|
def noteable_read_ability_name(noteable)
|
2016-06-10 02:57:56 -04:00
|
|
|
"read_#{noteable.class.to_s.underscore}".to_sym
|
2016-05-17 22:41:53 -04:00
|
|
|
end
|
|
|
|
end
|
2012-11-27 14:43:39 -05:00
|
|
|
end
|
|
|
|
end
|