Commit 01eab583d0d16b44554a9790fb502f14ea84faf0
1 parent
aaa1c942
Exists in
master
and in
4 other branches
API: list wall, snippet and issue notes
Showing
4 changed files
with
94 additions
and
1 deletions
Show diff stats
lib/api.rb
lib/api/entities.rb
| ... | ... | @@ -0,0 +1,38 @@ |
| 1 | +module Gitlab | |
| 2 | + # Notes API | |
| 3 | + class Notes < Grape::API | |
| 4 | + before { authenticate! } | |
| 5 | + | |
| 6 | + NOTEABLE_TYPES = [Issue, Snippet] | |
| 7 | + | |
| 8 | + resource :projects do | |
| 9 | + # Get a list of project wall notes | |
| 10 | + # | |
| 11 | + # Parameters: | |
| 12 | + # id (required) - The ID or code name of a project | |
| 13 | + # Example Request: | |
| 14 | + # GET /projects/:id/notes | |
| 15 | + get ":id/notes" do | |
| 16 | + @notes = user_project.common_notes | |
| 17 | + present paginate(@notes), with: Entities::Note | |
| 18 | + end | |
| 19 | + | |
| 20 | + NOTEABLE_TYPES.each do |noteable_type| | |
| 21 | + noteables_str = noteable_type.to_s.underscore.pluralize | |
| 22 | + noteable_id_str = "#{noteable_type.to_s.underscore}_id" | |
| 23 | + | |
| 24 | + # Get a list of project +noteable+ notes | |
| 25 | + # | |
| 26 | + # Parameters: | |
| 27 | + # id (required) - The ID or code name of a project | |
| 28 | + # noteable_id (required) - The ID of an issue or snippet | |
| 29 | + # Example Request: | |
| 30 | + # GET /projects/:id/noteable/:noteable_id/notes | |
| 31 | + get ":id/#{noteables_str}/:#{noteable_id_str}/notes" do | |
| 32 | + @noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"]) | |
| 33 | + present paginate(@noteable.notes), with: Entities::Note | |
| 34 | + end | |
| 35 | + end | |
| 36 | + end | |
| 37 | + end | |
| 38 | +end | ... | ... |
| ... | ... | @@ -0,0 +1,52 @@ |
| 1 | +require 'spec_helper' | |
| 2 | + | |
| 3 | +describe Gitlab::API do | |
| 4 | + include ApiHelpers | |
| 5 | + | |
| 6 | + let(:user) { create(:user) } | |
| 7 | + let!(:project) { create(:project, owner: user) } | |
| 8 | + let!(:issue) { create(:issue, project: project, author: user) } | |
| 9 | + let!(:snippet) { create(:snippet, project: project, author: user) } | |
| 10 | + let!(:issue_note) { create(:note, noteable: issue, project: project, author: user) } | |
| 11 | + let!(:snippet_note) { create(:note, noteable: snippet, project: project, author: user) } | |
| 12 | + let!(:wall_note) { create(:note, project: project, author: user) } | |
| 13 | + before { project.add_access(user, :read) } | |
| 14 | + | |
| 15 | + describe "GET /projects/:id/notes" do | |
| 16 | + context "when unauthenticated" do | |
| 17 | + it "should return authentication error" do | |
| 18 | + get api("/projects/#{project.id}/notes") | |
| 19 | + response.status.should == 401 | |
| 20 | + end | |
| 21 | + end | |
| 22 | + | |
| 23 | + context "when authenticated" do | |
| 24 | + it "should return project wall notes" do | |
| 25 | + get api("/projects/#{project.id}/notes", user) | |
| 26 | + response.status.should == 200 | |
| 27 | + json_response.should be_an Array | |
| 28 | + json_response.first['body'].should == wall_note.note | |
| 29 | + end | |
| 30 | + end | |
| 31 | + end | |
| 32 | + | |
| 33 | + describe "GET /projects/:id/noteable/:noteable_id/notes" do | |
| 34 | + context "when noteable is an Issue" do | |
| 35 | + it "should return an array of notes" do | |
| 36 | + get api("/projects/#{project.id}/issues/#{issue.id}/notes", user) | |
| 37 | + response.status.should == 200 | |
| 38 | + json_response.should be_an Array | |
| 39 | + json_response.first['body'].should == issue_note.note | |
| 40 | + end | |
| 41 | + end | |
| 42 | + | |
| 43 | + context "when noteable is a Snippet" do | |
| 44 | + it "should return an array of notes" do | |
| 45 | + get api("/projects/#{project.id}/snippets/#{snippet.id}/notes", user) | |
| 46 | + response.status.should == 200 | |
| 47 | + json_response.should be_an Array | |
| 48 | + json_response.first['body'].should == snippet_note.note | |
| 49 | + end | |
| 50 | + end | |
| 51 | + end | |
| 52 | +end | ... | ... |