From c946bf886c2c07491a4595e93861df02dbf809f4 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Thu, 29 Nov 2012 12:06:24 -0800 Subject: [PATCH] API: create new notes --- lib/api/notes.rb | 22 ++++++++++++++++++++++ spec/requests/api/notes_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/api/notes.rb b/lib/api/notes.rb index 84b6beb5c71..924eeaa3618 100644 --- a/lib/api/notes.rb +++ b/lib/api/notes.rb @@ -48,6 +48,28 @@ module Gitlab @note = @noteable.notes.find(params[:note_id]) present @note, with: Entities::Note end + + # Create a new +noteable+ note + # + # Parameters: + # id (required) - The ID or code name of a project + # 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 + @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 end end end diff --git a/spec/requests/api/notes_spec.rb b/spec/requests/api/notes_spec.rb index 175d405848b..b7c8ffaf320 100644 --- a/spec/requests/api/notes_spec.rb +++ b/spec/requests/api/notes_spec.rb @@ -67,4 +67,24 @@ describe Gitlab::API do end end end + + describe "POST /projects/:id/noteable/:noteable_id/notes" do + context "when noteable is an Issue" do + it "should create a new issue note" do + post api("/projects/#{project.id}/issues/#{issue.id}/notes", user), body: 'hi!' + response.status.should == 201 + json_response['body'].should == 'hi!' + json_response['author']['email'].should == user.email + end + end + + context "when noteable is a Snippet" do + it "should create a new snippet note" do + post api("/projects/#{project.id}/snippets/#{snippet.id}/notes", user), body: 'hi!' + response.status.should == 201 + json_response['body'].should == 'hi!' + json_response['author']['email'].should == user.email + end + end + end end