2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-28 02:48:23 -05:00
|
|
|
module API
|
2020-10-14 20:08:42 -04:00
|
|
|
class Discussions < ::API::Base
|
2018-02-28 02:48:23 -05:00
|
|
|
include PaginationParams
|
|
|
|
helpers ::API::Helpers::NotesHelpers
|
2019-08-15 00:50:52 -04:00
|
|
|
helpers ::RendersNotes
|
2018-02-28 02:48:23 -05:00
|
|
|
|
|
|
|
before { authenticate! }
|
|
|
|
|
2020-10-29 14:09:11 -04:00
|
|
|
Helpers::DiscussionsHelpers.feature_category_per_noteable_type.each do |noteable_type, feature_category|
|
2018-02-28 02:48:23 -05:00
|
|
|
parent_type = noteable_type.parent_class.to_s.underscore
|
|
|
|
noteables_str = noteable_type.to_s.underscore.pluralize
|
2018-05-01 08:39:44 -04:00
|
|
|
noteables_path = noteable_type == Commit ? "repository/#{noteables_str}" : noteables_str
|
2018-02-28 02:48:23 -05:00
|
|
|
|
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: "The ID of a #{parent_type}"
|
|
|
|
end
|
2018-11-08 07:18:17 -05:00
|
|
|
resource parent_type.pluralize.to_sym, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2018-02-28 02:48:23 -05:00
|
|
|
desc "Get a list of #{noteable_type.to_s.downcase} discussions" do
|
|
|
|
success Entities::Discussion
|
|
|
|
end
|
|
|
|
params do
|
2018-05-01 08:39:44 -04:00
|
|
|
requires :noteable_id, types: [Integer, String], desc: 'The ID of the noteable'
|
2018-02-28 02:48:23 -05:00
|
|
|
use :pagination
|
|
|
|
end
|
2019-08-15 00:50:52 -04:00
|
|
|
|
2020-10-29 14:09:11 -04:00
|
|
|
get ":id/#{noteables_path}/:noteable_id/discussions", feature_category: feature_category do
|
2020-01-24 16:09:09 -05:00
|
|
|
noteable = find_noteable(noteable_type, params[:noteable_id])
|
2018-02-28 02:48:23 -05:00
|
|
|
|
2020-03-25 05:08:11 -04:00
|
|
|
discussion_ids = paginate(noteable.discussion_ids_relation)
|
|
|
|
notes = readable_discussion_notes(noteable, discussion_ids)
|
2018-02-28 02:48:23 -05:00
|
|
|
|
2020-03-25 05:08:11 -04:00
|
|
|
present Discussion.build_collection(notes, noteable), with: Entities::Discussion
|
2018-02-28 02:48:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
desc "Get a single #{noteable_type.to_s.downcase} discussion" do
|
|
|
|
success Entities::Discussion
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :discussion_id, type: String, desc: 'The ID of a discussion'
|
2018-05-01 08:39:44 -04:00
|
|
|
requires :noteable_id, types: [Integer, String], desc: 'The ID of the noteable'
|
2018-02-28 02:48:23 -05:00
|
|
|
end
|
2020-10-29 14:09:11 -04:00
|
|
|
get ":id/#{noteables_path}/:noteable_id/discussions/:discussion_id", feature_category: feature_category do
|
2020-01-24 16:09:09 -05:00
|
|
|
noteable = find_noteable(noteable_type, params[:noteable_id])
|
2018-02-28 02:48:23 -05:00
|
|
|
notes = readable_discussion_notes(noteable, params[:discussion_id])
|
|
|
|
|
2018-05-01 08:39:44 -04:00
|
|
|
if notes.empty?
|
2018-04-18 05:19:40 -04:00
|
|
|
break not_found!("Discussion")
|
2018-02-28 02:48:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
discussion = Discussion.build(notes, noteable)
|
|
|
|
|
|
|
|
present discussion, with: Entities::Discussion
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Create a new #{noteable_type.to_s.downcase} discussion" do
|
|
|
|
success Entities::Discussion
|
|
|
|
end
|
|
|
|
params do
|
2018-05-01 08:39:44 -04:00
|
|
|
requires :noteable_id, types: [Integer, String], desc: 'The ID of the noteable'
|
2018-02-28 02:48:23 -05:00
|
|
|
requires :body, type: String, desc: 'The content of a note'
|
|
|
|
optional :created_at, type: String, desc: 'The creation date of the note'
|
2018-05-01 08:39:44 -04:00
|
|
|
optional :position, type: Hash do
|
|
|
|
requires :base_sha, type: String, desc: 'Base commit SHA in the source branch'
|
|
|
|
requires :start_sha, type: String, desc: 'SHA referencing commit in target branch'
|
|
|
|
requires :head_sha, type: String, desc: 'SHA referencing HEAD of this merge request'
|
|
|
|
requires :position_type, type: String, desc: 'Type of the position reference', values: %w(text image)
|
|
|
|
optional :new_path, type: String, desc: 'File path after change'
|
|
|
|
optional :new_line, type: Integer, desc: 'Line number after change'
|
|
|
|
optional :old_path, type: String, desc: 'File path before change'
|
|
|
|
optional :old_line, type: Integer, desc: 'Line number before change'
|
|
|
|
optional :width, type: Integer, desc: 'Width of the image'
|
|
|
|
optional :height, type: Integer, desc: 'Height of the image'
|
|
|
|
optional :x, type: Integer, desc: 'X coordinate in the image'
|
|
|
|
optional :y, type: Integer, desc: 'Y coordinate in the image'
|
2020-04-16 05:09:37 -04:00
|
|
|
|
|
|
|
optional :line_range, type: Hash, desc: 'Multi-line start and end' do
|
2020-07-08 17:09:09 -04:00
|
|
|
optional :start, type: Hash do
|
|
|
|
optional :line_code, type: String, desc: 'Start line code for multi-line note'
|
|
|
|
optional :type, type: String, desc: 'Start line type for multi-line note'
|
|
|
|
optional :old_line, type: String, desc: 'Start old_line line number'
|
|
|
|
optional :new_line, type: String, desc: 'Start new_line line number'
|
|
|
|
end
|
|
|
|
optional :end, type: Hash do
|
|
|
|
optional :line_code, type: String, desc: 'End line code for multi-line note'
|
|
|
|
optional :type, type: String, desc: 'End line type for multi-line note'
|
|
|
|
optional :old_line, type: String, desc: 'End old_line line number'
|
|
|
|
optional :new_line, type: String, desc: 'End new_line line number'
|
|
|
|
end
|
2020-04-16 05:09:37 -04:00
|
|
|
end
|
2018-05-01 08:39:44 -04:00
|
|
|
end
|
2018-02-28 02:48:23 -05:00
|
|
|
end
|
2020-10-29 14:09:11 -04:00
|
|
|
post ":id/#{noteables_path}/:noteable_id/discussions", feature_category: feature_category do
|
2020-01-24 16:09:09 -05:00
|
|
|
noteable = find_noteable(noteable_type, params[:noteable_id])
|
2018-05-01 08:39:44 -04:00
|
|
|
type = params[:position] ? 'DiffNote' : 'DiscussionNote'
|
|
|
|
id_key = noteable.is_a?(Commit) ? :commit_id : :noteable_id
|
2018-02-28 02:48:23 -05:00
|
|
|
|
|
|
|
opts = {
|
|
|
|
note: params[:body],
|
|
|
|
created_at: params[:created_at],
|
2018-05-01 08:39:44 -04:00
|
|
|
type: type,
|
2018-02-28 02:48:23 -05:00
|
|
|
noteable_type: noteables_str.classify,
|
2018-05-01 08:39:44 -04:00
|
|
|
position: params[:position],
|
|
|
|
id_key => noteable.id
|
2018-02-28 02:48:23 -05:00
|
|
|
}
|
2020-12-15 19:09:58 -05:00
|
|
|
opts[:commit_id] = params[:commit_id] if noteable.is_a?(MergeRequest) && type == 'DiffNote'
|
2018-02-28 02:48:23 -05:00
|
|
|
|
|
|
|
note = create_note(noteable, opts)
|
|
|
|
|
|
|
|
if note.valid?
|
|
|
|
present note.discussion, with: Entities::Discussion
|
|
|
|
else
|
|
|
|
bad_request!("Note #{note.errors.messages}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Get comments in a single #{noteable_type.to_s.downcase} discussion" do
|
|
|
|
success Entities::Discussion
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :discussion_id, type: String, desc: 'The ID of a discussion'
|
2018-05-01 08:39:44 -04:00
|
|
|
requires :noteable_id, types: [Integer, String], desc: 'The ID of the noteable'
|
2018-02-28 02:48:23 -05:00
|
|
|
end
|
2020-10-29 14:09:11 -04:00
|
|
|
get ":id/#{noteables_path}/:noteable_id/discussions/:discussion_id/notes", feature_category: feature_category do
|
2020-01-24 16:09:09 -05:00
|
|
|
noteable = find_noteable(noteable_type, params[:noteable_id])
|
2018-02-28 02:48:23 -05:00
|
|
|
notes = readable_discussion_notes(noteable, params[:discussion_id])
|
|
|
|
|
2018-05-01 08:39:44 -04:00
|
|
|
if notes.empty?
|
2018-04-18 05:19:40 -04:00
|
|
|
break not_found!("Notes")
|
2018-02-28 02:48:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
present notes, with: Entities::Note
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Add a comment to a #{noteable_type.to_s.downcase} discussion" do
|
|
|
|
success Entities::Note
|
|
|
|
end
|
|
|
|
params do
|
2018-05-01 08:39:44 -04:00
|
|
|
requires :noteable_id, types: [Integer, String], desc: 'The ID of the noteable'
|
2018-02-28 02:48:23 -05:00
|
|
|
requires :discussion_id, type: String, desc: 'The ID of a discussion'
|
|
|
|
requires :body, type: String, desc: 'The content of a note'
|
|
|
|
optional :created_at, type: String, desc: 'The creation date of the note'
|
|
|
|
end
|
2020-10-29 14:09:11 -04:00
|
|
|
post ":id/#{noteables_path}/:noteable_id/discussions/:discussion_id/notes", feature_category: feature_category do
|
2020-01-24 16:09:09 -05:00
|
|
|
noteable = find_noteable(noteable_type, params[:noteable_id])
|
2018-02-28 02:48:23 -05:00
|
|
|
notes = readable_discussion_notes(noteable, params[:discussion_id])
|
2019-05-06 06:59:02 -04:00
|
|
|
first_note = notes.first
|
2018-02-28 02:48:23 -05:00
|
|
|
|
2018-04-18 05:19:40 -04:00
|
|
|
break not_found!("Discussion") if notes.empty?
|
2019-05-06 06:59:02 -04:00
|
|
|
|
|
|
|
unless first_note.part_of_discussion? || first_note.to_discussion.can_convert_to_discussion?
|
|
|
|
break bad_request!("Discussion can not be replied to.")
|
|
|
|
end
|
2018-02-28 02:48:23 -05:00
|
|
|
|
|
|
|
opts = {
|
|
|
|
note: params[:body],
|
|
|
|
type: 'DiscussionNote',
|
|
|
|
in_reply_to_discussion_id: params[:discussion_id],
|
|
|
|
created_at: params[:created_at]
|
|
|
|
}
|
|
|
|
note = create_note(noteable, opts)
|
|
|
|
|
|
|
|
if note.valid?
|
|
|
|
present note, with: Entities::Note
|
|
|
|
else
|
|
|
|
bad_request!("Note #{note.errors.messages}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Get a comment in a #{noteable_type.to_s.downcase} discussion" do
|
|
|
|
success Entities::Note
|
|
|
|
end
|
|
|
|
params do
|
2018-05-01 08:39:44 -04:00
|
|
|
requires :noteable_id, types: [Integer, String], desc: 'The ID of the noteable'
|
2018-02-28 02:48:23 -05:00
|
|
|
requires :discussion_id, type: String, desc: 'The ID of a discussion'
|
|
|
|
requires :note_id, type: Integer, desc: 'The ID of a note'
|
|
|
|
end
|
2020-10-29 14:09:11 -04:00
|
|
|
get ":id/#{noteables_path}/:noteable_id/discussions/:discussion_id/notes/:note_id", feature_category: feature_category do
|
2020-01-24 16:09:09 -05:00
|
|
|
noteable = find_noteable(noteable_type, params[:noteable_id])
|
2018-02-28 02:48:23 -05:00
|
|
|
|
|
|
|
get_note(noteable, params[:note_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Edit a comment in a #{noteable_type.to_s.downcase} discussion" do
|
|
|
|
success Entities::Note
|
|
|
|
end
|
|
|
|
params do
|
2018-05-01 08:39:44 -04:00
|
|
|
requires :noteable_id, types: [Integer, String], desc: 'The ID of the noteable'
|
2018-02-28 02:48:23 -05:00
|
|
|
requires :discussion_id, type: String, desc: 'The ID of a discussion'
|
|
|
|
requires :note_id, type: Integer, desc: 'The ID of a note'
|
2018-05-01 08:39:44 -04:00
|
|
|
optional :body, type: String, desc: 'The content of a note'
|
|
|
|
optional :resolved, type: Boolean, desc: 'Mark note resolved/unresolved'
|
|
|
|
exactly_one_of :body, :resolved
|
2018-02-28 02:48:23 -05:00
|
|
|
end
|
2020-10-29 14:09:11 -04:00
|
|
|
put ":id/#{noteables_path}/:noteable_id/discussions/:discussion_id/notes/:note_id", feature_category: feature_category do
|
2020-01-24 16:09:09 -05:00
|
|
|
noteable = find_noteable(noteable_type, params[:noteable_id])
|
2018-02-28 02:48:23 -05:00
|
|
|
|
2018-05-01 08:39:44 -04:00
|
|
|
if params[:resolved].nil?
|
|
|
|
update_note(noteable, params[:note_id])
|
|
|
|
else
|
|
|
|
resolve_note(noteable, params[:note_id], params[:resolved])
|
|
|
|
end
|
2018-02-28 02:48:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
desc "Delete a comment in a #{noteable_type.to_s.downcase} discussion" do
|
|
|
|
success Entities::Note
|
|
|
|
end
|
|
|
|
params do
|
2018-05-01 08:39:44 -04:00
|
|
|
requires :noteable_id, types: [Integer, String], desc: 'The ID of the noteable'
|
2018-02-28 02:48:23 -05:00
|
|
|
requires :discussion_id, type: String, desc: 'The ID of a discussion'
|
|
|
|
requires :note_id, type: Integer, desc: 'The ID of a note'
|
|
|
|
end
|
2020-10-29 14:09:11 -04:00
|
|
|
delete ":id/#{noteables_path}/:noteable_id/discussions/:discussion_id/notes/:note_id", feature_category: feature_category do
|
2020-01-24 16:09:09 -05:00
|
|
|
noteable = find_noteable(noteable_type, params[:noteable_id])
|
2018-02-28 02:48:23 -05:00
|
|
|
|
|
|
|
delete_note(noteable, params[:note_id])
|
|
|
|
end
|
2018-05-01 08:39:44 -04:00
|
|
|
|
2019-06-03 10:41:05 -04:00
|
|
|
if Noteable.resolvable_types.include?(noteable_type.to_s)
|
2018-05-01 08:39:44 -04:00
|
|
|
desc "Resolve/unresolve an existing #{noteable_type.to_s.downcase} discussion" do
|
|
|
|
success Entities::Discussion
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :noteable_id, types: [Integer, String], desc: 'The ID of the noteable'
|
|
|
|
requires :discussion_id, type: String, desc: 'The ID of a discussion'
|
|
|
|
requires :resolved, type: Boolean, desc: 'Mark discussion resolved/unresolved'
|
|
|
|
end
|
2020-10-29 14:09:11 -04:00
|
|
|
put ":id/#{noteables_path}/:noteable_id/discussions/:discussion_id", feature_category: feature_category do
|
2020-01-24 16:09:09 -05:00
|
|
|
noteable = find_noteable(noteable_type, params[:noteable_id])
|
2018-05-01 08:39:44 -04:00
|
|
|
|
|
|
|
resolve_discussion(noteable, params[:discussion_id], params[:resolved])
|
|
|
|
end
|
|
|
|
end
|
2018-02-28 02:48:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
helpers do
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2020-03-25 05:08:11 -04:00
|
|
|
def readable_discussion_notes(noteable, discussion_ids)
|
2018-02-28 02:48:23 -05:00
|
|
|
notes = noteable.notes
|
2020-03-25 05:08:11 -04:00
|
|
|
.where(discussion_id: discussion_ids)
|
2018-02-28 02:48:23 -05:00
|
|
|
.inc_relations_for_view
|
|
|
|
.includes(:noteable)
|
|
|
|
.fresh
|
|
|
|
|
2019-08-15 00:50:52 -04:00
|
|
|
# Without RendersActions#prepare_notes_for_rendering,
|
2020-02-23 22:09:05 -05:00
|
|
|
# Note#system_note_with_references_visible_for? will attempt to render
|
2019-08-15 00:50:52 -04:00
|
|
|
# Markdown references mentioned in the note to see whether they
|
|
|
|
# should be redacted. For notes that reference a commit, this
|
|
|
|
# would also incur a Gitaly call to verify the commit exists.
|
|
|
|
#
|
|
|
|
# With prepare_notes_for_rendering, we can avoid Gitaly calls
|
|
|
|
# because notes are redacted if they point to projects that
|
|
|
|
# cannot be accessed by the user.
|
|
|
|
notes = prepare_notes_for_rendering(notes)
|
2020-02-23 22:09:05 -05:00
|
|
|
notes.select { |n| n.readable_by?(current_user) }
|
2018-02-28 02:48:23 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-02-28 02:48:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|