Commit c946bf886c2c07491a4595e93861df02dbf809f4

Authored by Nihad Abbasov
1 parent 1c5aa848

API: create new notes

lib/api/notes.rb
... ... @@ -48,6 +48,28 @@ module Gitlab
48 48 @note = @noteable.notes.find(params[:note_id])
49 49 present @note, with: Entities::Note
50 50 end
  51 +
  52 + # Create a new +noteable+ note
  53 + #
  54 + # Parameters:
  55 + # id (required) - The ID or code name of a project
  56 + # noteable_id (required) - The ID of an issue or snippet
  57 + # body (required) - The content of a note
  58 + # Example Request:
  59 + # POST /projects/:id/issues/:noteable_id/notes
  60 + # POST /projects/:id/snippets/:noteable_id/notes
  61 + post ":id/#{noteables_str}/:#{noteable_id_str}/notes" do
  62 + @noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"])
  63 + @note = @noteable.notes.new(note: params[:body])
  64 + @note.author = current_user
  65 + @note.project = user_project
  66 +
  67 + if @note.save
  68 + present @note, with: Entities::Note
  69 + else
  70 + not_found!
  71 + end
  72 + end
51 73 end
52 74 end
53 75 end
... ...
spec/requests/api/notes_spec.rb
... ... @@ -67,4 +67,24 @@ describe Gitlab::API do
67 67 end
68 68 end
69 69 end
  70 +
  71 + describe "POST /projects/:id/noteable/:noteable_id/notes" do
  72 + context "when noteable is an Issue" do
  73 + it "should create a new issue note" do
  74 + post api("/projects/#{project.id}/issues/#{issue.id}/notes", user), body: 'hi!'
  75 + response.status.should == 201
  76 + json_response['body'].should == 'hi!'
  77 + json_response['author']['email'].should == user.email
  78 + end
  79 + end
  80 +
  81 + context "when noteable is a Snippet" do
  82 + it "should create a new snippet note" do
  83 + post api("/projects/#{project.id}/snippets/#{snippet.id}/notes", user), body: 'hi!'
  84 + response.status.should == 201
  85 + json_response['body'].should == 'hi!'
  86 + json_response['author']['email'].should == user.email
  87 + end
  88 + end
  89 + end
70 90 end
... ...