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

122 lines
4.8 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module API
class Notes < Grape::API
2017-08-17 22:00:37 +05:30
include PaginationParams
2018-03-27 19:54:05 +05:30
helpers ::API::Helpers::NotesHelpers
2017-08-17 22:00:37 +05:30
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
2018-03-27 19:54:05 +05:30
NOTEABLE_TYPES.each do |noteable_type|
parent_type = noteable_type.parent_class.to_s.underscore
noteables_str = noteable_type.to_s.underscore.pluralize
params do
requires :id, type: String, desc: "The ID of a #{parent_type}"
end
2019-01-03 12:48:30 +05:30
resource parent_type.pluralize.to_sym, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
2014-09-02 18:07:02 +05:30
noteables_str = noteable_type.to_s.underscore.pluralize
2017-08-17 22:00:37 +05:30
2018-03-27 19:54:05 +05:30
desc "Get a list of #{noteable_type.to_s.downcase} notes" do
2017-08-17 22:00:37 +05:30
success Entities::Note
end
params do
requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
2018-03-17 18:26:18 +05:30
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.'
2017-08-17 22:00:37 +05:30
use :pagination
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
get ":id/#{noteables_str}/:noteable_id/notes" do
2018-03-27 19:54:05 +05:30
noteable = find_noteable(parent_type, noteables_str, params[:noteable_id])
2017-08-17 22:00:37 +05:30
2018-10-15 14:42:47 +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.
raw_notes = noteable.notes.with_metadata.reorder(params[:order_by] => params[:sort])
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.
paginate(raw_notes)
.reject { |n| n.cross_reference_not_visible_for?(current_user) }
present notes, with: Entities::Note
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
2018-03-27 19:54:05 +05:30
desc "Get a single #{noteable_type.to_s.downcase} note" do
2017-08-17 22:00:37 +05:30
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
2018-03-27 19:54:05 +05:30
noteable = find_noteable(parent_type, noteables_str, params[:noteable_id])
get_note(noteable, params[:note_id])
2014-09-02 18:07:02 +05:30
end
2018-03-27 19:54:05 +05:30
desc "Create a new #{noteable_type.to_s.downcase} note" do
2017-08-17 22:00:37 +05:30
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
2018-03-27 19:54:05 +05:30
noteable = find_noteable(parent_type, 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,
2018-03-27 19:54:05 +05:30
noteable_id: noteable.id,
created_at: params[:created_at]
2014-09-02 18:07:02 +05:30
}
2018-03-27 19:54:05 +05:30
note = create_note(noteable, opts)
2018-03-17 18:26:18 +05:30
2018-03-27 19:54:05 +05:30
if note.valid?
present note, with: Entities.const_get(note.class.name)
2014-09-02 18:07:02 +05:30
else
2018-03-27 19:54:05 +05:30
bad_request!("Note #{note.errors.messages}")
2014-09-02 18:07:02 +05:30
end
end
2015-04-26 12:48:37 +05:30
2018-03-27 19:54:05 +05:30
desc "Update an existing #{noteable_type.to_s.downcase} note" do
2017-08-17 22:00:37 +05:30
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
2018-03-27 19:54:05 +05:30
noteable = find_noteable(parent_type, noteables_str, params[:noteable_id])
2015-09-11 14:41:01 +05:30
2018-03-27 19:54:05 +05:30
update_note(noteable, params[:note_id])
2015-04-26 12:48:37 +05:30
end
2018-03-27 19:54:05 +05:30
desc "Delete a #{noteable_type.to_s.downcase} note" do
2017-08-17 22:00:37 +05:30
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
2018-03-27 19:54:05 +05:30
noteable = find_noteable(parent_type, noteables_str, params[:noteable_id])
2016-06-02 11:05:42 +05:30
2018-03-27 19:54:05 +05:30
delete_note(noteable, params[:note_id])
2016-06-02 11:05:42 +05:30
end
end
end
2014-09-02 18:07:02 +05:30
end
end