debian-mirror-gitlab/lib/api/notes.rb

150 lines
5.3 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
module API
class Notes < Grape::API
2017-08-17 22:00:37 +05:30
include PaginationParams
2014-09-02 18:07:02 +05:30
before { authenticate! }
2017-08-17 22:00:37 +05:30
NOTEABLE_TYPES = [Issue, MergeRequest, Snippet].freeze
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
params do
requires :id, type: String, desc: 'The ID of a project'
end
resource :projects, requirements: { id: %r{[^/]+} } do
2014-09-02 18:07:02 +05:30
NOTEABLE_TYPES.each do |noteable_type|
noteables_str = noteable_type.to_s.underscore.pluralize
2017-08-17 22:00:37 +05:30
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'
use :pagination
end
get ":id/#{noteables_str}/:noteable_id/notes" do
noteable = find_project_noteable(noteables_str, params[:noteable_id])
if can?(current_user, noteable_read_ability_name(noteable), noteable)
2016-06-02 11:05:42 +05:30
# 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.
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-08-17 22:00:37 +05:30
paginate(noteable.notes).
2016-06-02 11:05:42 +05:30
reject { |n| n.cross_reference_not_visible_for?(current_user) }
present notes, with: Entities::Note
else
not_found!("Notes")
end
2014-09-02 18:07:02 +05:30
end
2017-08-17 22:00:37 +05:30
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
noteable = find_project_noteable(noteables_str, params[:noteable_id])
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)
2016-06-02 11:05:42 +05:30
if can_read_note
2017-08-17 22:00:37 +05:30
present note, with: Entities::Note
2016-06-02 11:05:42 +05:30
else
not_found!("Note")
end
2014-09-02 18:07:02 +05:30
end
2017-08-17 22:00:37 +05:30
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
noteable = find_project_noteable(noteables_str, params[:noteable_id])
2014-09-02 18:07:02 +05:30
opts = {
2017-08-17 22:00:37 +05:30
note: params[:body],
noteable_type: noteables_str.classify,
noteable_id: noteable.id
2014-09-02 18:07:02 +05:30
}
2017-08-17 22:00:37 +05:30
if can?(current_user, noteable_read_ability_name(noteable), noteable)
if params[:created_at] && (current_user.admin? || user_project.owner == current_user)
opts[:created_at] = params[:created_at]
end
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
note = ::Notes::CreateService.new(user_project, current_user, opts).execute
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
if note.valid?
present note, with: Entities.const_get(note.class.name)
else
not_found!("Note #{note.errors.messages}")
end
2014-09-02 18:07:02 +05:30
else
2017-08-17 22:00:37 +05:30
not_found!("Note")
2014-09-02 18:07:02 +05:30
end
end
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
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-09-11 14:41:01 +05:30
note = user_project.notes.find(params[:note_id])
authorize! :admin_note, note
2015-04-26 12:48:37 +05:30
opts = {
2015-09-11 14:41:01 +05:30
note: params[:body]
2015-04-26 12:48:37 +05:30
}
2017-08-17 22:00:37 +05:30
note = ::Notes::UpdateService.new(user_project, current_user, opts).execute(note)
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
if note.valid?
present note, with: Entities::Note
2015-04-26 12:48:37 +05:30
else
render_api_error!("Failed to save note #{note.errors.messages}", 400)
end
end
2017-08-17 22:00:37 +05:30
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-06-02 11:05:42 +05:30
note = user_project.notes.find(params[:note_id])
authorize! :admin_note, note
2017-08-17 22:00:37 +05:30
::Notes::DestroyService.new(user_project, current_user).execute(note)
2016-06-02 11:05:42 +05:30
end
end
end
helpers do
2017-08-17 22:00:37 +05:30
def find_project_noteable(noteables_str, noteable_id)
public_send("find_project_#{noteables_str.singularize}", noteable_id)
end
2016-06-02 11:05:42 +05:30
def noteable_read_ability_name(noteable)
2016-06-22 15:30:34 +05:30
"read_#{noteable.class.to_s.underscore}".to_sym
2014-09-02 18:07:02 +05:30
end
end
end
end