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

132 lines
5.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
2021-01-03 14:25:43 +05:30
class Notes < ::API::Base
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! }
2021-01-29 00:20:46 +05:30
Helpers::NotesHelpers.feature_category_per_noteable_type.each do |noteable_type, feature_category|
2018-03-27 19:54:05 +05:30
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-02-15 15:39:39 +05:30
resource parent_type.pluralize.to_sym, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
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.'
2020-03-13 15:44:24 +05:30
optional :activity_filter, type: String, values: UserPreference::NOTES_FILTERS.stringify_keys.keys, default: 'all_notes',
desc: 'The type of notables which are returned.'
2017-08-17 22:00:37 +05:30
use :pagination
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2021-01-29 00:20:46 +05:30
get ":id/#{noteables_str}/:noteable_id/notes", feature_category: feature_category do
2020-03-13 15:44:24 +05:30
noteable = find_noteable(noteable_type, 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.
2020-03-13 15:44:24 +05:30
notes_filter = UserPreference::NOTES_FILTERS[params[:activity_filter].to_sym]
raw_notes = noteable.notes.with_metadata.with_notes_filter(notes_filter).reorder(order_options_with_tie_breaker)
2019-12-04 20:38:33 +05:30
# 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.
notes = paginate(raw_notes)
notes = prepare_notes_for_rendering(notes)
2020-04-08 14:13:33 +05:30
notes = notes.select { |note| note.readable_by?(current_user) }
2018-10-15 14:42:47 +05:30
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
2021-01-29 00:20:46 +05:30
get ":id/#{noteables_str}/:noteable_id/notes/:note_id", feature_category: feature_category do
2020-03-13 15:44:24 +05:30
noteable = find_noteable(noteable_type, params[:noteable_id])
2018-03-27 19:54:05 +05:30
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'
2020-07-28 23:09:34 +05:30
optional :confidential, type: Boolean, desc: 'Confidentiality note flag, default is false'
2017-08-17 22:00:37 +05:30
optional :created_at, type: String, desc: 'The creation date of the note'
end
2021-01-29 00:20:46 +05:30
post ":id/#{noteables_str}/:noteable_id/notes", feature_category: feature_category do
2021-03-11 19:13:27 +05:30
allowlist =
Gitlab::CurrentSettings.current_application_settings.notes_create_limit_allowlist
check_rate_limit! :notes_create, [current_user], allowlist
2020-03-13 15:44:24 +05:30
noteable = find_noteable(noteable_type, 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,
2020-07-28 23:09:34 +05:30
confidential: params[:confidential],
2018-03-27 19:54:05 +05:30
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
2021-11-18 22:05:49 +05:30
if note.errors.attribute_names == [:commands_only]
2020-04-22 19:07:51 +05:30
status 202
present note, with: Entities::NoteCommands
elsif note.valid?
2019-12-21 20:55:43 +05:30
present note, with: Entities.const_get(note.class.name, false)
2014-09-02 18:07:02 +05:30
else
2020-04-22 19:07:51 +05:30
note.errors.delete(:commands_only) if note.errors.has_key?(:commands)
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'
2020-10-24 23:57:45 +05:30
optional :body, type: String, allow_blank: false, desc: 'The content of a note'
optional :confidential, type: Boolean, desc: 'Confidentiality note flag'
2017-08-17 22:00:37 +05:30
end
2021-01-29 00:20:46 +05:30
put ":id/#{noteables_str}/:noteable_id/notes/:note_id", feature_category: feature_category do
2020-03-13 15:44:24 +05:30
noteable = find_noteable(noteable_type, 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
2020-03-13 15:44:24 +05:30
noteable = find_noteable(noteable_type, 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