BigW Consortium Gitlab

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

6
    NOTEABLE_TYPES = [Issue, MergeRequest, Snippet]
7 8 9 10 11 12 13 14 15

    resource :projects do
      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:
16
        #   id (required) - The ID of a project
17 18
        #   noteable_id (required) - The ID of an issue or snippet
        # Example Request:
Nihad Abbasov committed
19 20
        #   GET /projects/:id/issues/:noteable_id/notes
        #   GET /projects/:id/snippets/:noteable_id/notes
21
        get ":id/#{noteables_str}/:#{noteable_id_str}/notes" do
22
          @noteable = user_project.send(noteables_str.to_sym).find(params[noteable_id_str.to_sym])
23

24
          if can?(current_user, noteable_read_ability_name(@noteable), @noteable)
25 26 27 28 29 30 31 32 33 34 35 36 37
            # 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.
            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(@noteable.notes).
              reject { |n| n.cross_reference_not_visible_for?(current_user) }
            present notes, with: Entities::Note
          else
38
            not_found!("Notes")
39
          end
40
        end
Nihad Abbasov committed
41 42 43 44

        # Get a single +noteable+ note
        #
        # Parameters:
45
        #   id (required) - The ID of a project
Nihad Abbasov committed
46 47 48 49 50 51
        #   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
52
          @noteable = user_project.send(noteables_str.to_sym).find(params[noteable_id_str.to_sym])
Nihad Abbasov committed
53
          @note = @noteable.notes.find(params[:note_id])
54
          can_read_note = can?(current_user, noteable_read_ability_name(@noteable), @noteable) && !@note.cross_reference_not_visible_for?(current_user)
55

56
          if can_read_note
57
            present @note, with: Entities::Note
58 59
          else
            not_found!("Note")
60
          end
Nihad Abbasov committed
61
        end
Nihad Abbasov committed
62 63 64 65

        # Create a new +noteable+ note
        #
        # Parameters:
66
        #   id (required) - The ID of a project
Nihad Abbasov committed
67 68
        #   noteable_id (required) - The ID of an issue or snippet
        #   body (required) - The content of a note
69
        #   created_at (optional) - The date
Nihad Abbasov committed
70 71 72 73
        # 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
74
          required_attributes! [:body]
75

76 77 78 79 80 81
          opts = {
           note: params[:body],
           noteable_type: noteables_str.classify,
           noteable_id: params[noteable_id_str]
          }

82 83 84 85
          if params[:created_at] && (current_user.is_admin? || user_project.owner == current_user)
            opts[:created_at] = params[:created_at]
          end

86
          @note = ::Notes::CreateService.new(user_project, current_user, opts).execute
Nihad Abbasov committed
87

88
          if @note.valid?
89 90
            present @note, with: Entities::Note
          else
91
            not_found!("Note #{@note.errors.messages}")
Nihad Abbasov committed
92 93
          end
        end
94 95 96 97 98 99 100 101 102 103 104 105 106 107

        # Modify existing +noteable+ note
        #
        # Parameters:
        #   id (required) - The ID of a project
        #   noteable_id (required) - The ID of an issue or snippet
        #   node_id (required) - The ID of a note
        #   body (required) - New content of a note
        # Example Request:
        #   PUT /projects/:id/issues/:noteable_id/notes/:note_id
        #   PUT /projects/:id/snippets/:noteable_id/notes/:node_id
        put ":id/#{noteables_str}/:#{noteable_id_str}/notes/:note_id" do
          required_attributes! [:body]

108 109 110
          note = user_project.notes.find(params[:note_id])

          authorize! :admin_note, note
111 112

          opts = {
113
            note: params[:body]
114 115
          }

116
          @note = ::Notes::UpdateService.new(user_project, current_user, opts).execute(note)
117 118 119 120

          if @note.valid?
            present @note, with: Entities::Note
          else
121
            render_api_error!("Failed to save note #{note.errors.messages}", 400)
122 123 124
          end
        end

125
        # Delete a +noteable+ note
126 127 128 129 130 131 132 133 134 135 136
        #
        # Parameters:
        #   id (required) - The ID of a project
        #   noteable_id (required) - The ID of an issue, MR, or snippet
        #   node_id (required) - The ID of a note
        # Example Request:
        #   DELETE /projects/:id/issues/:noteable_id/notes/:note_id
        #   DELETE /projects/:id/snippets/:noteable_id/notes/:node_id
        delete ":id/#{noteables_str}/:#{noteable_id_str}/notes/:note_id" do
          note = user_project.notes.find(params[:note_id])
          authorize! :admin_note, note
137

138
          ::Notes::DeleteService.new(user_project, current_user).execute(note)
139

140
          present note, with: Entities::Note
141
        end
142 143
      end
    end
144 145 146

    helpers do
      def noteable_read_ability_name(noteable)
147
        "read_#{noteable.class.to_s.underscore}".to_sym
148 149
      end
    end
150 151
  end
end