notes.rb 3.71 KB
Newer Older
1 2 3 4 5
module Gitlab
  # Notes API
  class Notes < Grape::API
    before { authenticate! }

6
    NOTEABLE_TYPES = [Issue, MergeRequest, Snippet]
7 8 9 10 11

    resource :projects do
      # Get a list of project wall notes
      #
      # Parameters:
12
      #   id (required) - The ID of a project
13 14 15
      # Example Request:
      #   GET /projects/:id/notes
      get ":id/notes" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
16
        @notes = user_project.notes.common
17 18 19
        present paginate(@notes), with: Entities::Note
      end

Nihad Abbasov's avatar
Nihad Abbasov committed
20 21 22
      # Get a single project wall note
      #
      # Parameters:
23
      #   id (required) - The ID of a project
Nihad Abbasov's avatar
Nihad Abbasov committed
24 25 26 27
      #   note_id (required) - The ID of a note
      # Example Request:
      #   GET /projects/:id/notes/:note_id
      get ":id/notes/:note_id" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
28
        @note = user_project.notes.common.find(params[:note_id])
Nihad Abbasov's avatar
Nihad Abbasov committed
29 30 31
        present @note, with: Entities::Note
      end

32 33 34
      # Create a new project wall note
      #
      # Parameters:
35
      #   id (required) - The ID of a project
36 37 38 39
      #   body (required) - The content of a note
      # Example Request:
      #   POST /projects/:id/notes
      post ":id/notes" do
40
        required_attributes! [:body]
41

42 43 44 45 46 47
        @note = user_project.notes.new(note: params[:body])
        @note.author = current_user

        if @note.save
          present @note, with: Entities::Note
        else
48
          # :note is exposed as :body, but :note is set on error
49
          bad_request!(:note) if @note.errors[:note].any?
50 51 52 53
          not_found!
        end
      end

54 55 56 57 58 59 60
      NOTEABLE_TYPES.each do |noteable_type|
        noteables_str = noteable_type.to_s.underscore.pluralize
        noteable_id_str = "#{noteable_type.to_s.underscore}_id"

        # Get a list of project +noteable+ notes
        #
        # Parameters:
61
        #   id (required) - The ID of a project
62 63
        #   noteable_id (required) - The ID of an issue or snippet
        # Example Request:
Nihad Abbasov's avatar
Nihad Abbasov committed
64 65
        #   GET /projects/:id/issues/:noteable_id/notes
        #   GET /projects/:id/snippets/:noteable_id/notes
66 67 68 69
        get ":id/#{noteables_str}/:#{noteable_id_str}/notes" do
          @noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"])
          present paginate(@noteable.notes), with: Entities::Note
        end
Nihad Abbasov's avatar
Nihad Abbasov committed
70 71 72 73

        # Get a single +noteable+ note
        #
        # Parameters:
74
        #   id (required) - The ID of a project
Nihad Abbasov's avatar
Nihad Abbasov committed
75 76 77 78 79 80 81 82 83 84
        #   noteable_id (required) - The ID of an issue or snippet
        #   note_id (required) - The ID of a note
        # Example Request:
        #   GET /projects/:id/issues/:noteable_id/notes/:note_id
        #   GET /projects/:id/snippets/:noteable_id/notes/:note_id
        get ":id/#{noteables_str}/:#{noteable_id_str}/notes/:note_id" do
          @noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"])
          @note = @noteable.notes.find(params[:note_id])
          present @note, with: Entities::Note
        end
Nihad Abbasov's avatar
Nihad Abbasov committed
85 86 87 88

        # Create a new +noteable+ note
        #
        # Parameters:
89
        #   id (required) - The ID of a project
Nihad Abbasov's avatar
Nihad Abbasov committed
90 91 92 93 94 95
        #   noteable_id (required) - The ID of an issue or snippet
        #   body (required) - The content of a note
        # Example Request:
        #   POST /projects/:id/issues/:noteable_id/notes
        #   POST /projects/:id/snippets/:noteable_id/notes
        post ":id/#{noteables_str}/:#{noteable_id_str}/notes" do
Sebastian Ziebell's avatar
Sebastian Ziebell committed
96
          required_attributes! [:body]
97

Nihad Abbasov's avatar
Nihad Abbasov committed
98 99 100 101 102 103 104 105 106 107 108
          @noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"])
          @note = @noteable.notes.new(note: params[:body])
          @note.author = current_user
          @note.project = user_project

          if @note.save
            present @note, with: Entities::Note
          else
            not_found!
          end
        end
109 110 111 112
      end
    end
  end
end