BigW Consortium Gitlab

notes.rb 4.84 KB
Newer Older
1
module API
2
  class Notes < Grape::API
3
    include PaginationParams
Jan Provaznik committed
4
    helpers ::API::Helpers::NotesHelpers
5

6 7
    before { authenticate! }

8
    NOTEABLE_TYPES = [Issue, MergeRequest, Snippet].freeze
9

Jan Provaznik committed
10 11 12 13 14 15 16 17
    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
      resource parent_type.pluralize.to_sym, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
18
        noteables_str = noteable_type.to_s.underscore.pluralize
19

Jan Provaznik committed
20
        desc "Get a list of #{noteable_type.to_s.downcase} notes" do
21 22 23 24
          success Entities::Note
        end
        params do
          requires :noteable_id, type: Integer, desc: 'The ID of the noteable'
25 26 27 28
          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.'
29
          use :pagination
30 31
        end
        get ":id/#{noteables_str}/:noteable_id/notes" do
Jan Provaznik committed
32
          noteable = find_noteable(parent_type, noteables_str, params[:noteable_id])
33 34

          if can?(current_user, noteable_read_ability_name(noteable), noteable)
35 36 37 38 39
            # 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.
40
            raw_notes = noteable.notes.with_metadata.reorder(params[:order_by] => params[:sort])
41 42 43 44
            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.
45
              paginate(raw_notes)
46
              .reject { |n| n.cross_reference_not_visible_for?(current_user) }
47 48
            present notes, with: Entities::Note
          else
49
            not_found!("Notes")
50
          end
51
        end
Nihad Abbasov committed
52

Jan Provaznik committed
53
        desc "Get a single #{noteable_type.to_s.downcase} note" do
54 55 56 57 58 59 60
          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
Jan Provaznik committed
61 62
          noteable = find_noteable(parent_type, noteables_str, params[:noteable_id])
          get_note(noteable, params[:note_id])
Nihad Abbasov committed
63
        end
Nihad Abbasov committed
64

Jan Provaznik committed
65
        desc "Create a new #{noteable_type.to_s.downcase} note" do
66 67 68 69 70 71 72 73
          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
Jan Provaznik committed
74
          noteable = find_noteable(parent_type, noteables_str, params[:noteable_id])
75

76
          opts = {
77 78
            note: params[:body],
            noteable_type: noteables_str.classify,
Jan Provaznik committed
79 80
            noteable_id: noteable.id,
            created_at: params[:created_at]
81 82
          }

Jan Provaznik committed
83
          note = create_note(noteable, opts)
84

Jan Provaznik committed
85 86
          if note.valid?
            present note, with: Entities.const_get(note.class.name)
87
          else
Jan Provaznik committed
88
            bad_request!("Note #{note.errors.messages}")
Nihad Abbasov committed
89 90
          end
        end
91

Jan Provaznik committed
92
        desc "Update an existing #{noteable_type.to_s.downcase} note" do
93 94 95 96 97 98 99 100
          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
Jan Provaznik committed
101
          noteable = find_noteable(parent_type, noteables_str, params[:noteable_id])
102

Jan Provaznik committed
103
          update_note(noteable, params[:note_id])
104 105
        end

Jan Provaznik committed
106
        desc "Delete a #{noteable_type.to_s.downcase} note" do
107 108 109 110 111 112 113
          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
Jan Provaznik committed
114
          noteable = find_noteable(parent_type, noteables_str, params[:noteable_id])
115

Jan Provaznik committed
116
          delete_note(noteable, params[:note_id])
117
        end
118 119 120 121
      end
    end
  end
end